ASP.NET单选按钮更改

17

我正试图弄清楚为什么这段代码不触发单选按钮的更改事件。

以下是两个单选按钮的 ASP 页面代码:

  <asp:RadioButton ID="rdoButton1" GroupName="Group1" Text="Yes" Value="Yes"  runat="server" OnCheckedChanged="Group1_CheckedChanged" />
  <asp:RadioButton ID="rdoButton2" GroupName="Group1" Text="No" Value="No" runat="server" OnCheckedChanged="Group1_CheckedChanged" />

以下是代码:

protected void Group1_CheckedChanged(Object sender, EventArgs e)
{
    if (rdoButton1.Checked) {
        panel1.Visible = true;
    }

    if (rdoButton2.Checked) {
        panel1.Visible = false;
    }
}
6个回答

39

为了告诉ASP.NET改变那个元素应该触发一个回发(postback),你需要指定属性和值AutoPostBack="true"。它应该被应用于每个你希望引起回发的单选按钮(RadioButton)。


7
你需要在两个控件上添加AutoPostBack=True 属性。

但是建议将值用引号括起来。 - Grant Thomas
AutoPostBack="true" - answerSeeker

4

您需要在两个控件中都指定 AutoPostBack=True


3
我建议您使用 RadioButtonList,并为需要的内容设置 AutoPostBack=true

2

您应该将AutoPostBack设置为True,并在后台代码中处理相应的函数。

例如:

Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)           Handles RadioButton1.CheckedChanged

0

您只需要在单选按钮控件中指定AutoPostBack=True即可解决问题。

像这样:

<asp:RadioButton AutoPostBack=True ID="rdoButton1" GroupName="Group1" Text="是" Value="Yes" runat="server" OnCheckedChanged="Group1_CheckedChanged" />
<asp:RadioButton AutoPostBack=True ID="rdoButton2" GroupName="Group1" Text="否" Value="No" runat="server" OnCheckedChanged="Group1_CheckedChanged" />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接