How To avoid that an ascx template overwrites the original style, class, maxlength, etc. attributes?
Main issue: Adding to all(!) textboxes in a MVC2 solution/project a js/jquery method.
Basics: How to call an ascx template? Solved, see here (Thanks to Darin Dimitrov!)BUT...
Means the ascx template "Test.ascx" will be called like here shown:
<%: Html.EditorFor(model => model.Firstname, "Test", new { style = "float: left; width: 4.1em;", maxlength = "4" })%>
The "Test.ascx" template adds the js/jquery function.
Works fine, np so fare. But: The "original" attributesstyle="..." maxlength="..."
will be lost/overwritten.
My current solution / my "Test.ascx" file:
<%@ 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="&l开发者_JAVA百科t;%: ViewData["class"] %>"
maxlength="<%: ViewData["maxlength"] %>"
onkeyup="Foo(this); return false;"/>
This works really fine and returns / renders it perfectly:
<input type="text"
id="Firstname"
name="Firstname"
value=""
style="float: left; width: 4.1em;"
class=""
maxlength="4"
onkeyup="Foo(this); return false;"/>
Note that here are the style="..."
and maxlength="..."
attributes are well handled!
I use currently the "standard" < input ... />
tag because I didnt get this to work:
<%
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 ...
)%>
This renders finally the same but WITHOUT the style="..."
and maxlength="..."
attributes!
But I want to use the <%: Html.TextBox(...) %>
instead of the < input ... />
tag!
class=""
and like before shown.
Where is my mistake?
You have to supply the Html.TextBox with a dictionary of String,Object to hold all attributes
<%
var dict = new Dictionary<string, object>();
String style = this.ViewData["style"] as String;
if (!String.IsNullOrEmpty(style))
dict.Add("style", style);
String @class = this.ViewData["class"] as String;
if (!String.IsNullOrEmpty(@class))
dict.Add("class", @class);
String maxlength = this.ViewData["maxlength"] as String;
if (!String.IsNullOrEmpty(maxlength))
dict.Add("maxlength", maxlength);
dict.Add("onkeyup", "Foo(this); return false;");
%>
<%: Html.TextBox( string.Empty,
ViewData.TemplateInfo.FormattedModelValue,
dict
)%>
精彩评论