开发者

Viewstate issue with first dropdownlist item selection

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:

Viewstate issue with first dropdownlist item selection

    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:

Viewstate issue with first dropdownlist item selection

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 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.


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜