RadioButton控件

时间:2022-04-02 06:35:59

前台代码:

 <div>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName ="sex" Text ="男"/>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName ="sex" Text ="女"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick ="Button1_Click"/>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>

*其中,groupName要写上,这个的作用是让其属于同一个组,选了一个,不能选另一个。

后台代码:

 /// <summary>
/// Button1按钮的单机事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string s = string.Empty; if (RadioButton1.Checked)
{
s += RadioButton1.Text;
}
if (RadioButton2.Checked)
{
s += RadioButton2.Text;
} this.Label1.Text = "您选择的是:" + s;
}

最终效果:

RadioButton控件

上面就是RadioButton控件的使用方法。