1.单选按钮(RadioButtonList)1.1.概述在ASP.NETWebForm应用程序中,单选按钮使用RadioButtonList/RadioButton控件表示,用来呈现一组相互排斥的按钮。每组只能选择一项。单选按钮一般用于表示男/女、是/否等排拆相反的选项。如下是在aspx页面上添加的RadioButtonList按钮完整Html代码:这里在aspx页面上拖放了RadioButtonList按钮,源码为:asp:RadioButtonListID=RadioButtonList1runat=server/asp:RadioButtonListRadioButtonList按钮的ID值以“rbl”开头。可以在设计器中查看RadioButtonList按钮在浏览器中的样子,这样可以将源码与生成的结果一一进行对应。1.2.添加选项可以使用任务窗格或代码给RadioButtonList添加选项。在图中点击“编辑项…”,如下图:点击“添加”按钮就可以给单选按钮添加项了。用法与DropDownList控件一样。Text属性用于显示,Value属性用于内部编程使用。运行一下上面的代码:在上图中已经生成具有2个选项的单选按钮了,二者只能选择一个,默认没有任何项选中。1.3.排列方向对于RadioButtonList按钮,默认是竖排的,现在修改为横排:只需要将RadioButtonList控件代码中的RepeatDirection属性的值由Vertical修改为Horization即可。RepeatDirection=Horizontal表示横向排列选项。RepeatDirection=Vertical表示竖向排列选项。运行一下代码:1.4.默认值对于单选按钮,一般情况下,会有一个常用的默认值,这里将“男”设置为默认值。将“Selected”属性设置为“True”即可将此项设置为默认值。1.5.获取选中的值在页面上添加一个按钮,并在按钮中编写如下代码:用法与DropDownList获取值是一样的,完整C#代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespaceYidosoft.Edu{publicpartialclassRadioButtonListControl:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidbtnOK_Click(objectsender,EventArgse){//获取Text的值stringstrSex1=rblSex.SelectedItem.Text;//获取Value的值stringstrSex2=rblSex.SelectedItem.Value;//或:rblSex.SelectedValueResponse.Write(选中的Text的值:+strSex1+br/);Response.Write(选中的Value的值:+strSex2+br/);}}}2.单选按钮(RadioButton)2.1.概述RadioButton也表示单选按钮,但不是一组,而只是单个的按钮,在aspx页面上添加一个RadioButton按钮:asp:RadioButtonID=RadioButton1runat=server/运行一下:发现只是一个单选按钮,也没有文本,现在添加文本:也可以设置默认值:使用的是Checked属性设置默认值,当Checked为True时表示选中。2.2.实现一组按钮RadioButton也可以做成相互排斥的一组单选按钮:divasp:RadioButtonID=rbDengShanText=登山runat=serverGroupName=GlobalChecked=True/asp:RadioButtonID=rbHuaXueText=滑雪runat=serverGroupName=Global/asp:RadioButtonID=rbYouYongText=游泳runat=serverGroupName=Global//div通过GroupName的值相同归为一组,使用每个值相互排斥,一次只能选择一个。2.3.赋值和获取值RadioButton与RadioButtonList在赋值和获取值时是有一些不一样的。且设置默认值的属性也不一样:给RadioButton赋值和取值的C#完整代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespaceYidosoft.Edu{publicpartialclassRadioButton:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidbtnOK_Click(objectsender,EventArgse){if(rbDengShan.Checked){Response.Write(您已经选择了

rbDengShan.Text);}if(rbHuaXue.Checked){Response.Write(您已经选择了

rbHuaXue.Text);}if(rbYouYong.Checked){Response.Write(您已经选择了

rbYouYong.Text);}}}}对RadioButton中的每个项,需要单个的去判断,运行一下代码,结果如下:C#深入编程-线上视频图文课堂配套课后技能作业+理论考试+源码淘宝¥购买已下架