How do I get an Asp.Net radiobox to callback when it is clicked?
I'm working on an asp.net/C# application, and trying to get a radiobox to invoke a callback when any of the buttons on it is clicked. Does anyone know how to do this? I've tried this...
<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="Text开发者_如何学运维3" GroupName="mybox" />
and also this...
<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>
but in both cases get the error "Microsoft JScript runtime error: 'do_foo' is undefined". I know that do_foo is defined because I can invoke it from a button.
onclick="do_foo();"
Tested without the brackets and it doesn't fire.
Please include brackets after function name, It should like this
onclick="do_foo();"
You are mistaking Javascript client side function with a c# server side function.
Radio Buttons dont have a server side 'onclick' event but a client side exists so when it is clicked the browser cant find do_foo, hence the error.
You will have to use AutoPostBack="True"
and the OnSelectedIndexChanged
event to check which item was clicked like:
if(radiolist1.SelectedIndex==0)
{
//first item clicked
}
else if(radiolist1.SelectedIndex==1)
{
//second item clicked
}
Try this:
<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>
精彩评论