Can't get ViewState to work on production server

I have an ASP.NET application developed in my Vista panel (IIS7). It works fine until I deploy it to a product server (W2K3 / IIS6). Upon deployment, I get a consistent "object reference not set to an object instance". when reading from my ViewState object that determines whether the button displays the image "On" or "Off".

The page load code initializes the ViewState to include:

if (!IsPostBack)
{
    ViewState["ButtonState"] = true;
}

      

Then I check the state of the button in the OnPreRender method:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    if ((bool)ViewState["ButtonState"])
    {
        MyButton.ImageUrl = Constants.ButtonIcon;
    }
    else
    {
        MyButton.ImageUrl = Constants.NoButtonIcon;
    }
}

      

To switch between the states of a button, I capture the click of the button and toggle the value in the ViewState:

protected void MyButton_Click(object sender, ImageClickEventArgs e)
{
    ViewState["ButtonState"] = !(bool)ViewState["ButtonState"];
}

      

In development window this works fine. However, in real time the page loads correctly, but when you press a button (or even any other button that triggers a postback) you get an error message after the postback.

Can anyone please help?

0


source to share


1 answer


Do you have <system.web> <pages enableViewState = "false" / "> set in the server's web.config or machine.config files?



+2


source







All Articles