At design time, if you want a radio button to show a clicked state by default, set the value of its Boolean Checked state. By default, it is set to False. If you want the radio button to be checked by default, set this value to True. Here is an example: <form id="frmExercise" method="post" runat="server"> <ul style="list-style-type: none;"> <li><asp:RadioButton id="rdoTeen" Text="Teen" runat="server" /></li> <li><asp:RadioButton id="rdoAdult" Text="Adult" Checked="True" runat="server" /></li> <li><asp:RadioButton id="rdoSenior" Text="Senior" runat="server" /></li> </ul> </form>
To use a radio button, a web page visitor can click one in a group. When a radio button has been clicked, its round button may receive a black round box in it. If the user clicks another radio button of the same group, that newly clicked button should receive the black round box and the previous one should lose it. This means that the radio button that was previous checked should have its checked state changed, the newly clicked button also. When the checked state of a radio button has changed, the control fires an event called CheckedChanged. This event is of type EventArgs class, which means there is no more information than to know that the control was clicked. To visually implement this event, click the control on the form and, in the Events section of the Properties window, double-click CheckedChange. This event is handled by the OnCheckedChanged attribute. To manually implement this event, create a method that is passed an Object and an EventArgs arguments. In the asp:RadioButton tag, add the OnCheckedChanged attribute and assign the name of the method to it. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> private void AdultButtonClicked(Object sender, EventArgs e) { } </script> <title>Exercise</title> </head> <body> <form id="frmExercise" method="post" runat="server"> <ul style="list-style-type: none;"> <li><asp:RadioButton id="rdoTeen" Text="Teen" runat="server" /></li> <li><asp:RadioButton id="rdoAdult" Text="Adult" Checked="True" OnCheckedChanged="AdultButtonClicked" runat="server" /></li> <li><asp:RadioButton id="rdoSenior" Text="Senior" runat="server" /></li> </ul> </form> </body> </html> Although we implemented an event for only one radio button, if you want, you can create an event for each radio button of the group.
The code we have used so far to create the radio buttons would produce: Notice that all radio buttons are checked. This should never be the case for all radio buttons of the same group because it deceives the purpose of mutually-exclusive selection. To solve this problem, you can create a name and assign it to each radio button of the same group. To support this, the <asp:RadioButton> tag is equipped with an attribute named GroupName, to which you can assign a name. Here are examples: <%@ Page Language="C#" %> <html> <head> <script runat="server"> private void AdultButtonClicked(Object sender, EventArgs e) { } </script> <title>Exercise</title> </head> <body> <form id="frmExercise" method="post" runat="server"> <ul style="list-style-type: none;"> <li><asp:RadioButton id="rdoTeen" Text="Teen" GroupName="Membership" runat="server" /></li> <li><asp:RadioButton id="rdoAdult" Text="Adult" GroupName="Membership" OnCheckedChanged="AdultButtonClicked" runat="server" /></li> <li><asp:RadioButton id="rdoSenior" Text="Senior" GroupName="Membership" runat="server" /></li> </ul> </form> </body> </html> This time, when a user clicks a radio button, that button receives focus and is checked; no other radio button of the same group should be checked.
When a radio button has been clicked, if you want the result to be posted to the server, set its AutoPostBack Boolean attribute accordingly. By default, its value is set to "False". If you want to change it, type this attribute and assign "True" to it. You can do this for each radio button of the group. |
|
|||||||||
|