The. aspx code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codefile= "Sample 3-04 05.aspx.cs" inherits= "Sample_3_1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<link href= "Stylesheet.css" rel= "StyleSheet" type= "Text/css"/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div id= "mydiv" runat= "Server" >
<p> three unrelated radiobutton: </p>
<asp:radiobutton id= "rb_1" runat= "Server" text= "Lone 1"/>
<asp:radiobutton id= "rb_2" runat= "Server" text= "Lone 2"/>
<asp:radiobutton id= "Rb_3" runat= "Server" text= "Lone 3"/>
<p> button </p> with the same GroupName property
<asp:radiobutton id= "RB_A1" runat= "server" text= "cat" groupname= "Animal"/>
<asp:radiobutton id= "RB_A2" runat= "Server" text= "dog" groupname= "Animal"
/>
<asp:radiobutton id= "RB_A3" runat= "server" text= "pig" groupname= "Animal"/>
<asp:radiobutton id= "RB_A4" runat= "server" text= "sheep" groupname= "Animal"/>
<br/> <br/>
Test the selected animal with the button: <br/><br/>
<asp:button id= "Button1" runat= "Server" text= "What Animal is selected?" "
onclick= "Button1_Click"/><asp:textbox id= "Txt_animal" runat= "Server" ></asp:TextBox>
Control the selected button via a daemon: <br/><br/>
Please enter a number between 0-120:
<asp:textbox id= "txt_nums" runat= "Server" ></asp:TextBox>
<asp:button id= "Btn_checknum" runat= "Server" text= "OK"
onclick= "Btn_checknum_click"/> <br/>
<asp:label id= "Lab_info" runat= "Server" forecolor= "Red" ></asp:Label> <br/><br/>
<asp:radiobutton id= "RB_NUM1" runat= "Server" Groupname= "Numcheck" text= "Failed"/>
<asp:radiobutton id= "rb_num2" runat= "Server" Groupname= "Numcheck" text= "qualified"/>
<asp:radiobutton id= "rb_num3" runat= "Server" Groupname= "Numcheck" text= "good"/>
<asp:radiobutton id= "RB_NUM4" runat= "Server" Groupname= "Numcheck" text= "excellent"/>
</div>
</form>
</body>
The. Aspx.cs code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class sample_3_1:system.web.ui.page{protected void Page_Load (object sender, EventArgs e) {} protected void Button1_Click (object sender, EventArgs e) {if (rb_a1.c hecked) {Txt_animal. Text = "You have selected:" + rb_a1. Text; } if (rb_a2. Checked) {Txt_animal. Text = "You have selected:" + rb_a2. Text; } if (rb_a3. Checked) {Txt_animal. Text = "You have selected:" + rb_a3. Text; } if (rb_a4. Checked) {Txt_animal. Text = "You have selected:" + rb_a4. Text; }/* * Take control traversal method, look for GroupName for animal RadioButton, * understand this method, and know that there is a Foreach loop//define a variable, type control: R Adiobutton RadioButton MYRB; foreach (Control ctl in Mydiv. Controls) {//Traverse all controls under Mydiv, each loop assigns the control to a control variable named CTL to operate//determine if the control is RadioButton type if (CTL is Rad Iobutton) {//Convert control to RadioButton class to operate myRB = (RadioButton) CTL; if (MYRB. GroupName = = "Animal" && MYRB. Checked)//If GROUPNAMD is animal and is selected, output {txt_animal. Text = "The Animal you selected is:" + MYRB. Text; }}} */} protected void Btn_checknum_click (object sender , EventArgs e) {double d_num=-1; The empty action must have: otherwise the page will retain the last check result, which makes the program setting statement invalid RB_NUM1. Checked = false; Rb_num2. Checked = false; Rb_num3. Checked = false; Rb_num4. Checked = false; and D_num=-1, and the first logical expression work together to avoid input errors//In this statement, as the number can be copied to D_num, will be correctly assigned, and such as text, etc. can not be assigned to D_num, will skip do not execute,//At this time still: D_num=-1, try {d_num = double. Parse (txt_nums. Text);} Catch {}//here, D_num must be a number if (D_num < 0 | | d_num >) {lab_info. Text = "Please enter the correct number"; } else {Lab_info. Text = ""; if (D_num <) {Rb_num1. Checked = true; } if (D_num >= && D_num <) {rb_num2. Checked = true; } if (D_num >= && D_num <) {rb_num3. Checked = true; } if (D_num >= && d_num <=) {rb_num4. Checked = true; } } }}
The effect is as follows:
RadioButton Attribute Study Summary