Persisting dynamically added usercontrols

OnTheAnvil

Regular
Joined
Nov 4, 2003
Messages
92
Location
Columbus, Ohio, USA
I have a simple usercontrol, uc.ascx. In my page load event I have the following code, C#:

if (! this.IsPostBack)
{
System.Web.UI.Control uc;
uc = LoadControl("uc.ascx");
this.Panel1.Controls.Add(uc);
}

When the page first loads I can see my usercontrol in the panel. When I click a button on the page that simply causes a refresh the page refreshes and the usercontrol vanishes. This is a huge problem because the user enters data on the usercontrol that I need to persist across post backs. I also have to add the usercontrol at runtime because I need to load the same control multiple times depending on some data entered from another page.

In short: can I dynamically place a usercontrol at runtime and have it persist postback events. Any help would be appreciated because I'm completely stuck without this functionality.

~OnTheAnvil
 
No actually I hadn't. One thing that I did try and seems to work, is removing the "if (! this.IsPostBack)". It seems to persist the user information across pages. The problem is that I have to be careful to place each user control on the page in exaclty the same order every single time or else I get errors. I'll try your suggestion later today. Thanks for the response.

~OnTheAnvil
 
That's because you're now loading the control whenever the http request is a GET or a POST, where as before, it was only loaded on http GETs.

I've used this when I had a similar problem as to what you're experiencing.
 
Back
Top