Tutorial :ASP.NET Dynamic UserControl ID generation problem


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.


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.


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


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".


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »