它是一个输入控件,用于从用户获取输入。它允许用户从选项组中选择一个选项。
要创建RadioButton,无涯教程可以从visual studio的工具箱中拖动它。
这是一个服务器端控件,ASP.NET提供自己的标记来创建它。下面给出了示例。
链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp-net-radiobutton.html
来源:LearnFk无涯教程网
<asp:RadioButtonID="RadioButton1" runat="server" Text="Male" GroupName="gender"/>
服务器将其呈现为HTML控件,并向浏览器生成以下代码。
<input id="RadioButton1" type="radio" name="gender" value="RadioButton1" /><labelfor="RadioButton1">Male</label>
此控件具有自己的属性,如下表所示。
Property | Description |
---|---|
AccessKey | 它用于设置控制的键盘快捷键。 |
TabIndex | 控件的选项卡顺序。 |
BackColor | 它用于设置控件的背景颜色。 |
BorderColor | 它用于设置控件的边框颜色。 |
BorderWidth | 它用于设置控件边框的宽度。 |
Font | 它用于为控制文本设置字体。 |
ForeColor | 它用于设置控制文本的颜色。 |
Text | 它用于设置要为控件显示的文本。 |
ToolTip | 当鼠标遍布控件时,它会显示文本。 |
Visible | 在表格上设置控制的可见性。 |
Height | 它用于设置控件的高度。 |
Width | 它用于设置控件的宽度。 |
GroupName | 它用于设置单选按钮组的名称。 |
在本例中,无涯教程创建了两个单选按钮,并将其放入名为Gender的组中。
//WebControls.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs" Inherits="WebFormsControlls.WebControls" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:RadioButton ID="RadioButton1" runat="server" Text="Male" GroupName="gender" /> <asp:RadioButton ID="RadioButton2" runat="server" Text="Female" GroupName="gender" /> </div> <p> <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" style="width: 61px" /> </p> </form> <asp:Label runat="server" id="genderId"></asp:Label> </body> </html>
//WebControls.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebFormsControlls { public partial class WebControls : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { genderId.Text = ""; if (RadioButton1.Checked) { genderId.Text = "Your gender is "+RadioButton1.Text; } else genderId.Text = "Your gender is "+RadioButton2.Text; } } }
对于此控件,无涯教程设置了一些属性,如下所示:
输出:
它会向浏览器生成以下输出。
当用户选择性别时,它会返回给客户端。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)