ASP.NET控件之RadioButtonList详解
“RadioButtonList”控件表示一个封装了一组单选按钮控件的列表控件。
可以使用两种类型的ASP.NET控件将单选按钮添加到网页上:各个“RadioButton”控件或一个“RadioButtonList”控件。这两类控件都允许用户从一小组互相排斥的预定义选项中进行选择。使用这些控件,可定义任意数目的带标签的单选按钮,并将它们水平或垂直排列。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在system.web.dll中)
[ValidationPropertyAttribute("SelectedItem")]
publicclassRadioButtonList:ListControl,IRepeatInfoUser,INamingContainer,IPostBackDataHandler
RadioButtonList控件为网页开发人员提供了一组单选按钮,这些按钮可以通过数据绑定动态生成。该控件包含一个Items集合,集合中的成员与列表中的各项相对应。若要确定选择了哪一项,请测试列表的SelectedItem属性。
可以用RepeatLayout和RepeatDirection属性指定如何呈现列表。如果将RepeatLayout设置为RepeatLayout.Table(默认设置),列表将呈现在表中。如果设置为RepeatLayout.Flow,列表将不以表格形式呈现。默认情况下,RepeatDirection设置为RepeatDirection.Vertical。将该属性设置为RepeatDirection.Horizontal时,列表将水平呈现。
RadioButtonList用法:
<divclass="rblStyle">
<asp:RadioButtonListID="rblChangQHT"runat="server"RepeatDirection="Horizontal">
<asp:ListItemText="是"Value="1"></asp:ListItem>
<asp:ListItemText="否"Value="0"></asp:ListItem>
</asp:RadioButtonList></div>
1.RadioButtonList校验
varrb_ChangQHT=document.getElementById("rblChangQHT"); varShiF=rb_ChangQHT.getElementsByTagName("INPUT"); varresult=false; for(vari=0;i<ShiF.length;i++){ if(ShiF[i].checked){ result=true; break; } } if(!result){ alert("是否为中长期合同为必填项!"); returnfalse; }
2.RadioButtonList样式调整
.rblStyle{width:100%;height:auto;}
.rblStyleinput{border-style:none;}
3.onselectedindexchanged事件
像下拉控件dropdownlist控件一样,它也有onselectedindexchanged事件,当选项改变后进行触发
注意点是:控件中的AutoPostBack属性一定设为"True",这样服务器端才知道你的选项改变了,并触发相应事件
4.为ListItem添加提示
RadioButtonList1.Items[0].Attributes.Add("title","提示内容");
5.绑定数据源
stringsql="select*fromprovince"; DataTabledt=SQLHelper.ExecuteDataTable(sql); this.RadioButtonList1.DataSource=dt; this.RadioButtonList1.DataTextField="Provinces"; this.RadioButtonList1.DataValueField="PId"; this.RadioButtonList1.DataBind();
6.改变选中项的前景色
<asp:RadioButtonListID="rblIsLock"runat="server"AutoPostBack="true"OnSelectedIndexChanged="rblIsLock_SelectedIndexChanged"RepeatDirection="Horizontal"RepeatLayout="Flow"> <asp:ListItemSelected="True"Value="0">启用</asp:ListItem> <asp:ListItemValue="1">禁用</asp:ListItem> </asp:RadioButtonList> <label>*禁用的用户将无法登录</label>
后台:
protectedvoidrblIsLock_SelectedIndexChanged(objectsender,EventArgse) { varrbl=senderasRadioButtonList; HighliehgSelectedItem(rbl); } privatevoidHighliehgSelectedItem(RadioButtonListrbl) { foreach(ListItemliinrbl.Items) { if(li.Selected) { li.Attributes.Add("style","color:red;"); } } }
7.后台动态增加RadioButtonList
RadioButtonListrbl=newRadioButtonList(); rbl.ID="rbl"+(i+1).ToString(); rbl.BorderStyle=BorderStyle.None; rbl.RepeatLayout=RepeatLayout.Flow; rbl.RepeatDirection=RepeatDirection.Horizontal; rbl.TextAlign=TextAlign.Right; rbl.CellSpacing=6; rbl.Attributes.Add("onclick","CheckRbl('ctl00_ctl00_ctl00_ContentPlaceHolder1_cphBody_cphLower_"+rbl.ID+"')"); rbl.DataSource=dtRating.DefaultView; rbl.DataTextField="LevelID"; rbl.DataValueField="LevelID"; rbl.DataBind(); tc.Controls.Add(rbl);//tc是TableRow的一个单元格TableCell for(intk=0;k<rbl.Items.Count;k++) { rbl.Items[k].Attributes.Add("title",dtRating.Rows[k][1].ToString()); rbl.Items[k].Attributes.Add("style","margin-left:10px;"); }
8.前台改变选中项的背景色
window.onload=function(){ vararr=document.getElementsByTagName("INPUT"); for(vari=0;i<arr.length;i++){ if(arr[i].checked){ if(arr[i].type=="radio"){ arr[i].style.backgroundColor="red"; } else{ arr[i].style.backgroundColor=""; } } else{ arr[i].style.backgroundColor=""; } } }
为大家附3个精彩的专题:
ASP.NET控件使用手册
ASP.NET数据绑定控件使用汇总
ASP.NET控件使用汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。