“RadioButtonList”控件表示一个封装了一组单选按钮控件的列表控件。
可以使用两种类型的 ASP.NET 控件将单选按钮添加到网页上:各个“RadioButton”控件或一个“RadioButtonList”控件。这两类控件都允许用户从一小组互相排斥的预定义选项中进行选择。使用这些控件,可定义任意数目的带标签的单选按钮,并将它们水平或垂直排列。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
[ValidationPropertyAttribute("SelectedItem")]
public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
RadioButtonList 控件为网页开发人员提供了一组单选按钮,这些按钮可以通过数据绑定动态生成。该控件包含一个 Items 集合,集合中的成员与列表中的各项相对应。若要确定选择了哪一项,请测试列表的 SelectedItem 属性。
可以用 RepeatLayout 和 RepeatDirection 属性指定如何呈现列表。如果将 RepeatLayout 设置为 RepeatLayout.Table(默认设置),列表将呈现在表中。如果设置为 RepeatLayout.Flow,列表将不以表格形式呈现。默认情况下,RepeatDirection 设置为 RepeatDirection.Vertical。将该属性设置为 RepeatDirection.Horizontal 时,列表将水平呈现。
RadioButtonList用法:
<div class="rblStyle">
<asp:RadioButtonList ID="rblChangQHT" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="是" Value="1"></asp:ListItem>
<asp:ListItem Text="否" Value="0"></asp:ListItem>
</asp:RadioButtonList></div>
1.RadioButtonList 校验
var rb_ChangQHT = document.getElementById("rblChangQHT"); var ShiF = rb_ChangQHT.getElementsByTagName("INPUT"); var result = false; for (var i = 0; i < ShiF.length; i++) { if (ShiF[i].checked) { result = true; break; } } if (!result) { alert("是否为中长期合同为必填项!"); return false; }
2.RadioButtonList样式调整
.rblStyle{width:100%;height:auto;}
.rblStyle input{border-style:none;}
3.onselectedindexchanged事件
像下拉控件dropdownlist控件一样,它也有onselectedindexchanged事件,当选项改变后进行触发
注意点是:控件中的AutoPostBack属性一定设为"True",这样服务器端才知道你的选项改变了,并触发相应事件
4.为ListItem添加提示
RadioButtonList1.Items[0].Attributes.Add("title", "提示内容");
5.绑定数据源
string sql = "select * from province"; DataTable dt = SQLHelper.ExecuteDataTable(sql); this.RadioButtonList1.DataSource = dt; this.RadioButtonList1.DataTextField = "Provinces"; this.RadioButtonList1.DataValueField = "PId"; this.RadioButtonList1.DataBind();
6.改变选中项的前景色
<asp:RadioButtonList ID="rblIsLock" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblIsLock_SelectedIndexChanged" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Selected="True" Value="0">启用 </asp:ListItem> <asp:ListItem Value="1">禁用 </asp:ListItem> </asp:RadioButtonList> <label>*禁用的用户将无法登录</label>
后台:
protected void rblIsLock_SelectedIndexChanged(object sender, EventArgs e) { var rbl = sender as RadioButtonList; HighliehgSelectedItem(rbl); } private void HighliehgSelectedItem(RadioButtonList rbl) { foreach (ListItem li in rbl.Items) { if (li.Selected) { li.Attributes.Add("style", "color: red;"); } } }
7.后台动态增加RadioButtonList
RadioButtonList rbl = new RadioButtonList(); 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 (int k = 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 () { var arr = document.getElementsByTagName("INPUT"); for (var i = 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控件使用汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。