views:

156

answers:

1

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

The code for the page load initialises the ViewState to on:

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

I then check the button's state 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 swtich between states of button, I capture the clicking of the button and toggle the value in the ViewState:

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

On the development box, this works perfectly. However, on the live box, the page loads correctly but when you click the button (or indeed any other button that causes a postback), you get the error after the postback.

Can anyone help?

+1  A: 

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

Martin Brown
That was the problem. The site was placed within a Microsoft Dynamics CRM web site. I hadn't realised that CRM disabled ViewState.
Baffled by ASP.NET