Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ASP.NET name-mangling consistent?

When you have an ASP control like this:

<asp:TreeView ID="TreeItems" runat="server"></asp:TreeView>

The html that it generates mangles the names. If I want to access the ids of the generated items directly, I can try and figure out what it mangles the names to and look for that ID.

Are the names of the items generated guaranteed to be done in a specific way by whatever standard there is from Microsoft? I'm just afraid of this breaking if they release a new version of .NET that does it in a different way. Is there a way to generate the name mangling myself in code?

like image 296
Jonathan Sternberg Avatar asked Jul 23 '26 15:07

Jonathan Sternberg


2 Answers

It isn't guranteed to be consistent (it's an implementation detail)

Use ClientID instead (which gives you the generated id).

like image 75
µBio Avatar answered Jul 26 '26 05:07

µBio


I believe with ASP.NET on .NET 4, you can specify that the IDs be generated a certain way.

See: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

Additionally, if you are using jQuery, you can be sure that the nominal ID will be in the client ID, so you could select it with something like:

$("input[id*='TreeItems']")

See: http://api.jquery.com/attribute-contains-selector/

like image 42
Matthew Groves Avatar answered Jul 26 '26 05:07

Matthew Groves