Ozkan
Ozkan

Reputation: 4160

viewstate variable is always available?

I was thinking about if a custom made ViewState variable is always available on each page visit (same browser session) (like Session but client-side) or only at a page postback? I know this is client-side data that is always encapsulated with the Request packet and the Response packet from the server.

I was testing this right now, and I did the following:

on Home.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    ViewState["test"] = "test1";
}
protected void Button1_Click(object sender, EventArgs e)
{
    string test = ViewState["test"].ToString();

    Server.Transfer("Default.aspx");
}

And on Default.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    string test = ViewState["test"].ToString();
}

But I get NullReferenceException. So it means that ViewState["test"] doesn't exists because the ViewState is completely new and regenerated. So my conclusion is that you can use ViewState variables only when doing a form postback (but in fact, you do always a redirect after a form postback, so I can't use ViewState always...).

Am I right with my opinion?

Upvotes: 0

Views: 5589

Answers (4)

Damith
Damith

Reputation: 63065

If you use HttpServerUtility.Transfer Method (String, Boolean) with url and true parameter, then it will preserve the QueryString and Form collections. But still you can't access ViewState from 2nd page directly. Here is a way to access view state.

Home.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    ViewState["test"] = "test1";
}

protected void Button1_Click(object sender, EventArgs e)
{
    Server.Transfer("Default.aspx", true);
}

// adding this method to return view state

public StateBag ReturnViewState()
{
    return ViewState;
}

Default.aspx: here using PreviousPage we can invoke method on that page and get the view state

private StateBag PreviousPageViewState
{
    get
    {
        StateBag returnValue = null;
        if (PreviousPage != null)
        {
            Object objPreviousPage = (Object)PreviousPage;
            MethodInfo objMethod = objPreviousPage.GetType().GetMethod
                    ("ReturnViewState");
            return (StateBag)objMethod.Invoke(objPreviousPage, null);
        }
        return returnValue;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (PreviousPage != null)
    {
        if (PreviousPageViewState != null)
        {
            var text = PreviousPageViewState["test"].ToString();
        }
    }
}

Upvotes: 1

Jeff Watkins
Jeff Watkins

Reputation: 6359

Use Page_PreRender, Page_Load is before the viewstate is in a usable state IIRC.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062660

ViewState can be configured in different ways, with different (or custom) providers, or can be completely disabled. However, the default provider is that it is a form-field, so yes: in that default configuration case it will only exist on a POST, and will not exist for a GET. A transfer operates essentially like a GET.

If you need data between unrelated pages, but user-related - use session-state, or something cookie-based.

In unrelated news: view-state is pretty horrible in many ways - think of the kittens!

Upvotes: 3

m.edmondson
m.edmondson

Reputation: 30872

What you've done here is stumble across one of the fundamental points of understanding within ASP.NET Web Forms and how it abstracts the stateless nature of HTTP away from you.

The difference between Session variables and Viewstate is simple:

  • Session is stored server-side, and it produced when the browser returns a recognised cookie
  • Viewstate is stored completely client-side within hidden <input> tags, meaning they are submitted fresh on every postback

As you have experienced Session can live accross pages, whereas Viewstate is only available when posting to the same page.

Upvotes: 1

Related Questions