c# - SelectedIndexChanged 事件未针对 1 个列表项触发

标签 c# asp.net radiobuttonlist selectedindexchanged

我有一个单选按钮列表、一个标签和一个下拉列表,如下所示:

<asp:RadioButtonList id="rbList" runat="server" AutoPostBack="true" EnableViewState="false" 
            OnSelectedIndexChanged="rbList_SelectedIndexChanged" 
RepeatLayout="Table" RepeatDirection="Horizontal" RepeatColumns="3">
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
            <asp:ListItem> Radio 2 </asp:ListItem>
            <asp:ListItem> Radio 3 </asp:ListItem>
            </asp:RadioButtonList>

<asp:Label runat="server" ID="lbl" text="1,2" EnableViewState="false"></asp:Label>

<asp:DropDownList runat="server" ID="ddl" Visible="false">                    
            </asp:DropDownList> 

我的 rbList_SelectedIndexChanged 如下:

protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (rbList.SelectedIndex == 0 | rbList.SelectedIndex==1)
    {
        lbl.Text = "1,2";
        ddl.Visible = false;
        //ddl.Attributes.Add("style", "display:none");
    }
    else if (rbList.SelectedIndex == 2)
    {
        lbl.Text = "3";
        ddl.Visible = true;
        //ddl.Attributes.Add("style", "");
     }
}

现在,当我从 radio3 更改为 radio2 时,事件像往常一样被触发,一切看起来都很好。但是当我从 radio3 更改为 radio1 时,我没有看到事件被触发(我插入了一个断点)ddl 保持可见但 lbl 的值变为 1,2。

我的2个问题如下:

1) 为什么在从 radio3 更改为 radio1 时没有触发事件?

2) 当事件没有触发时,标签值是如何改变的?

非常感谢任何帮助或评论..提前致谢!

最佳答案

我不确定这是不是一个错误,但是...

当使用 DDL 或 RBL 的 EnableViewState="false"并且用户尝试选择第一个列表项(索引 0)时,不会触发 SelectedIndexChanged。

如果您设置 EnableViewState="true",那么当用户选择第一个列表项而未选择第一个项目时,DDL 或 RBL 应该可以正常工作...

关于c# - SelectedIndexChanged 事件未针对 1 个列表项触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16823017/

相关文章:

asp.net - 如何从 HTML 5 多文件输入元素捕获上传的文件

c# - 在 Windows Azure 中部署 Unity3D

android - 如何更改单选按钮 ListView 的字体大小?

c# - Xamarin.Mac - 发送通知

c# - GPU 内核中的指针和位运算符

c# - 我应该为我的 GUI 应用程序使用异步 "all the way"吗?

ios - Swift 单选按钮不可点击

c# - Xamarin.iOS 和 Xamarin.Android 的 NuGet 包缩写是什么?

asp.net - 在没有提示的情况下从 asp.net 页面下载 .CSV/.TXT

html - 将单选按钮与包含元素组合在一起?