简体   繁体   中英

SelectedIndexChanged event not firing for 1 list item

I have a radiobuttonlist, a label, and a dropdown as follows:

<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:

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. 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.

My 2 questions are as follows:

1) Why is the event not getting fired on changing from radio3 to radio1?

2) How is the label value getting changed when the event is not firing?

Any help or comments are much appreciated..Thanks in advance!

I am not sure this is a bug or not, but...

When EnableViewState="false" with DDL or RBL and user trying to pick the first list item (index 0), the SelectedIndexChanged will NOT get fired.

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...

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.

this line is your culprit.

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

if you remove the Selected attribute the event should register properly

 <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.

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.

That is, unless you want to go the jQuery route..

I had a similar problem, but it had to do with the "ChildrenAsTriggers" property of the update panel being set to false. All other radio button indexes worked fine this way, except index 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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