主要问题:向MVC2解决方案/项目中的所有(!)文本框添加js/jquery方法。
基础知识:如何调用ascx模板?
Solved, see here
(!感谢达林季米特洛夫)如何避免ascx模板覆盖原始样式,类,maxlength等属性?
但是...
指本ASCX模板 “Test.ascx” 将被称为像这里显示:
<%: Html.EditorFor(model => model.Firstname, "Test", new { style = "float: left; width: 4.1em;", maxlength = "4" })%>
的“Test.ascx”模板添加了js/jquery函数。
工作正常,np等票价。
但:“原始”属性style="..." maxlength="..."
将丢失/覆盖。
我目前的解决方案/我 “Test.ascx” 文件:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input type="text"
id="<%: ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>"
name="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>"
value="<%: ViewData.TemplateInfo.FormattedModelValue %>"
style="<%: ViewData["style"] %>"
class="<%: ViewData["class"] %>"
maxlength="<%: ViewData["maxlength"] %>"
onkeyup="Foo(this); return false;"/>
这个作品真的很好,回报/使得它完美:
<input type="text"
id="Firstname"
name="Firstname"
value=""
style="float: left; width: 4.1em;"
class=""
maxlength="4"
onkeyup="Foo(this); return false;"/>
请注意,这里的style="..."
和maxlength="..."
属性处理得好! 我目前使用的“标准” < input ... />
标签,因为我没有得到这个工作:
<%
String tmpConfig = " new {onkeyup=\"Foo(this); return false;\"";
String style = this.ViewData["style"] as String;
if (!String.IsNullOrEmpty(style))
tmpConfig += ", style=\"" + style + "\"";
String @class = this.ViewData["class"] as String;
if (!String.IsNullOrEmpty(@class))
tmpConfig += ", class=\"" + @class + "\"";
String maxlength = this.ViewData["maxlength"] as String;
if (!String.IsNullOrEmpty(maxlength))
tmpConfig += ", maxlength=\"" + maxlength + "\"";
tmpConfig += " } ";
%>
<!-- doesnt work: -->
<%: Html.TextBox( string.Empty,
ViewData.TemplateInfo.FormattedModelValue,
// new { onkeyup = "Foo(this); return false;" } // the style, class, maxlength attributes will be lost/not rendered!
// new { tmpConfig } // result would be the attribut: tempConfig=" onkeyup..."
// new tmpConfig // result: error, tmpConfig isnt object
Html.Encode(tmpConfig) // doesnt work ...
)%>
这使得最后相同,但没有style="..."
和maxlength="..."
属性!
但我想用<%: Html.TextBox(...) %>
代替< input ... />
标签!
因为这会让我有可能避免像class=""
之类的空属性,就像之前所示。
我的错误在哪里?
可能重复【如何:重写所有Html.TextBox - ASCX不叫!为什么?](http://stackoverflow.com/questions/5126696/how-to-overwriting-all-html-textbox-ascx-not-called-why) – 2011-02-27 19:12:00
不,不是双倍。第一个问题是关于“如何调用ascx模板”,在这里我问如何避免覆盖“原始”属性(style,maxlength,...)。请阅读我的文章。谢谢! – user415876 2011-02-27 19:15:16
你可以添加生成的HTML文本框的当前输出,然后告诉你期望会有什么不同吗? – rene 2011-02-27 19:20:00