Preventing ASP.NET from generating unique ID's

Posted at Thu, 05 Jan 2006 21:55:26 GMT by Wilco Bauwer

Damien Guard mentioned a couple of times how that he hated ASP.NET's 'ID mangling': based on the hierarchy of containers, ASP.NET renders a unique ID to ensure that you don't have to worry about ID's on the client being globally unique. This was problematic for him with relation to external applications which sometimes require you to use specific ID's to let them 'integrate'. While I understand that ASP.NET probably tries to target the 99% case, I can also see that this may be problematic in a few cases. And it doesn't exactly seem to be trivial to enforce that the specified ID is the ID that is rendered.

That's why I extended the Wilco.Web ASP.NET library with an IDOverride control. It lets you specify which controls should render their ID as is. Please use this with caution, since you can now use multiple controls with the same ID, and all you would get is unexpected results.

Here's what it looks like:

ASP.NET:
1 
2 
3 
4 
5 
6 
7 
8 
9 

<asp:Content ContentPlaceHolderID="main">
  <asp:TextBox ID="userName" Runat="server" />
  <asp:TextBox ID="password" Runat="server" TextMode="Password">

  <WilcoWeb:IDOverride runat="server">
    <WilcoWeb:Override TargetControlID="userName" />
    <WilcoWeb:Override TargetControlID="password" />
  </WilcoWeb:IDOverride>
</asp:Content>

Doesn't work.

No errors, just doesn't retain the ID.

<%@ Control Language="VB" enabletheming="true" ClassName="PscodeMem" %>
<%@ Register TagPrefix="WilcoWebControls" Namespace="Wilco.Web.UI" Assembly="Wilco.Web" %>


<asp:textbox id="TextBox1" runat="server"></asp:textbox>

<WilcoWebControls:IDOverride runat="server">
<WilcoWebControls:Override targetcontrolid="TextBox1" />
</WilcoWebControls:IDOverride>





Weird. Looks like I forgot to remove something that broke it. Please download the library again to see if it is fixed.
I would like to use the IDOverride control programmatically as I am retrofitting a .Net 1.1 application. This application uses some custom server controls that are causing problem in the Javascript (because of different client ids) as I migrated the application .Net 2.0

In the page_init event, I recursively look for controls I am interested in, and then I add them to the IDOverride control. However, I am not getting the results I expect. The controls are still being rendered with different ids (e.g. ctl100...)

Do you have any thoughts as to how can I approach the problem programmatically. This will save me a lot of time, because I have to do it in the base page class and not on indivial pages.

Thanks

Arvinder Chopra
Your message will be encoded/formatted when it is displayed. If you want to post code, please put the code inside [code=X][/code] tags, where X is the language of your code (C#, ASPX, SQL, etc).
Name:
Email:
(will be encoded using JavaScript to keep it functional and prevent it from being picked up by spammers)
Url:
 
Message:
3 + 3 =