Selected Index Changed Event Firing when it's not supposed to

时间:2015-07-13 21:00:20

标签: c# asp.net

I have just encountered a strange situation with an ASP.NET page. My form has 2 controls that have AutoPostBack="true": a RadioButtonList and a DropdownList. The RadioButtonList also has EnableViewState="False". When I change the selected item of the dropdownlist, the RadioButtonList SelectedIndexChanged event fires as well. If I remove EnableViewState="False", then this behavior goes away. Can anyone explain why this is happening? I have included sample code below so you can see this behavior for yourself:

<div>                
    <asp:RadioButtonList ID="rblTest" AutoPostBack="true" OnSelectedIndexChanged="rblTest_SelectedIndexChanged" EnableViewState="false" runat="server">            
        <asp:ListItem Text="Option 1" Value="1" />
        <asp:ListItem Text="Option 2" Value="2" />
        <asp:ListItem Text="Option 3" Value="3" />
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:DropDownList ID="ddlTest" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" runat="server">
        <asp:ListItem Text="Select One" Value="" />
        <asp:ListItem Text="Option 1" Value="1" />
        <asp:ListItem Text="Option 2" Value="2" />
        <asp:ListItem Text="Option 3" Value="3" />
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" EnableViewState="false" runat="server" />
    <br />
    <br />
    <asp:Label ID="lblResult" runat="server" />              
</div>

Here is the code behind code:

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

    protected void rblTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblResult.Text += "RadioButton List SelectedIndexChanged fired<br />";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblResult.Text += "Submit Button Click Event Fired<br />";
    }

    protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblResult.Text += "Dropdown List SelectedIndexChanged Event Fired<br />";
    }

Additionally, if you comment out this line in the code behind:

rblTest.SelectedIndex = 0;

Then this behavior does not occur until you select something from the radio button list. Any help is greatly appreciated.

2 个答案:

答案 0 :(得分:0)

When you set EnableViewState to false, it means you are not gonna keep the state of the objects. This means that when you do an autopost, the values are reset to their original values.

When it is a post back, you are changing the state of a radio button (rblTest.SelectedIndex = 0) and that's firing the event. So, you change one radio button and 2 events are fired.

Just something else to point out, remember that every time you do a postback, the page is recreated at server-side. And the life-cycle of the page is executed. The ViewState is used on that process to populate the controls. You can read more about it on: https://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx

答案 1 :(得分:0)

对我而言,selectedindexchanged事件在我没想到的时候也开始了。我的代码所做的是更改selectedindexchange事件的选定值。因此,下次发生回发时,selectedindexchange事件也会再次触发。我首先将selectedindex设置为-1然后设置实际值来解决此问题。

初始代码是 -

protected void rbl_selectedindexchanged(object sender, event evt)
{
rbl.Items.FindByText("something").Selected = true;
}

修改后 -

protected void rbl_selectedindexchanged(object sender, event evt)
{
rbl.SelectedIndex = -1;
rbl.Items.FindByText("something").Selected = true;
}