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.
jamaicahest : I am not assigning any ID to the control, I'm intentionally leaving it up to ASP.NET to assign IDs.JohnIdol : 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 problemjamaicahest : 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.JohnIdol : So you can't put a breakpoint were the control is being generated?jamaicahest : 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. -
to be sure it's the same instance of the control and not another, check the GetHashCode() method of the control.
jamaicahest : GetHashCode() returns the same number before and after Page.Form.RenderControl() -
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".
jamaicahest : The ClientID changes as well, the problem is that the ID changes _during_ Page life cycle. Not on a postback or page reload.JohnIdol : postback is part of the page lifecyclerAm : 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. -
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
0 comments:
Post a Comment