.Net Changes the element IDs

.Net is kindly changing the element ids on my pages by appending a ct100_ to them. As much as I appreciate microsoft trying to help me keep from duplicating ids on my site, I think I can manage it on my own. Can anyone tell me how to prevent this?
Thanks in advance.

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

That’s just how aspnet works. Controls provide the clientid method for you to use in your code behind for this reason.

If you want to refer to objects from js, you can either inject the clientid or use classes or other attributes.


Edit: Note that this only applies to the ASP.NET controls. If you use the HTML controls, the given IDs are preserved. You can access them in your code behind by adding the runat=server attribute to them, too. Obviously these controls break the webforms model with viewstate, etc. but they do give you your desired functionality.

Of course it’s been a while since I worried about it so I could be wrong…(please comment or edit if I am!).

Method 2

You cannot prevent this in the current version of ASP.NET – the next version will allow you to do this. Perhaps ASP.NET MVC is a good choice for you?

Method 3

Any control which has the INamingContainer interface on it will get the control heirarchy appended to it to allow for multiple controls to be on the page without conflicting. You should be using the ClientID property of the control if you wish to know what the id of the element will be on the client.

Method 4

You will need to:
override regular controls’ behavior to decouple the Control.UniqueID property from the Control.ID property
override naming container controls to allow us to control how ID generation is done and how to find a control

References:
http://nunogomes.net/post/2008/06/02/ASPNET-Controls-Improving-automatic-ID-generation-Architectural-Changes-(-Part-3).aspx

http://weblogs.asp.net/nunogomes/archive/2008/06/04/asp-net-controls-improving-automatic-id-generation-architectural-changes-part-3.aspx

http://forums.asp.net/t/1394822.aspx

Method 5

to not use anything on the server side.

This is an inherent aspect of the ASP.NET system, and there is no way to use .NET Server controls and not have the prefixes appended.

To access the client-side name, you can use the myControl.ClientID property.


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
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x