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