Dynamic Controls, Postbacks, ViewState, Oh My!

  • Thread starter Thread starter Gummy
  • Start date Start date
G

Gummy

Hello,



I am loading several user controls dynamically in OnInit() like this:



ucListBoxSelections ucLocation =
(ucListBoxSelections)LoadControl("UserControls/ucListBoxSelections.ascx");

ucListBoxSelections ucDept =
(ucListBoxSelections)LoadControl("UserControls/ucListBoxSelections.ascx");

ucListBoxSelections ucBusiness =
(ucListBoxSelections)LoadControl("UserControls/ucListBoxSelections.ascx");

PlaceHolder1.Controls.Add(ucLocation);

PlaceHolder1.Controls.Add(ucDept);

PlaceHolder1.Controls.Add(ucBusiness);



Then, in the Page_Load I thought I should be able to access these Controls
and reload (or assign for the first time) its data through the ViewState
(that has not been lost because it was loaded in OnInit().



Am I doing this correctly because I cannot "find" these Controls in
Page_Load?



Also, each time I click on any one of these User Controls there is a
postback, as expected, but all the listboxes (in each of the user controls)
still seems slow to load. I assume that is because they are reloading. The
idea, I thought, was that it was faster to load these controls in OnInit.



I think I am close, does anyone have some suggestions?



Thank you.



p.s. There are several user controls that get loaded each time. However,
they do not have to be dynamic because they will always appear on the page.
It's just their data in their listboxes will change. Is there a better way
to approach this?
 
Here is a starter article to read:
http://msdn2.microsoft.com/en-us/library/ms178472.aspx

This article describes the ASP.NET Page LifeCycle. It describes how
everything is built when you request an asp.net page and in what order.

IIRC, You should be able to access the controls in the Page_Load
method, at this point OnLoad is called and postback data is loaded into
the controls. So you should be able to.

I apologize for the vagueness of the post, its been a little while
since I've played with dynamic controls so my mind is a little fuzzy.

Take a look at that article though, that should point you in the right
direction.
 
Back
Top