asp.net - 第一个下拉列表项选择的 View 状态问题

标签 asp.net drop-down-menu viewstate selectedindexchanged

在我使用母版页的 ASP.NET 4.0 网站中,我在 web.config 中禁用了站点范围内的 View 状态:

<pages enableViewState="false" />

并且仅在绝对必要时才尝试启用它。

我遇到了 DropDownList 控件的问题(没有数据绑定(bind),只是硬编码的项目):
    <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>

即使我为这个特定的控件启用了 View 状态,选择第一个项目还是有问题:

enter image description here
    protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)
    {
        TextBox1.Text = (sender as DropDownList).SelectedValue;
    }

预期的结果是,无论何时选择“巧克力”,TextBox1 都会显示“巧克力”。但我看到的是 TextBox1 只有在选择草莓或 Vanilla 时才会改变。在上面的示例中,我选择了草莓,然后选择了巧克力。

换句话说,DropDownList SelectedIndexChanged 不会在选择第一个项目时触发,而是在选择第二个或第三个项目时触发。

以下是 DropDownList 的属性设置:

enter image description here

我从一个空白项目开始尝试相同的代码,页面按预期工作。 (选择第一项确实会触发该事件)。

在此先感谢您的任何建议。

最佳答案

看来你不能设置<pages enableViewState="false" />在 web.config 或 ViewStateMode 的任何页面指令中属性(property)工作。

基本上 EnableViewState=false将覆盖所有 ViewStateMode设置。

似乎没有办法设置 ViewStateMode此时 web.config 中的属性,因此看起来您必须删除任何 EnableViewState应用程序中的属性并设置 ViewStateMode属性(property)给 Disabled在您的所有页面指令中。

关于asp.net - 第一个下拉列表项选择的 View 状态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6287256/

相关文章:

c# - 未将对象引用设置为行 if (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text)) 处的对象实例

javascript - 如何在点击时正确更改 react-bootstrap DropdownButton 的标题?

c# - 如何存储 ASP.NET CommandName 和 CommandArgument 值?

c# - Asp.net 无效的 View 状态 - 也许 IE8 4k 丢失了错误?

c# - .HasRequired 无法在模型中工作

c# - 如何在 asp.net 表单例份验证中启用多次登录尝试?

PHP mysql代码帮助解码

html - IE7 中的偏移下拉菜单

jquery - 从下拉框中打开一个 fancybox 并将所选值直接传递给它

c# - Base-64 字符数组的长度无效