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?
Vansfannel
source share