0

就像我在标题中所写的那样,当我尝试更改 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,而是仍然认为所选索引是初始索引。

为什么?

4

1 回答 1

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

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

于 2012-09-10T12:18:51.580 回答