<input type=“radio”name=“xxx”...> 語法形式 <asp:RadioButton ID=”rb_cat” runat=”server”> GroupName=”animal” Text=”貓” AutoPostBack=“false || true” Checked=“false||true” oncheckedChanged=“functionName”
規范文本框輸入內容有誤
目標:希望輸入0-120之間的數字
問題: 可能輸入了不在0-120之間的數字
可能輸入的根本不是數字
可能根本沒有輸入
D_num -> try{d_num=...} -> 0或者轉換過來的數字
標簽部分ASP <div> <asp:RadioButton ID="RadioButton1" runat="server" Text="貓" GroupName="fenzu" /> <!-- 加上groupname 說明這個四個radiobutton是一個組的,只能選擇一個。沒有groupname 都可以選擇--> <asp:RadioButton ID="RadioButton2" runat="server" Text="狗" GroupName="fenzu"/> <asp:RadioButton ID="RadioButton3" runat="server" Text="雞" GroupName ="fenzu"/> <asp:RadioButton ID="RadioButton4" runat="server" Text="豬" GroupName="fenzu" /> <br /> <asp:Button ID="Button1" runat="server" Text="你選擇的是:" OnClick="Button1_Click" /><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> 請輸入您的考試成績:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="提交" /> <br /> <asp:Label ID="Label1" runat="server" Text=" "></asp:Label> <br /> 請選擇下面的分數: <asp:RadioButton ID="RadioButton5" runat="server" GroupName="fenshu" Text="不及格" /> <asp:RadioButton ID="RadioButton6" runat="server" GroupName="fenshu" Text="及格"/> <asp:RadioButton ID="RadioButton7" runat="server" GroupName="fenshu" Text="良好"/> <asp:RadioButton ID="RadioButton8" runat="server" GroupName="fenshu" Text="優秀" /> </div> C#事件部分 protected void Button1_Click(object sender, EventArgs e) { try { if (RadioButton1.Checked == true) { TextBox1.Text = RadioButton1.Text; } if (RadioButton2.Checked == true) { TextBox1.Text = RadioButton2.Text; } if (RadioButton3.Checked == true) { TextBox1.Text = RadioButton3.Text; } if (RadioButton4.Checked == true) { TextBox1.Text = RadioButton4.Text; } } catch { } } protected void Button2_Click(object sender, EventArgs e) { Label1.Text = " "; RadioButton5.Checked = RadioButton6.Checked = RadioButton7.Checked = RadioButton8.Checked = false; //radioButton默認不選中 int a = -1; try { a = int.Parse(TextBox2.Text); } catch { } if (a>=0&&a<=100) { if (a < 60) { RadioButton5.Checked = true; } if (a >= 60 && a <= 70) { RadioButton6.Checked = true; } if (a > 70 && a < 90) { RadioButton7.Checked = true; } if (a > 90 && a <= 100) { RadioButton8.Checked = true; } } else { Label1.Text = "請輸入正確的分數"; }
RadioButton 2個編程要點
AutoPostBack
在change事件發生后立即執行(提交頁面)
后台程序向設置某組某項為選定狀態是,必須確保同組的其他控件為checked=false