Problems with ViewState: "function 'ViewState.get_Item' evaluated and returned null"

Discussion in 'ASP .Net' started by Steph, Apr 27, 2004.

  1. Steph

    Steph Guest

    I am trying to save a simple integer value in ViewState. I set the
    value in the Page_Load() event and retrieve it in the function
    Page_Unload() event. The value seems to be set correctly, but upon
    the next Page_Load(), I get the following error when I try to retrieve
    the value from ViewState:

    "function 'ViewState.get_Item' evaluated and returned null".

    The page does have a form on it with the runat="server" attribute set.
    From the MSDN documentation, this seems to be all you have to do.

    What am I doing wrong?

    I have created a simple test page that illustrates the problem. Here
    is the code behind the page:

    public class Testing : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button Button1;
    protected int modPageMode;
    private void Page_Load(object sender, System.EventArgs e)
    {
    Page.EnableViewState = true;

    // Initial page load
    if (!Page.IsPostBack)
    {
    modPageMode = 1;
    }
    // Post back
    else
    {
    // Set the page mode
    modPageMode = (int)ViewState["PageMode"];
    }
    }
    private void Page_Unload(object sender, System.EventArgs e)
    {
    ViewState["PageMode"] = modPageMode.ToString();
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
    modPageMode = 2;
    }
    }
     
    Steph, Apr 27, 2004
    #1
    1. Advertising

  2. Steph

    bruce barker Guest

    updating viewstate in Unload is not very useful. the page has been rendered,
    so the client will not see the update, nor send it back.

    PreRender is your last chance to update Viewstate.

    -- bruce (sqlwork.com)



    "Steph" <> wrote in message
    news:...
    > I am trying to save a simple integer value in ViewState. I set the
    > value in the Page_Load() event and retrieve it in the function
    > Page_Unload() event. The value seems to be set correctly, but upon
    > the next Page_Load(), I get the following error when I try to retrieve
    > the value from ViewState:
    >
    > "function 'ViewState.get_Item' evaluated and returned null".
    >
    > The page does have a form on it with the runat="server" attribute set.
    > From the MSDN documentation, this seems to be all you have to do.
    >
    > What am I doing wrong?
    >
    > I have created a simple test page that illustrates the problem. Here
    > is the code behind the page:
    >
    > public class Testing : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Button Button1;
    > protected int modPageMode;
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > Page.EnableViewState = true;
    >
    > // Initial page load
    > if (!Page.IsPostBack)
    > {
    > modPageMode = 1;
    > }
    > // Post back
    > else
    > {
    > // Set the page mode
    > modPageMode = (int)ViewState["PageMode"];
    > }
    > }
    > private void Page_Unload(object sender, System.EventArgs e)
    > {
    > ViewState["PageMode"] = modPageMode.ToString();
    > }
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > modPageMode = 2;
    > }
    > }
     
    bruce barker, Apr 27, 2004
    #2
    1. Advertising

  3. Steph

    heislord5

    Joined:
    May 11, 2011
    Messages:
    1
    Answer

    Save the ViewState in an earlier event.

    Then in Page_Unload, call

    this.SaveViewState()

    That will update the values while in the page_unload event.
     
    heislord5, May 11, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Rush
    Replies:
    2
    Views:
    573
    Ben Rush
    Dec 5, 2003
  2. ujjc001
    Replies:
    0
    Views:
    1,398
    ujjc001
    Jul 27, 2005
  3. sck10
    Replies:
    6
    Views:
    957
    sck10
    Sep 1, 2006
  4. mark

    Viewstate errors... how do I get viewstate working?

    mark, Feb 20, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    299
  5. Josema
    Replies:
    2
    Views:
    387
Loading...

Share This Page