【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《.NET4.0 单选框控件》,欢迎阅读! ASP.NET4.0 单选框控件 使用HtmlInputRadioButton控件对HTMLtype=radio>元素进行编程,通过将Name属性设置为组中所有type=radio>元素所共有的值,可以将多个HtmlInputRadioButton控件组成一组。同组中的单选按钮互相排斥;一次只能选择该组中的一个单选按钮。 HtmlRadioButton控件不会自动向服务器回送。必须依赖于使用某个按钮控件(如HtmlInputButton、HtmlInputImage或HtmlButton)来回送到服务器。可通过为ServerChange事件编写处理程序来对HtmlRadioButton控件进行编程。 只为更改成选中状态的单选按钮引发ServerChange事件。 当我们要限制使用者的选择为单选,并只能够在我们所提供的项目中选择一个答案时,使用HtmlInputRadio。以输入使用者性别数据为例,我们提供使用者“男”及“女”的选项让使用者选择,利用单选按钮可以限制他只能选择一个答案。其中HtmlInputRadio控件最重要的属性为Name属性,用来设定单选按钮的群组。单选按钮有一个规则,那就是同一个群组的单选按钮同一时间内只能有一个按钮被选择,所以它可以用在单选的选项上。此时被选取的单选按钮其Checked属性则为True,没有被选取则为False。 单选框控件声明的语法格式如下所示: Id="programmaticID" Checked Name="radiobuttongroup" Runat="server"> HtmlInputRadioButton控件是选项控件,如果Name属性设为相同,则表示这是一组单选按钮,在一组单选按钮中,在某一时刻只能有一个按钮是选中状态。如果checked属性被选中,则表示该单选框默认是选中状态。 下面是一个使用HtmlInputRadioButton控件选择用户喜欢运动的实例,代码如下所示。 文件名:RadioButton.aspx <%@ Page Language="C#" html PUBLIC AutoEventWireup="true" "-//W3C//DTD CodeFile="RadioButton.aspx.cs" 1.0 Transitional//EN" Inherits="RadioButton" %> XHTML "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> HtmlInputRadioButton控件实例 请从下面选项中选择你喜欢的运动 篮球 足球 台球 id="Button1" type="button" value="确定" onserverclick="button_click" runat="server" /> 文件名:RadioButton.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class RadioButton : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e){ } public void button_click(object sender, EventArgs e){ if (Radio1.Checked == true){ showInfo.InnerText = "你喜欢的运行是篮球"; } if (Radio2.Checked == true){ showInfo.InnerText = "你喜欢的运行是足球"; } if (Radio3.Checked == true){ showInfo.InnerText = "你喜欢的运行是台球"; } } } 执行上述代码结果,选择单选按钮中你喜欢的运动,单击“确定”按钮,结果如图4-16所示。 图4-16 执行结果 本文来源:https://www.wddqxz.cn/617bbe3502768e9951e738b8.html
本文来源:https://www.wddqxz.cn/617bbe3502768e9951e738b8.html