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;
}
}