Radio button 代码隐藏获取错误的单选按钮值

Radio button 代码隐藏获取错误的单选按钮值,radio-button,webforms,Radio Button,Webforms,就像我在标题中写的那样,当我试图更改单选按钮的选择时,隐藏的代码得到了错误的值 困境。aspx: <asp:RadioButtonList ID="rbList" runat="server"> <asp:ListItem Text="Yes, please." /> <asp:ListItem Text="No, thanks." /> <asp:ListItem Text="Ummm... maybe." /> <

就像我在标题中写的那样,当我试图更改单选按钮的选择时,隐藏的代码得到了错误的值

困境。aspx:

<asp:RadioButtonList ID="rbList" runat="server">
    <asp:ListItem Text="Yes, please." />
    <asp:ListItem Text="No, thanks." />
    <asp:ListItem Text="Ummm... maybe." />
</asp:RadioButtonList>
<asp:Button ID=""btnChoose" runat="server" text="OK" OnClick="btnChoose_Click" />
protected void Page_Load(object sender, EventArgs e)
{
    int initialIndex = GetInitialIndex(); // Simplified for sake of question.
    rbList.SelectedIndex = initialIndex; // This works.
}

protected void btnChoose_Click(object sender, EventArgs e)
{
    int selection = rbList.SelectedIndex; // This gets it wrong!
}
隐藏代码没有获取新选择的单选按钮,而是认为所选索引是初始索引

为什么?

有人发布了此答案,但出于某种原因将其删除。
不管是谁在我面前发布了这个答案——它是有效的

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        int initialIndex = GetInitialIndex(); // Simplified for sake of question.
        rbList.SelectedIndex = initialIndex; // This works.
    }
}