Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you do when you can't use ViewState?

I have a rather complex page that dynamically builds user controls inside of a repeater. This repeater must be bound during the Init page event before ViewState is initialized or the dynamically created user controls will not retain their state.

This creates an interesting Catch-22 because the object I bind the repeater to needs to be created on initial page load, and then persisted in memory until the user opts to leave or save.

Because I cannot use ViewState to store this object, yet have it available during Init, I have been forced to store it in Session.

This also has issues, because I have to explicitly null the session value during non postbacks in order to emulate how ViewState works.

There has to be a better way to state management in this scenario. Any ideas?

Edit: Some good suggestions about using LoadViewState, but I'm still having issues with state not being restored when I do that.

Here is somewhat if the page structure

Page --> UserControl --> Repeater --> N amount of UserControls Dynamicly Created.

I put the overridden LoadViewState in the parent UserControl, as it is designed to be completely encapsulated and independent of the page it is on. I am wondering if that is where the problem is.

like image 763
FlySwat Avatar asked Sep 05 '08 03:09

FlySwat


People also ask

How do you use ViewState?

To use the ViewState property, the ASP.NET Web page must have a form element that has the attribute runat="server". To save a value to view state, create a new item that contains the value to save and add the item to the view state dictionary.

Why ViewState is not used in MVC?

ASP.NET MVC does not use ViewState in the traditional sense (that of storing the values of controls in the web page). Rather, the values of the controls are posted to a controller method. Once the controller method has been called, what you do with those values is up to you.

How will you retrieve the value from ViewState?

If you need to store extraneous information in ViewState, you can get and set data in ViewState like you would any other key/value collection: //store value in viewstate ViewState["someValue"] = "Foo"; //retrieve value from viewstate string someValue = ViewState["someValue"]. ToString();

Why do we use ViewState?

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.


3 Answers

The LoadViewState method on the page is definitely the answer. Here's the general idea:

protected override void LoadViewState( object savedState ) {
  var savedStateArray = (object[])savedState;

  // Get repeaterData from view state before the normal view state restoration occurs.
  repeaterData = savedStateArray[ 0 ];

  // Bind your repeater control to repeaterData here.

  // Instruct ASP.NET to perform the normal restoration of view state.
  // This will restore state to your dynamically created controls.
  base.LoadViewState( savedStateArray[ 1 ] );
}

SaveViewState needs to create the savedState array that we are using above:

protected override object SaveViewState() {
  var stateToSave = new List<object> { repeaterData, base.SaveViewState() };
  return stateToSave.ToArray();
}

Don't forget to also bind the repeater in Init or Load using code like this:

if( !IsPostBack ) {
  // Bind your repeater here.
}
like image 158
William Gross Avatar answered Oct 02 '22 09:10

William Gross


This also has issues, because I have to explicitly null the session value during non postbacks in order to emulate how ViewState works.

Why do you have to explicitly null out the value (aside from memory management, etc)? Is it not an option to check Page.IsPostback, and either do something with the Session variable or not?

like image 33
Greg Hurlman Avatar answered Oct 02 '22 07:10

Greg Hurlman


I have always recreated my dynamic controls in the LoadViewState event. You can store the number of controls needed to be created in the viewstate and then dynamically create that many of them using the LoadControl method inside the LoadViewState event. In this event you have access to the ViewState but it has not been restored to the controls on the page yet.

like image 27
DancesWithBamboo Avatar answered Oct 02 '22 09:10

DancesWithBamboo