How to remove the 'name' attribute from server controls?

The following asp.net control code:

<asp:TextBox runat="server" ID="LimitTextBox" Text="20" ClientIDMode="Static" /> 

Generates the following HTML code:

 <input name="ctl11$ctl00$ctl02$TeamPlayerSelector$LimitTextBox" type="text" value="20" id="LimitTextBox"> 

Attribute id - as required, but how can I remove the name attribute? This is not required for me, nor is it too long to transfer it to the user's browser.

How can I prevent the creation of the name attribute? Thanks

PS I work under ASP.NET 4.0

+9
Dec 12 '10 at 22:31
source share
6 answers

create a Filter (a class that inherits from Stream ), assign it to your HttpContext.Response.Filter attribute, and you will overwrite the Write method in it to remove all name tags from the generated html :)

See this page for more information http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx

Update

Looking at the source code for the TextBox , it shows that the name was actually added to the Attributes list during rendering, so it should be possible to interfere with the rendering of the TextBox class and prevent this attribute from being added. It should do

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

Update again

How could I forget! You do not even need to subclass your text box. In asp.net, you can determine which type of HtmlTextWriter you want to use for each control, so you can simply configure that each TextBox control should use an instance of your own NoNamesHtmlTextWriter like this

 <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); } } 
+7
Dec 12 '10 at
source share

For some unknown reason, overriding WriteAttribute does not work. I replaced it with:

 public override void AddAttribute(HtmlTextWriterAttribute key, string value) { if (key == HtmlTextWriterAttribute.Name) return; base.AddAttribute(key, value); } 

And it worked like a charm. Also, if you just need your own name, you can simply override the UniqueID property:

 public class MyCustomControl : TextBox { public override string UniqueID { get { //return base.UniqueID; return "test123"; } } } 

Thanks for your help!

+3
Jun 24 '13 at 18:08
source share

Setting EnableViewState = "False" will reduce the name. You can also create a class that inherits the Textbox control and overrides the Render procedure to not include the name.

 Public Class CustomTextBox Inherits TextBox Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) MyBase.Render(writer) 'Simplified rendering of control... writer.WriteLine("<input type='text' id='" & MyBase.ClientID & "'>") End Sub End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim MyCustomTextBox As New CustomTextBox form1.Controls.Add(MyCustomTextBox) End Sub 

Alternatively, if you do not want to add a control at runtime, you can make your CustomTextBox as a ServerControl so that you can add it at design time.

+2
Dec 12 '10 at 23:00
source share

which ASP.Net controls are displayed in the name attributes comes from the UniqueID property. It is not necessarily a good idea to override it, because ASP.NET uses this to find feedback control to route feedback and events. However, if you are sure that this is normal in your scenario, you can certainly override the UniqueID property after @Pauli Østerø's answer. Enjoy!

+1
Dec 12 2018-10-12T00:
source share

I think it’s better to change the name property as well as the identifier.

Just try using jQuery on document.ready(function(){})

 document.ready(function(){ $.each($('div').children(), function() { $(this).attr("name",$(this).attr("id")); }); }); 
0
Apr 30 '14 at 7:43
source share

You can override the name property and return whatever you want ( http://referencesource.microsoft.com/#System.Web/xsp/system/Web/UI/HtmlControls/HtmlInputControl.cs ).

0
12 Oct '14 at 19:13
source share



All Articles