views:

174

answers:

4

I have a problem that a DropDownList is losing its SelectedValue after a PostBack on one server on the web farm. On the other servers, everything is working correctly.

All servers are on the same version of code with the same service packs and all updates applied. The code also works correctly when I run on my local machine (but point at the production database).

I thought ViewState might be the problem, but I confirmed that the web.config, the aspx page, and code are the same across all machines.

I did receive a Input string was not in a correct format. error when trying to Convert.ToInt32().

I added Trace.Write to figure out that the DropDownList value is null on only this particular server.

Code Snippet

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostBack)
    {
        LoadClientDropDown();
    }
}

protected void LoadClientDropDown()
{
    //load the drop down ddlClients from data
}

protected void btnSave_Click(object sender, System.EventArgs e)
{
    int clientValue = Convert.ToInt32(ddlClients.SelectedValue);
}

What could I be missing?

A: 

It looks like you're populating the control only if there's no postback, so what happens here is that on post you get your control with no items and obviously no value.

I'd sugest to load it with those items at the point you're actually creating that control.

Shouldn't the viewstate hold these values across postbacks?
Yes it should, I forgot that you're using it.
A: 

Short of further info I'd suspect the ViewState. If the value submitted from a dropdown control is not amongst the ones that are in it's Items list (As populated from the ViewState) then it will set that value to null.

Take a look at "Request.Form[ddlClients.UniqueID]" on the form submit and see if it has a value. If that has a value then .NET is rejecting the value that's been posted back. In that case then the possibilities are either that the viewstate data is not being preserved or some code is modifying the contents of the dropdown list after it's Init but before the event handling.

Hopefully one of these suggestions helps you find your way to the problem.

Tim Schneider
+1  A: 

Has the application been configured to run on a web farm? Check out this MSDN article:

http://msdn.microsoft.com/en-us/library/ms998288.aspx#paght000007_viewstate

You need to ensure that the machineKey is consistent across each server your app runs on. You can define this in web.config. I remember a previous employer that hadn't done this ran into viewstate issues.

spooner
+1  A: 

As well as the machine key mentioned above, how is your web farm load balanced?

I've previously run into issues where information is being received form one server and posted back to another.

Tim Goody