Viewstate variable lost during user control is loaded dynamically

I have a problem with ViewState. I have an aspx page with a tree on the left and an UpdatePanel with an ASP.NET panel inside on the right. It is in this internal panel that I load and unload dynamically user controls. I use this update panel to dynamically control the controls.

I also created a custom control for my custom controls because I need to pass some values ​​from the page. In this constructor, I use ViewState to store these values.

The first time a user control is loaded, I invoke its constructor with parameters. When I reload this user control with each postback, I use its regular constructor.

My problem is that the values ​​that I saved on the ViewState became null during the subsequent postback.

Update:

This is part of my user control:

public class MyUserControl : System.Web.UI.UserControl { private int PKId { get { return ViewState["pkId"] as int; } set { ViewState["pkId"] = value; } } public MyUserControl(int pkId) { this.PKId = pkId; } ... } 

I follow this article to dynamically load controls: http://msdn.microsoft.com/en-us/magazine/cc748662.aspx#id0070065 .

Second update:
I also set the same control identifier when I load a user control for the first time and on every load.

Maybe I can use another method to store these values, such as entering hidden fields or cache. I chose ViewState because I don't want to overload the server with Session values ​​for each user.

Third update:

 I load the controls with this code: System.Web.UI.UserControl baseControl = LoadControl(ucUrl) as System.Web.UI.UserControl; if (baseControl != null) { baseControl.ID = "DestinationUserControl"; PanelDestination.Controls.Add(baseControl); } 

And the actual download with this code:

 DynamicControls.CreateDestination ud = this.LoadControl(TrackedUserControl) as DynamicControls.CreateDestination; if (ud != null) { ud.ID = "DestinationUserControl"; PanelDestination.Controls.Add(ud); } 

What's happening?

+6
c # user-controls
source share
4 answers

Try storing the control in a local variable after loading / assembling it before adding it to the control hierarchy. This allows you to display ViewState data from and to the control. See "Rule 2" here http://chiragrdarji.wordpress.com/2009/05/20/maintain-viewstate-for-dynamic-controls-across-the-postback/ .

+3
source share

When do you load a user control? This should happen in the Init event if you want to save / restore the ViewState.

+3
source share

Adding dynamic controls to UpdatePanel is such a bad idea. This causes so many problems.

If possible, move the dynamic control creation from UpdatePanel, and I believe that your problem will be solved.

0
source share

As mentioned by Bryan , you should load your dynamic controls into Page_Init, not Page_Load.

How this description of the page life cycle explains that by the time the Page_Load event occurred, the state of the view from the postback was already being processed (in PreLoad). If the controls have not yet been reloaded, the view state is nowhere.

PreLoad:

Use this event if you need to perform processing on your page or control before the Load event.

Before the page instance, this event occurs, loads the view state for itself and all the controls , and then processes any postback data included in the Instance Request.

0
source share

All Articles