Werner Heisenberg Bug aka Observer Effect
Heisenberg Bug (c) Dan.Oscar.Mckinley. 2007
When dynamically creating a dropdown extender control form the ajaxControlToolkit, I was attempting to set the TargetControlID to be the ID of a Linkbutton.
OnPreRender:However, when this ran it told me that TargetControlID cannot be an empty string. Hmm weird, i only have some idea how the uniqueId's are generated, but i dont know how exactly or when in the page lifecycle, but I'm pretty sure it should be available by the preRender phase.
DropDownExtender.TargetControlID = dd.Parent.Controls(2).ID
I set a break point and on this line, and in the immediate window i check the client id
?dd.Parent.Controls(2).ClientID
"GResults1_dg_ctl02_ctl13"
?dd.Parent.Controls(2).ID
"ctl13"
What the hell is going on, the ID is set, i dont know why TargetControlID is an empty string. I continue running, and the page loads fine. I refresh and it errors again. I step through this code, check the ID's, and it works again.
After a little research (aka not checking on google and going straight to a Sr. Developer) i found out that ASP.NET will only build up a control ID, when the ClientID property is referenced. This makes a lot of sense since you dont want to waste time generating ClientID's for every control: only the ones you are planning on referencing on the client side.
I changed the code to reference the ClientID, and now the ID is correctly created (although in this case i'm setting it myself)
OnPreRender:
dd.Parent.Controls(2).ID = dd.Parent.ClientID + "_lb"
DropDownExtender.TargetControlID = dd.Parent.Controls(2).ID
The interesting part here is that i couldn't actually see the bug here since my commands on in the immediate window were affecting the objects. Observing the ClientID caused the ID to be generated, making it seem like the ID should not be nothing at this point in the code. Maybe the underlying problem is that people shouldn't be programming serious apps if they dont understand what is going on under the hood. Note to self: learn more stuff.
2 comments:
It can be usefull to implement INamingContainer in your custom control
It cam be usefull to implement INamingContainer in your custom control
Post a Comment