C# – Even if DropDownList has its viestate disabled, SelectedValue should still return a value

asp.netcviewstate

I think I understand ViewState pretty well, but the following is giving me some troubles:

From http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx

Postback controls such as dropdownlist and textbox restore their posted state (the selected item of a dropdown ist 'posted') even when ViewState is disabled, because even with ViewState disabled the control is still able to post its value

Assuming DropDownList has EnableViewState set to false, then ( according to the above quote ) when user issues a postback by selecting an item in DropDownList, the following code should result in Label1.Text displaying a value of a selected item ( thus DropDownList.SelectedValue should return a value selected by user, even if viewstate is disabled ), but instead I get an empty string:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] number = {"first","second","third"};
        DropDownList1.DataSource = number;
        this.DataBind();

    }
    if (IsPostBack)
    {
       Label1.Text = DropDownList1.SelectedValue; // displays empty string
       // Label1.Text = DropDownList1.SelectedItem.Text; // causes an exception           
       // Label1.Text = DropDownList1.SelectedIndex.ToString(); // displays empty string
    }
}

The author of that article appears to be an expert on the subject, so I'm assuming I'm doing something wrong !?!

thanx

Best Answer

There is no selected value because:

1) there are no items in the drop down list, because you are only binding the data to it the first time around. Normally this is good practice, but when you have ViewState turned off, you must explicitly re-create the data for the control every time.

2) You are binding the data after the point in the page lifecycle where the Request dictionary values are applied to controls (namely, during the restoration of ViewState to controls.) The posted value exists in the Request dictionary, but since there are no items in the dropdownlist, it can't really do much with it. Even though ViewState is turned off, your author is correct - the posted value will be applied, at the point in the lifecycle where ViewState would normally be applied.

If you were to re-create the data for the list in Init(), then the dropdown would be populated for the posted value to be applied to, it would be applied, and the selected value would be available by the time you got to Load(). Hopefully that's clear. Some working code is below:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Label1.Text = DropDownList1.SelectedValue;
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        string[] number = { "first", "second", "third" };
        DropDownList1.DataSource = number;
        DropDownList1.DataBind();
    }
}
Related Topic