Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewstate of ascx lost between postbacks

In my ASP.NET application, I am loading an .ascx dynamically using LoadControl, using the following pattern:

var ctrl = LoadControl("/path/to/control.ascx");
((ControlType)ctrl).SomeProperty = someData;
placeholder.Controls.Add(ctrl);

The control that I add saves the SomeProperty property value directly to ViewState, as follows:

public int? SomeProperty
{
    get { return (int?)ViewState["SomeProperty"]; }
    set { ViewState["SomeProperty"] = value; }
}

After that, the ascx control lives a life on its own and all is well until postback occurs. When the page posts back, suddenly the view state is empty! I suspect this happens because I manipulate the ViewState before I add the instantiated ascx to my page. Also, I can prevent the ViewState from getting lost by adding the following line in the Page_Load() method of my ascx control:

SomeProperty = SomeProperty;

I have to do the above for each and every property to ensure that the ViewState is preserved. Now, is there a prettier way of doing this? Manipulating the ViewState after the instantiated .ascx has been added to the page is not an option - I need the contents of the ViewState in the Page_Init() and Page_Load() methods, which are triggered the instant I add the .ascx to my page.

Thanks.

like image 482
Ulrik Rasmussen Avatar asked Nov 20 '09 13:11

Ulrik Rasmussen


People also ask

Can ViewState be accessed in another page?

You can't access ViewState of one page from another page directly. If you want to access a particular ViewState value then you can pass the value in Context collection and then access the value in other page.

Where is ViewState information stored?

By default, view state data is stored in the page in a hidden field and is encoded using base64 encoding. In addition, a hash of the view state data is created from the data by using a machine authentication code (MAC) key.

How to maintain View State in asp net?

The view state is the state of the page and all its controls. It is automatically maintained across posts by the ASP.NET framework. When a page is sent back to the client, the changes in the properties of the page and its controls are determined, and stored in the value of a hidden input field named _VIEWSTATE.


3 Answers

Take a look at the ASP.NET Page Life Cycle and Understanding View State. View State gets loaded after Initialization, so you won't be able to access it in Page_Init. You'd be better off using a hidden field.

If you are dead set on using View State, the earliest you can get to it would be by overriding the LoadViewState method (Remember to call base.LoadViewState before trying to access it though).

like image 93
Bob Avatar answered Sep 23 '22 09:09

Bob


You also need to add the control to the controls collection BEFORE you set the property. ViewState does not get recorded until after it is added to the controls collection.

placeholder.Controls.Add(ctrl); 
((ControlType)ctrl).SomeProperty = someData; 
like image 23
Jon444 Avatar answered Sep 21 '22 09:09

Jon444


Keep track of the ID of the UserControl before postback then on postback re-create the control and assign the ID back and it should automatically load the ViewState back in.

like image 41
James Avatar answered Sep 21 '22 09:09

James