我怎样得到一个Asp.Net单选框在单击时回调

本文关键字:单选框 Net Asp 单击 回调 一个 | 更新日期: 2023-09-27 18:04:22

我正在制作一个asp.net/C#应用程序,并试图让一个单选框在单击其上的任何按钮时调用回调。有人知道怎么做吗?我试过了……

<asp:RadioButton id="id1" onclick="do_foo" runat="server" Text="Text1" GroupName="mybox" />
<asp:RadioButton id="id2" onclick="do_foo" runat="server" Text="Text2" GroupName="mybox" />
<asp:RadioButton id="id3" onclick="do_foo" runat="server" Text="Text3" GroupName="mybox" />

<asp:RadioButtonList onclick="do_foo" id="radiolist1" runat="server">
    <asp:ListItem>Text1</asp:ListItem>
    <asp:ListItem>Text2</asp:ListItem>
    <asp:ListItem>Text3</asp:ListItem>
</asp:RadioButtonList>

但在这两种情况下得到错误"Microsoft JScript运行时错误:'do_foo'是未定义"。我知道do_foo已经定义了,因为我可以从按钮调用它。

我怎样得到一个Asp.Net单选框在单击时回调

onclick="do_foo();"

经过测试,没有括号,它不会触发

请在函数名后加上括号,应该像这样

onclick="do_foo();"

你把Javascript客户端函数和c#服务器端函数混淆了。

单选按钮没有服务器端'onclick'事件,但客户端存在,所以当点击它时浏览器找不到do_foo,因此出现错误。

您必须使用AutoPostBack="True"OnSelectedIndexChanged事件来检查哪个项目被点击,如:

if(radiolist1.SelectedIndex==0) 
{
  //first item clicked
}
else if(radiolist1.SelectedIndex==1) 
{
  //second item clicked
}

试试这个:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" 
    onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
    <asp:ListItem>text1</asp:ListItem>
    <asp:ListItem>text2</asp:ListItem>
    <asp:ListItem>text3</asp:ListItem>
</asp:RadioButtonList>