3

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:

enter image description here

    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:

enter image description here

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.

Adam Kane
  • 3,996
  • 7
  • 44
  • 53
  • SO if you select the second item. The event is fired. If you then select the first item, the event is not fired? – Phill Jun 09 '11 at 02:01
  • @Phil: Yes, the SelectedIndexChanged event fires when the second or third items are selected. But after that, selecting the first item doesn't cause the event to fire. – Adam Kane Jun 09 '11 at 02:21

2 Answers2

1

It appears you can't set <pages enableViewState="false" /> in the web.config or in any page directives for the ViewStateMode property to work.

Basically EnableViewState=false will override any and all ViewStateMode settings.

There doesn't appear to be a way to set the ViewStateMode property in the web.config at this point so it looks like you'll have to remove any EnableViewState properties from your application and set the ViewStateMode property to Disabled in all of your page directives.

Aaron Carlson
  • 5,522
  • 4
  • 31
  • 35
0

I guess I have know what you say. You want show default value from dropdownlist when page load. If you want so you can do like this when dropdownlist load.

protected void DropDownList1_Load(object sender, EventArgs e)
    {
        TextBox1.Text = (sender as DropDownList).Text;
    }
Justin
  • 676
  • 1
  • 8
  • 24