Get Control ID Along With Master Page in ASP.Net

This question pops up on the ASP.NET forums where I moderate:

"How do I find an HTML element on a Master Page, from a child page, using jQuery?"

I licked my chops. I like questions like this because when I don't know the answer, it gives me an excuse to explore and learn.
The problem is that element ids on the Master Pages get mangled, or decorated, to prevent duplicate ids on the final rendered HTML.

For instance a TextBox with an id like this: MasterPageTextBox

Ends up with an id like this: ctl00$MasterPageTextBox

Solution 1

We could hard-code the mangled id into the jQuery search criteria and it would work. But what a maintenance nightmare, in the future, the mangled id might change: Not acceptable. When people pay you money to write code, you should write good code.

Solution 2

If you are using ASP.NET 4, you have control over the generated ids and can make them predictable. Then you can hard-code the generated id into the CSS selector. However, that isn't the case for most sites at this time.

Solution 3

After Googling and Binging around a bit, I came up with this approach to use in the Master Page:

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     Page.ClientScript.RegisterHiddenField("HiddenFieldClientID"this.MasterPageTextBox.ClientID);  
  4. } 

The code above takes the generated ClientID and puts it in a HiddenField that gets sent to the browser. The jQuery code in the child page can then get the value in the HiddenField and use it to search for the element. I thought this was pretty cool, but the jQuery code wouldn't compile because the HiddenField wasn't on its page. So an empty HiddenField control must be placed on the page. It's messy but it works! Here is how the jQuery on the child page accesses the hidden field and then accesses the TextBox on the Master Page:

  1. // ---- SetMasterPageTextBox ----  
  2. // write hello in a textbox field on the master page  
  3. function SetMasterPageTextBox()  
  4. {  
  5.     // Get the hidden field  
  6.     var HidField = $("#HiddenFieldClientID");  
  7.     if (HidField.length == 1)  
  8.     {  
  9.         // get the contents of the hidden field  
  10.         var ClientID = HidField[0].value;  
  11.         // use it as the ID of the TextBox control  
  12.         $("#" + ClientID).val("Hello");  
  13.     }  
  14. } 

I went to post my 'brilliant' answer, but in the meantime another forum member posted an answer that was far superior to mine.

Solution 4

Two things make this next solution work.

Span tag ID's are not mangled or decorated.

CSS selectors are cool, really cool. The following is a quick review:

div, p: The comma (,) operator means and. All divs and paragraphs on the page will be selected.
div > p: The greater than (>) operator means "direct parent of". Any paragraphs directly inside of any divs are selected
div p: The space ( ) operator means "ancestor of". Any paragraphs inside a div are selected, even if they are inside of other elements within the div.

In the Master Page wrap the TextBox with a span element as in the following:

  1. <%-- Span is to allow child page to jQuery select textbox--%>  
  2. <span id="SpanMyTextBox">  
  3. <asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>  
  4. </span> 

Here's what it looks like rendered, note the TextBox id gets mangled but the span id remains unscathed: 

  1. <span id="SpanMyTextBox">  
  2. <input name="ctl00$MyTextBox" type="text" id="ctl00_MyTextBox" />  
  3. </span> 

In the Child Page we can use the greater than or the space operator. The greater than operator is more explicit as to our intent. We use "input" because textboxes render as HTML input elements.

So the CSS selector is: #SpanMyTextBox > input

  1. // ---- SetMasterPageTextBox ----  
  2. // write hello in a textbox field on the master page  
  3. function SetMasterPageTextBox()  
  4. {  
  5.     //Textbox is wrapped in span element  
  6.     $("#SpanMyTextBox > input").val("Hello");  
  7. } 

Now, isn't that better? Of course, it's up to the developer to ensure duplicate span ids are not used.


Solution 5: (from the comments)

You can also use a wild-card CSS selector.


The preceding line matches all HTML input elements with an id attribute that ends with "MyTextBox".

It will match:

  1. <asp:TextBox ID="ctl00$MyTextBox" runat="server"...  
  2. <asp:TextBox ID="LaLaLaMyTextBox" runat="server"...  
  3. <asp:TextBox ID="abc123MyTextBox" runat="server"... 

I hope someone finds this useful.

Similar Articles