Asp – How to dump viewstate for a control while leaving it enabled for future postbacks

asp.net

We have a Telerik Grid (i.e. enhanced version of ASP.Net vanilla grid). On some postbacks we perform major changes to the grid and we don't want load the viewstate for the grid control for that one page load, but we want viewstate to be load to future loads. And in all cases we want to save the viewstate in case the user uses the same data source each time.

It basically goes like this:

  • Page initially loads.
  • Viewstate saved.
  • User manipulates control on page that isn't the change datasource control.
  • Page posts back.
  • Controls created.
  • Viewstate loaded.
  • Events processed.
  • Viewstate saved.
  • User gets back page.
  • User manipulated datasource change control.
  • Page posts back.
  • Controls created.
  • Viewstate loaded (we don't want the grid's viewstate loaded, but it is anyways).
  • Events processed.
  • Viewstate saved (we want all viewstate saved including the new grid's viewstate).
  • User gets back page.

Best Answer

You can derive from the grid control in question and then override the LoadViewState method to not load viewstate when you don't want to.

public class MyGrid : BaseGrid
{
    public bool IsNew { get; set; }

    public override void LoadViewState(object viewState)
    {
        if (!this.IsNew)
        {
            base.LoadViewState(viewState);
        }
    }
}
Related Topic