我有一个单选按钮列表、一个标签和一个下拉列表,如下所示:
<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/