Dynamic Controls, Lifecycle, and AJAX
Well, today turned out to be a fun day too :). For some reason, I haven't found the need to dynamically add controls to a page very often - but I certainly did today.
Using web custom controls and server controls built into .NET, this is a lot easier than I had thought it would be. So long as you recreate the control no later than the Page.Load event, it will participate in viewstate and postback events. I had a small hurdle to get around, in that each pageload I needed to get the new value and then replace the control, but no big deal. I originally thought I'd have to manually fetch the data from Request, so this made me happy.
However, dynamically loading User Controls is not quite as fun if you don't know how. Having never done this before at all, I thought I could instantiate the class and be done with it. Oops! I didn't entirely forget that the ascx side of the page is built into a class inheriting from the ascx.cs file, I was just hoping the environment could figure it out. It can, but you need to give some hints - before adding the control, you need to place the <%@ Reference Control = "MyUserControl.ascx" %> directive in the design side, and use the Page.LoadControl("MyUserControl"); method before adding it to a container code-side. I didn't get a chance to play with this too much - the reference I found showed using this method only on the first load, but I'm hoping it needs to be done whenever you want to see the control - I don't want it in the page on every load or I wouldn't be adding it dynamically.
The last bit of fun - and the one I left off on, is AJAX. We have the Teleric r.a.d. controls suite, so I have been making use of their callback controls - having developed callback functionality before, I really appreciate that these work just by drag and drop, no other wiring needed. However, many of my callbacks have begun to misbehave. Some just don't update the appropriate controls anymore, even though I haven't changed those files in a while. Others make the page lifecycle all whacky (I get two PreRender events, then my Click callback trigger event, then another PreRender event). This means that the PreRender wipes out the information I needed to get on callback. This problem is what led to the UserControl bit above - the project I'm working on has many nested UserControls, I'm hoping that if I dynamically load only the needed ones then any conflicts causing the weird callback behavior will be resolved.
Using web custom controls and server controls built into .NET, this is a lot easier than I had thought it would be. So long as you recreate the control no later than the Page.Load event, it will participate in viewstate and postback events. I had a small hurdle to get around, in that each pageload I needed to get the new value and then replace the control, but no big deal. I originally thought I'd have to manually fetch the data from Request, so this made me happy.
However, dynamically loading User Controls is not quite as fun if you don't know how. Having never done this before at all, I thought I could instantiate the class and be done with it. Oops! I didn't entirely forget that the ascx side of the page is built into a class inheriting from the ascx.cs file, I was just hoping the environment could figure it out. It can, but you need to give some hints - before adding the control, you need to place the <%@ Reference Control = "MyUserControl.ascx" %> directive in the design side, and use the Page.LoadControl("MyUserControl"); method before adding it to a container code-side. I didn't get a chance to play with this too much - the reference I found showed using this method only on the first load, but I'm hoping it needs to be done whenever you want to see the control - I don't want it in the page on every load or I wouldn't be adding it dynamically.
The last bit of fun - and the one I left off on, is AJAX. We have the Teleric r.a.d. controls suite, so I have been making use of their callback controls - having developed callback functionality before, I really appreciate that these work just by drag and drop, no other wiring needed. However, many of my callbacks have begun to misbehave. Some just don't update the appropriate controls anymore, even though I haven't changed those files in a while. Others make the page lifecycle all whacky (I get two PreRender events, then my Click callback trigger event, then another PreRender event). This means that the PreRender wipes out the information I needed to get on callback. This problem is what led to the UserControl bit above - the project I'm working on has many nested UserControls, I'm hoping that if I dynamically load only the needed ones then any conflicts causing the weird callback behavior will be resolved.
0 Comments:
Post a Comment
<< Home