Dynamically created control disappears after postback.

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
 
M

Mark Rae [MVP]

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top