前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <br/> <asp:button ID="Button1" runat="server" text="实例三确定" onclick="Button1_Click" /> </div> <hr/> <br/> <p>拥有相同属性的na组</p> <asp:RadioButton ID="Rn1" runat="server" Text= "猫" GroupName="na"/> <asp:RadioButton ID="Rn2" runat="server" Text= "狗" GroupName="na" /> <asp:RadioButton ID="Rn3" runat="server" Text= "鼠" GroupName="na" /> <asp:RadioButton ID="Rn4" runat="server" Text= "鱼" GroupName="na" /> 通过按钮来测试选中什么动物<br/> <asp:Button ID="Button2" runat="server" Text="选中了什么动物" onclick="Button2_Click" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <hr/> <br/> 请输入0-120的任意数字: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button3" runat="server" Text="确定" onclick="Button3_Click" /><br/> <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label><br/> <asp:RadioButton ID="Ran1" runat="server" Text = "不及格" GroupName = "yz"/> <asp:RadioButton ID="Ran2" runat="server" Text = "及格" GroupName = "yz"/> <asp:RadioButton ID="Ran3" runat="server" Text = "良好" GroupName = "yz"/> <asp:RadioButton ID="Ran4" runat="server" Text = "优秀" GroupName = "yz"/> <br/> <br/> <div style = "border:1px solid red;180px;"> 谁最能吃<br /><br /> <asp:RadioButton ID="R1" runat="server" Text = "孙玉滨" GroupName = "key" AutoPostBack = "true" OnCheckedChanged = "rr1" /> <asp:RadioButton ID="R2" runat="server" Text = "李小妹" GroupName = "key" AutoPostBack = "true" OnCheckedChanged = "rr2"/> <asp:RadioButton ID="R3" runat="server" Text = "李二妹" GroupName = "key" AutoPostBack = "true" OnCheckedChanged = "rr3"/> <asp:RadioButton ID="R4" runat="server" Text = "李三妹" GroupName = "key" AutoPostBack = "true" OnCheckedChanged = "rr4"/> <br/> <br/> <asp:Label ID="Label2" runat="server" Text=""><br/> </asp:Label><asp:Button ID="Button" runat="server" Text="提交" Height = "53px" Width = "113px" onclick="Button_Click"/> </div> </form> </body> </html>
后端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { int s = 0; for (int i = 1; i <= 10; i++) { s += i; } Response.Write("1到10的和为" + s.ToString()); } protected void Button2_Click(object sender, EventArgs e) { if (Rn1.Checked) {TextBox1.Text = "你选中了:"+Rn1.Text;} if (Rn2.Checked) {TextBox1.Text = "你选中了:"+Rn2.Text;} if (Rn3.Checked) {TextBox1.Text = "你选中了:"+Rn3.Text;} if (Rn4.Checked) { TextBox1.Text = "你选中了:" + Rn4.Text; } } protected void Button3_Click(object sender, EventArgs e) { //将文本框的值转化成 浮点型 double num = -1; //每次运行之前要清空所选中的文本框 Label1.Text = ""; Ran1.Checked = false; Ran2.Checked = false; Ran3.Checked = false; Ran4.Checked = false; //先将文本框的值 转换成浮点型,再将其赋值给num, //因为num事先已经是-1了, try{num = double.Parse(TextBox2.Text);} catch {} if (num < 0 || num > 120) { Label1.Text = "请输入正确的数字"; } else{ if(num<60){Ran1.Checked = true;} if(num>60&&num<80){Ran2.Checked = true;} if(num>80&&num<100){Ran3.Checked = true;} if(num>100&&num<120){Ran4.Checked = true;} } } protected void rr1(object sender, EventArgs e) { if (R1.Checked == true) { Label2.Text = "你选择了" + R1.Text; } } protected void rr2(object sender, EventArgs e) { if (R2.Checked == true) { Label2.Text = "你选择了" + R2.Text; } } protected void rr3(object sender, EventArgs e) { if (R3.Checked == true) { Label2.Text = "你选择了" + R3.Text; } } protected void rr4(object sender, EventArgs e) { if (R4.Checked == true) { Label2.Text = "你选择了" + R4.Text; } } protected void Button_Click(object sender, EventArgs e) { if (R1.Checked == false && R2.Checked == false && R3.Checked == false && R4.Checked == false) { Response.Write("<script>alert('请选择正确答案')</script>"); } else if (R2.Checked) { Response.Write("<script>alert('答对了')</script>"); } else { Response.Write("<script>alert('答错了')</script>"); } } }