Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
User Controls When to set the value of controls
Message
 
To
28/07/2009 08:53:06
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
ASP.NET
OS:
Windows XP SP2
Application:
Web
Miscellaneous
Thread ID:
01414924
Message ID:
01415030
Views:
36
>The book I have indicates the PreRender is definately the place but as you say it doesn't work there. I moved my code to the page load and it works just fine there. But if the PreRender is too late, why are the controls still null?

I had to break out the event diagram ;-) I thought ViewState was already generated at this point, but that happens right after PreRender, so you're right, this should be safe. You have to reload controls during each postback - are you doing that?

>This is a surprise to hear. I did get this working just fine, but I was sure under the impression that common wisdom indicates using seperate controls was a good thing for keeping a page de-cluttered and for maintenance. For now, I have them working but guess I will do enough testing to make sure there isn't a problem. They are in an ajax panel so they only refresh this portion. The problem is there are two different controls for different situations. It is a bit of a mess to keep all that junk in the page and deal with hiding and unhiding these controls.



>
>I did get them working in the place holder but I had to change my code from this:
>
>ctrlMyControl myControl = new ctrlMyControl();
>this.placeholder1.Controls.Add(myControl);
>
>to this:
><pre>
>ctrlMyControl myControl = (ctrlMyControl)Page.LoadControl("~/UserControls/ctrlMyControl.ascx");
>this.placeholder1.Controls.Add(myControl);
>

The biggest issue is that you need to reload the controls on each postback (I do this in Page_Load). If your page can change controls during postback, you still need to reload the original control then the new one. If you don't you'll run into ViewState errors (assuming it's enabled). Eventing for these controls can also get weird. Your code looks OK - I took a quick look at some code I've got for this and the only thing it looks like I'm doing extra (well, besides all the code I've got for determining which control to reload) is setting the PlaceHolder controls Visible property to true.
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform