In my ASP.NET 4.0 website, which uses master pages, I’ve disabled viewstate sitewide in web.config:
<pages enableViewState="false" />
and am trying to enable it only when absolutely necessary.
I’ve run into an issue with a DropDownList control (no databinding going on, just hardcoded items):
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="150px" ViewStateMode="Enabled" EnableViewState="True">
<asp:ListItem>Chocolate</asp:ListItem>
<asp:ListItem>Strawberry</asp:ListItem>
<asp:ListItem>Vanilla</asp:ListItem>
</asp:DropDownList>
Even though I’ve enabled view state for this particular control, there’s a problem with selecting the first item:

protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
{
TextBox1.Text = (sender as DropDownList).SelectedValue;
}
The expected result is that whenever “Chocolate” is selected TextBox1 will display “Chocolate”. But what I’m seeing is that TextBox1 only changes when Strawberry or Vanilla is selected. In the example above I selected Strawberry and then Chocolate.
In other words, the DropDownList SelectedIndexChanged isn’t firing when the first item is selected, but is firing when the second or third is selected.
Here are the property settings for the DropDownList:

I tried the same code starting from a blank project and the page works as expected. (Selecting the first item does fire the event).
Thanks in advance for any suggestions.
It appears you can’t set
<pages enableViewState="false" />in the web.config or in any page directives for theViewStateModeproperty to work.Basically
EnableViewState=falsewill override any and allViewStateModesettings.There doesn’t appear to be a way to set the
ViewStateModeproperty in the web.config at this point so it looks like you’ll have to remove anyEnableViewStateproperties from your application and set theViewStateModeproperty toDisabledin all of your page directives.