HTML ‘name’ attribute generated for ASP.net child controls, instead of the unique ‘ID’ attribute

The generated HTML code for my custom ASP.net server control generates the name attribute for child controls, instead of the id attribute. Something like this :

<span id="GridView2_ctl02_editdis">
    <input type="text" name="GridView2$ctl02$editdis$ctl00"/>
</span>

The ID for the custom control itself is apparently proper.

What is even stranger for me, is that the ID does get generated sometimes (I do not know under what conditions). But a FindControl() with that ID returns null on the server side. FindControl() with the value of the name attribute works just fine.

Something like this :

<span class="TextBox" id="GridView2_ctl02_editdis">
    <input type="text" id="GridView2_ctl02_editdis_ctl00" name="GridView2$ctl02$editdis$ctl00"/>
</span>

For the above, FindControl("GridView2$ctl02$editdis$ctl00") works fine, FindControl("GridView2_ctl02_editdis_ctl00") doesn’t.

How do I ensure consistent and predictable IDs?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

They are consistent.

Internally, controls that are children of a naming control (i.e. GridView), have their full IDs built by appending IDs of their parents using “$”. In case of a grid, it’s gridID$rowID$cellID$mycontrolID. This is necessary to differentiate between multiple instances of the same child control (i.e. mycontrolID). Why “$” and not “_”? I guess because many people already tend to name their controls “my_control_something” and the “$” symbol is as good as any.

So, the GridView2$ctl02$editdis$ctl00 is the right ID and that’s why it’s used as a name for controls such as INPUT. When post back occurs, the framework needs to be able to match form keys with appropriate controls.

The confusion with IDs, I think, comes from the fact that an ID you are using inside .aspx and the ID you see in HTML are two different things. The client-side IDs are just that. For whatever reason, when a control gets rendered (using ClientID property), all “$” get replaced with “_”. My guess is that this was done to make it javascript/css friendly.

Now, about that FindControl(“GridView2$ctl02$editdis$ctl00”)… You really should try avoiding it whenever possible. FindControl is a recursive function, which breaks “GridView2$ctl02$editdis$ctl00” into “GridView2” and “ctl02$editdis$ctl00”, finds GridView2 and asks if it has the “ctl02$editdis$ctl00” as a child control. The process repeats for each part separated by $.

On a side note, whenever you find yourself calling Page.FindControl for some deeply buried control, you need to examine the pattern and ask why. For instance, whatever needs to be done with the “GridView2$ctl02$editdis$ctl00”, most likely needs to be done with a “GridView2$ctl02$editdis$ctl01” as well. In that case, it probably needs to be handled on OnItemCreated or on OnItemDataBound, where you have access to a row who “knows” about “ctl00”.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x