简体   繁体   English

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

[英]SelectedIndexChanged event not firing for 1 list item

I have a radiobuttonlist, a label, and a dropdown as follows: 我有一个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> 

My rbList_SelectedIndexChanged is as follows: 我的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", "");
     }
}

Now when I change from radio3 to radio2, the event is getting fired as usual and everything looks good. 现在,当我从radio3更改为radio2时,事件会像往常一样被解雇,一切看起来都很好。 But when I change from radio3 to radio1, I don't see the event getting fired (I inserted a breakpoint) the ddl stays visible but the value of lbl changes to 1,2. 但是当我从radio3更改为radio1时,我没有看到事件被触发(我插入了一个断点),ddl保持可见但是lbl的值更改为1,2。

My 2 questions are as follows: 我的两个问题如下:

1) Why is the event not getting fired on changing from radio3 to radio1? 1)为什么从radio3改为radio1时事件没有被解雇?

2) How is the label value getting changed when the event is not firing? 2)当事件未触发时,标签值如何变化?

Any help or comments are much appreciated..Thanks in advance! 任何帮助或评论都非常感谢..谢谢!

I am not sure this is a bug or not, but... 我不确定这是不是一个bug,但......

When EnableViewState="false" with DDL or RBL and user trying to pick the first list item (index 0), the SelectedIndexChanged will NOT get fired. 当EnableViewState =“false”使用DDL或RBL并且用户尝试选择第一个列表项(索引0)时,SelectedIndexChanged将不会被触发。

If you set EnableViewState="true", then the DDL or RBL should work properly when user pick the first list item while non-first item was selected... 如果设置EnableViewState =“true”,那么当用户选择第一个列表项而非第一个项目被选中时,DDL或RBL应该可以正常工作...

Preselecting a radio button in your markup is causing your problems. 预先选择标记中的单选按钮会导致问题。 going from any other option back to option 1 will not trigger the changed event. 从任何其他选项返回到选项1将不会触发更改的事件。

this line is your culprit. 这条线是你的罪魁祸首。

<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>

if you remove the Selected attribute the event should register properly 如果删除Selected属性,则事件应正确注册

 <asp:ListItem> Radio 1 </asp:ListItem>

you could handle the preselection in your code behind. 您可以在后面的代码中处理预选。

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
           rbList.SelectedIndex = 0;
        }
    }

As far as I can tell, programmatically setting SelectedIndex (even on first post) results in the same behaviour as setting Selected="True" on the markup. 据我所知,以编程方式设置SelectedIndex(即使在第一篇文章中)也会产生与在标记上设置Selected =“True”相同的行为。

The only sure-fire way seems to be using an UpdatePanel with the RadioButtonList as an async trigger, making sure the markup changes with every change. 唯一可靠的方法似乎是使用UpdatePanel和RadioButtonList作为异步触发器,确保标记随每次更改而变化。

That is, unless you want to go the jQuery route.. 也就是说,除非你想去jQuery路线..

I had a similar problem, but it had to do with the "ChildrenAsTriggers" property of the update panel being set to false. 我有类似的问题,但它与更新面板的“ChildrenAsTriggers”属性设置为false有关。 All other radio button indexes worked fine this way, except index 0. 除索引0外,所有其他单选按钮索引都以这种方式工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM