防止显示name=""在HtmlInputButton

本文关键字:quot HtmlInputButton 显示 name | 更新日期: 2023-09-27 18:18:02

如何防止name="..."属性作为HtmlInputButton的一部分呈现?

代码:

 var button = new System.Web.UI.HtmlControls.HtmlInputButton();
 button.Value = "abc";

呈现的HTML:

<input name="ctl00$AppName_Content$ctl01" type="button" value="abc"/>

防止显示name=""在HtmlInputButton

您可以覆盖渲染方法并阻止添加属性。

private class RemoveNamesHtmlTextWriter : HtmlTextWriter
{    
    public override void WriteAttribute(string name, string value, bool fEncode) 
    { 
        if (name.Equals("name", StringComparison.OrdinalIgnoreCase)) return;
            return; 
        base.WriteAttribute(name, value, fEncode); 
    }
}
protected override void Render(HtmlTextWriter writer)
{
    var RemoveNamesWriter = new RemoveNamesHtmlTextWriter(writer);
    base.Render(RemoveNamesWriter);
}

如何删除'name'属性