![](/img/trans.png)
[英]Item in RadioButtonList not firing SelectedIndexChanged
[英]SelectedIndexChanged event not firing for 1 list item
我有一个radiobuttonlist,一个标签和一个下拉列表,如下所示:
<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。
我的两个问题如下:
1)为什么从radio3改为radio1时事件没有被解雇?
2)当事件未触发时,标签值如何变化?
任何帮助或评论都非常感谢..谢谢!
我不确定这是不是一个bug,但......
当EnableViewState =“false”使用DDL或RBL并且用户尝试选择第一个列表项(索引0)时,SelectedIndexChanged将不会被触发。
如果设置EnableViewState =“true”,那么当用户选择第一个列表项而非第一个项目被选中时,DDL或RBL应该可以正常工作...
预先选择标记中的单选按钮会导致问题。 从任何其他选项返回到选项1将不会触发更改的事件。
这条线是你的罪魁祸首。
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
如果删除Selected属性,则事件应正确注册
<asp:ListItem> Radio 1 </asp:ListItem>
您可以在后面的代码中处理预选。
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
rbList.SelectedIndex = 0;
}
}
据我所知,以编程方式设置SelectedIndex(即使在第一篇文章中)也会产生与在标记上设置Selected =“True”相同的行为。
唯一可靠的方法似乎是使用UpdatePanel和RadioButtonList作为异步触发器,确保标记随每次更改而变化。
也就是说,除非你想去jQuery路线..
我有类似的问题,但它与更新面板的“ChildrenAsTriggers”属性设置为false有关。 除索引0外,所有其他单选按钮索引都以这种方式工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.