0

How do I prevent the name="..." attribute from rendering as part of an HtmlInputButton?

Code:

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

Rendered HTML:

<input name="ctl00$AppName_Content$ctl01" type="button" value="abc"/>
Mark
  • 981
  • 2
  • 13
  • 25
  • This may help you: [`How to remove 'name' attribute`](http://stackoverflow.com/a/4424499/1042848) – Vishal Suthar Jun 12 '13 at 05:07
  • That did the trick. Thanks. public override void WriteAttribute(string name, string value, bool fEncode) { if (name == "name) return; base.WriteAttribute(name, value, fEncode); } – Mark Jun 12 '13 at 05:40
  • Glad it helped.!! I just added it as an answer so you can accept it if you want. – Vishal Suthar Jun 12 '13 at 05:56

1 Answers1

0

You can override the render method and prevent the attribute from being added.

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);
}

You can refer here for more information: How to remove 'name' attribute

Community
  • 1
  • 1
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105