views:

1353

answers:

4

I'm having a problem with the ID property of dynamically loaded UserControls changing during the Page lifecycle. More specifically the ID property changes when the system calls Page.Form.RenderControl(htmlTextWriter); Before it is called the control has ID "ctl84", but after the call it has ID "ctl99".

The output from htmlTextWriter contains the original ID, however inspecting the Control's ID property in the VS 2008 debugger reveals that it has changed.

The application is running inside an MCMS 2002 (Microsoft CMS 2002) framework using .NET 2.0, converted from 1.1 and xhtmlConformance="Legacy" is not enabled.

I need the ID to be constant throughout the Page lifecycle.

Edit: Setting the ID property manually is not an option.

+1  A: 

Are you explicitly assigning an ID to the control from code?

If you are the ID should stay the same.

It doesn't explain why it's changing though - my guess is ... is not the same control. Chances are for some reason you control generation routine is running twice or smt like that.

Put a breakpoint where the control is genretated and see if it gets hit twice - If so, there you go, that's your problem.

JohnIdol
I am not assigning any ID to the control, I'm intentionally leaving it up to ASP.NET to assign IDs.
jamaicahest
is there any reason why you're doing that? put a breakpoint where the control is genretated and see if it gets hit twice - If so that's your problem
JohnIdol
Yes it's because they are loaded from the MCMS system, where you can basically add a bunch of controls to a virtual page thorugh an editor and let the system figure out how to render it.
jamaicahest
So you can't put a breakpoint were the control is being generated?
JohnIdol
Yes, I have and it only gets hit once during Page lifecycle. However digging further I discovered that after Page.Form.RenderControl returns, the ID property shows as null in the debugger watcher until I access either ClientID or UniqueID, at which point it is changed to "ctl99".
jamaicahest
After even further digging I found that during rendering the CMS system adds the UserControl to a label, renders it, clears the label's Controls collection, which causes the change in the ID property. Now I'll just have to find a way around that :) Thanks for the help.
jamaicahest
A: 

to be sure it's the same instance of the control and not another, check the GetHashCode() method of the control.

pomarc
GetHashCode() returns the same number before and after Page.Form.RenderControl()
jamaicahest
A: 

When you include an asp.net control , at run time the generated id changes. You cannot predict the exact client id that is generated. What you can do is use the ClienID property provided by the user control.

Button btnSave = new Button();

btnSave.ID = "btnSave";

string clientID = btnSave.ClientID;

If you check cientID it will be something like "ctl88_99_***_btnSave".

rAm
The ClientID changes as well, the problem is that the ID changes _during_ Page life cycle. Not on a postback or page reload.
jamaicahest
postback is part of the page lifecycle
JohnIdol
i agree clientID changes. The use of having client id is to use this for performing operations than a hard coded client id. I guess you are trying to access the control from client side. in this case, use a hidden variable at the end of the page and set the value to the clientid of asp control.
rAm
A: 

Yes I think rAm is right.. in my previous experiences.. I have noticed that explicit id value works all the time and would recommend the same.. Andy

Andy