Skip to main content
 首页 » 编程设计

c#之DateTime -> Textbox (jquery!) -> EditorFor (DateTime.ascx) -> 添加 javascript 事件。如何

2024年02月24日36Free-Thinker

我定义了这个DateTime.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %> 
<%: Html.TextBox(   string.Empty,  
                    (Model.HasValue ? Model.Value.ToString("dd.MM.yyyy") : string.Empty),  
                    new {   @class = "whatever" 
 
                        } 
                 )%> 

aspx 站点 看起来是这样的:

<%: Html.EditorFor(model => model.Data.birthday) %> 

现在的想法是调用 javascript 函数“ABC()” 当文本框失去焦点时(意味着“onBlur”事件) (理想情况下:将 trextbox 的值传递给调用的函数“ABC(birtday.value)”) 但我不想更改 DateTime.ascx 以结束 ALL(!) EditorFor(对于 Datetimes)将有一个 javascript 函数调用!

我的尝试: 这个

<%: Html.EditorFor(model => model.Data.birthday, "onBlur='javascript:ABC();'", "birthday") %> 

结果

<input class="whatever" id="birthday" name="birthday" type="text" value="25.08.2010" /> 

但是 onBlur/javascript 函数调用未设置。

和

<%: Html.EditorFor(model => model.Data.birthday, new { onClick="javascript:ShowHideC1();"} ) %> 

也不行。

(下一步是这个文本框“当然”是一个 jquery 日期选择器;-)

请您参考如下方法:

是的,回答自己的问题很愚蠢,但我想分享我的知识。 ;-)

解决方法:

不要使用 Html.EditorFor!

使用 TextBoxesFor!

<%: Html.TextBoxFor(model => model.Data.birthday, new { onBlur="javascript:ABC();"} )%> 

使用 TextBoxFor 意味着不使用 DateTime.ascx。 当 Controller 中的“...生日”设置为非空值时,输出的格式与 DateTime.ascx 中定义的不同。还意味着将显示小时、分钟和时间。 但是添加了 OnBlur - 事件!这就是目标!