tags:

views:

271

answers:

5

Hi, .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.

+2  A: 

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?

Andrew Hare
very interesting, I'll have to check this out.
Praesagus
+2  A: 

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!).

Michael Haren
One of the elements in questions is actually an htmlcontrol with an id specified in the html. In spite of that .net still changes my specified id.
Praesagus
Good to know, thanks
Michael Haren
+1  A: 

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.

Stephen Wrighton
+2  A: 

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.

Daniel
+1  A: 

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


Amr ElGarhy
Here is a solution that is not for the faint of heart. I like it!
Praesagus