Can I force asp to give a name just like id

I need to process a form full of all kinds of different search controls, however now these controls are inside the main page, so the identifier gets an extra junk file ('ct100 $ Body $ TextBox_Postal' as opposed to 'TextBox_Postal').

I was able to fix this by setting ClientIDMode = CliendIDMode.Static, this works fine because it does not try to include the container naming in the identifier. I am sure that there will never be two identical controls on the page for this to work.

The problem is that when the form is submitted, the controls are processed by names. Names still have the format 'ct1200 $ Body $ ..', so the processform function cannot find any controls. Is there a way to get ASP to set names in Static mode?

+8
source share
2 answers

The short answer is no, you have to redefine the rendering of the name attribute, an example below from this question: ASP.NET: how to remove the 'name' attribute from server controls?

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); } } 
+1
source share

I don’t think there is a way to set the name of the controls correctly, but you can easily change their names using jQuery if this is an option for you.

Example here

Some explanation:

  • Assuming you have this markup:

     <div> <asp:textbox runat="server" id="staticid1" /> <asp:textbox runat="server" id="staticid2" /> <asp:textbox runat="server" id="staticid3" /> </div> 

You can automatically change all of these control names to have the same names as their identifiers, doing something like this on window.load :

  $.each($('div').children(), function() { $(this).attr("name",$(this).attr("id")); }); 

All you need to include jQuery in this work; you can use google cdn: http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

+13
source share

All Articles