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



就像我在标题中写的那样,当我试图更改RadioButton的选择时,后面的代码得到了错误的值。

困境.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" />

困境.aspx。cs:

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!
}

后面的代码仍然认为所选的索引是初始索引,而不是获得新选择的RadioButton。

为什么?

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

有人发布了这个答案,但由于某种原因删除了它
无论是谁在我面前发布了这个答案,它都有效!

最新更新