Dynamically created control disappears after postback.

  • Thread starter Thread starter Sami Rehman
  • Start date Start date
S

Sami Rehman

Hi,
I am creating some web user controls dynamically Using
LoadControl("ABC.ascx")
My user control contains 2 drop down list, 2nd one is loaded based on the
selection in the 1st one.

However whenever I make the selection in the 1st one, the control disappears
after the post back. I tried setting the EnableViewState=true but even that
did not help..
UCTemp ucTemp = (UCTemp)LoadControl("UCTemp.ascx");
ucTemp.EnableViewState = true;
plhInput.Controls.Add(ucTemp);

Does any one know why this is happening?

-Sami
 
Does any one know why this is happening?

Dynamically created controls do not survive a postback - they need to be
recreated each time the page loads, irrespective of how it loads.

In addition, if they need to be wired up to events, they need to be created
before the Page_Load event e.g. in the Page_Init event...
 
Back
Top