前台代码:
1 <div> 2 <asp:RadioButton ID="RadioButton1" runat="server" GroupName ="sex" Text ="男"/> 3 <asp:RadioButton ID="RadioButton2" runat="server" GroupName ="sex" Text ="女"/> 4 <asp:Button ID="Button1" runat="server" Text="Button" OnClick ="Button1_Click"/> 5 <asp:Label ID="Label1" runat="server" Text=""></asp:Label> 6 </div>
*其中,groupName要写上,这个的作用是让其属于同一个组,选了一个,不能选另一个。
后台代码:
1 /// <summary> 2 /// Button1按钮的单机事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void Button1_Click(object sender, EventArgs e) 7 { 8 string s = string.Empty; 9 10 if (RadioButton1.Checked) 11 { 12 s += RadioButton1.Text; 13 } 14 if (RadioButton2.Checked) 15 { 16 s += RadioButton2.Text; 17 } 18 19 this.Label1.Text = "您选择的是:" + s; 20 }
最终效果:
上面就是RadioButton控件的使用方法。
时间: 2024-10-09 13:00:44