Dynamic Loaded User Control

In my project OnlineVote, I created web user controls. Then in an aspx file, I have a panel(or placeholder) to dynamically load user controls which are generated in an array.

I failed many times in the beginning then I did research and found out that the dynamic loading has to be done in page_init or page_load, otherwise those controls can't show up.

So I redesign my way to load the controls based on user's command click event, and place that part into page_load, under both !IsPostback and Postback section.

Like:

page_Load()
{
If (!Ispostback)
{
...
define and loading control
...
}
else
{
...
define and loading control
...
}
}

In this way, the controls are created within the page _loading process.

However, I still can't see controls coming up if I click some button.
That takes me another day to find out that in my user control I have databinding in !IsPostBack part. So that prevent data binding if I click the button.

After removing it, the whole page works as expected.

In summary:
1) dynamically loading controls must make sure it works in page_load or page_init
and it must repeatedly declared for each request, that means must be in postback or not
2) Check web user control for databinding if this control will be dynamically loaded the data bind must happens for each request.

Comments

Popular Posts