以下asp.net端控制代码: asp:TextBox runat="server" ID="LimitTextBox" Text="20" ClientIDMode="Static" / 生成这样的HTML代码: input name="ctl11$ctl00$ctl02$TeamPlayerSelector$LimitTextBox" type="text" value="20" id="LimitTextB
<asp:TextBox runat="server" ID="LimitTextBox" Text="20" ClientIDMode="Static" />
生成这样的HTML代码:
<input name="ctl11$ctl00$ctl02$TeamPlayerSelector$LimitTextBox" type="text" value="20" id="LimitTextBox">
ID属性 – 根据需要,但如何删除’name’属性?这对我来说并不是必需的,也不会将其转移到用户浏览器.
如何防止“名称”属性生成?谢谢
附:我在ASP.NET 4.0下工作
创建一个Filter(继承自Stream的类),将其分配给您的HttpContext.Response.Filter属性,然后在其中覆盖Write方法,从生成的html中删除所有名称标签:)有关更多信息,请参见此页面http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx
更新
查看TextBox的源代码,它显示Name在渲染过程中实际上已添加到Attributes-list中,因此应该可以干扰TextBox类的呈现并阻止添加此属性.这应该做
public class NoNamesTextBox : TextBox { private class NoNamesHtmlTextWriter : HtmlTextWriter { public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) {} public override void WriteAttribute(string name, string value, bool fEncode) { if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return; base.WriteAttribute(name, value, fEncode); } } protected override void Render(HtmlTextWriter writer) { var noNamesWriter = new NoNamesHtmlTextWriter(writer); base.Render(noNamesWriter); } }
再次更新
我怎么会忘记!您甚至不需要对文本框进行子类化.在asp.net中,您可以定义每个控件要使用的HtmlTextWriter类型,因此您可以配置每个TextBox控件应该使用您自己的NoNamesHtmlTextWriter实例,如下所示
<browsers> <browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.WebControls.TextBox" adapterType="NoNamesTextBoxAdapter" /> </controlAdapters> </browser> </browsers> public class NoNamesTextBoxAdapter : ControlAdapter { private class NoNamesHtmlTextWriter : HtmlTextWriter { public NoNamesHtmlTextWriter(TextWriter writer) : base(writer) { } public override void WriteAttribute(string name, string value, bool fEncode) { if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return; base.WriteAttribute(name, value, fEncode); } } protected override void Render(HtmlTextWriter writer) { var noNamesRender = new HtmlTextWriter(writer); base.Render(noNamesRender); } }