3

I'm trying to understand the order of events here.

This page tells me that at some point in the page lifecycle that the ViewState is mapped to the page session.

I am assuming that happens in the control.SaveViewState() method.

Can anyone tells me when this happens?

I'm concerned because I am seeing some code that reads ViewState variables in Page_Load, and I think it is still returning info even after the session is cleared. (causing issues with a timeout redirection we have set up)

Community
  • 1
  • 1
KevinDeus
  • 11,988
  • 20
  • 65
  • 97
  • 2
    Old article, but explains ViewState, PostBacks...etc better than anything I have ever found: Understanding ASP.NET View State - http://msdn.microsoft.com/en-us/library/ms972976.aspx – rick schott Apr 12 '12 at 18:30

2 Answers2

3

ViewState is restored between Init and Load1 and saved after PreRender. Thus it is valid to use in Load through PreRender.

However, ViewState != Session. ViewState is tied with the page form and not the session as it lives in the hidden __VIEWSTATE field2. Thus, unless it is otherwise "cleared" when a session timeout is detected it will still post the client-side stored values (which might be hours, or days, old ;-) on the next post-back.


1 It is actually okay to access ViewState in PreLoad, which is available to a Page but not general Controls. LoadViewState/TrackViewState is done after the page Init by default. (And the Init of a Page is done after the Init of all Controls currently in the hierarchy.)

2 I believe using the form is the only correct way of handling ViewState. However, it can be stored/loaded using a different backing (e.g. server side).

  • 1
    +1. Also, see the Page Lifecycle documentaiton for a more detailed explanation. Every ASP.NET WebForms developer needs to know this anyway. http://msdn.microsoft.com/en-us/library/ms178472.aspx – David Apr 12 '12 at 18:16
  • ok, so if a page session times out, will the viewstate variable remain? I'm thinking yes in a lot of cases. – KevinDeus Apr 12 '12 at 18:16
  • @KevinDeus Generally, yes. One can implement a custom method to save/restore view state (e.g. persist on server) but ... ick, and that's not default. –  Apr 12 '12 at 18:16
  • @pst, ok, cool. not looking to reconstitute the ViewState, just trying to track why variables remain. I was worried that if you cleared the session, since ViewState is stored on the page, it would remap to new sesssion variables after it was cleared. – KevinDeus Apr 12 '12 at 18:20
  • @KevinDeus No, ViewState has nothing to do with session stuff. It is only enough information to get "remap"ed to the materialized Control tree. It is so general that it can even occasionally map wrong values to an *incorrect* tree at times ... but unless there are dynamic controls, no worries ;-) –  Apr 12 '12 at 18:22
  • 2
    Also, don't confuse ViewState with the IPostBackDataHandler. http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackdatahandler.aspx – rick schott Apr 12 '12 at 18:22
2

First, Initialize the ViewState in your Page Load. Set the BreakPoints on each page Events as mentioned below.

Now start debug on a button click at each event.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        ViewState["abc"] = "10";
    }
}

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
}

protected override void OnInitComplete(EventArgs e)
{
    base.OnInitComplete(e);
}

protected override void OnPreLoad(EventArgs e)
{
    base.OnPreLoad(e);
}

OnPreLoad will start to give you the ViewState persisted value. Before this event like Init/InitComplete/PreInit will not give any ViewState Value.

When a postback occurs, SaveViewState() will capture the new value and LoadViewState() will repopulate it after the postback.

Why are you comparing Session with ViewState?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • because I am seeing that sometimes ViewState variables persist after Session timeout. I havent narrowed down why this is happening – KevinDeus Apr 12 '12 at 18:26
  • I hope that this sentence is cleared already 'ViewState will Persist across Postback and Session will not impact ViewState despite of the fact that it is timeout'. In case the Session TimesOut, It will redirect user to some logic page. – Pankaj Apr 12 '12 at 18:34