“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 = "";
                }
            }
        }

ASP.NET控件之RadioButtonList的更多相关文章

  1. asp.net <asp:Content>控件

    <asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...

  2. FineUI 基于 ExtJS 的专业 ASP.NET 控件库

    FineUI 基于 ExtJS 的专业 ASP.NET 控件库 http://www.fineui.com/

  3. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

  4. asp.net控件的Hyperlink控件

    Asp.net控件: Hyperlink控件:Hyperlink控件又称为超链接控件,该控件在功能上跟Html的<a herf=””>控件相似,其显示的模式为超链接的形式. 注意: Hyp ...

  5. asp.net控件开发基础(1)(转)原文更多内容

    asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思. wrox网站上有本 ...

  6. 把某个asp.net 控件 替换成 自定义的控件

    功能:可以把某个asp.net 控件 替换成 自定义的控件 pages 的 tagMapping 元素(ASP.NET 设置架构) 定义一个标记类型的集合,这些标记类型在编译时重新映射为其他标记类型. ...

  7. Asp.Netserver控件开发的Grid实现(三)列编辑器

    以下是GridColumnsEditor的实现代码: GridColumnsEditor.cs using System; using System.Collections.Generic; usin ...

  8. 为ASP.NET控件加入快捷菜单

    ContextMenu Control 快捷菜单控件概述: MSDN Liabrary 中包含了几个DHTML快捷菜单的示例.分别提供了对这一功能的不能实现方法.一个快捷菜单就是在页面中任何位置的一组 ...

  9. <asp:FileUpload>控件 获取不到文件名

    <asp:FileUpload>控件 放在了<asp:UpdatePanel>控件中会导致获取不到文件名.

随机推荐

  1. [bzoj1207][HNOI2004]打鼹鼠_动态规划

    打鼹鼠 bzoj-1207 HNOI-2004 题目大意:题目链接. 注释:略. 想法: $dp_i$表示打到了第$i$个鼹鼠时最多打了多少个鼹鼠. $O(n)$转移即可. 总时间复杂度$O(n^2) ...

  2. P1028 数的计算 洛谷

    https://www.luogu.org/problem/show?pid=1028 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n): 先输入一个自然数n(n<=1000), ...

  3. Windows下如何查看当前登录用户

    1.通过whoami命令查看 2.通过username变量查看,具体命令如下:echo %username% 上述两种方法只能查看当前会话用户信息,那么如何看到其他登录用户呢? 可以通过执行query ...

  4. hadoop 文件操作

    Create a directory in HDFS - mkdir The hadoop mkdir command is for creating directories in the hdfs. ...

  5. Maven项目中遇到的奇葩问题(续)

    场景描写叙述 开发项目搞环境是一个很蛋疼的问题.总是会遇到各种奇葩的问题,上一篇文章http://blog.csdn.net/gao36951/article/details/50955526中遇到的 ...

  6. 某法院HP-P4500存储数据恢复案例

    好久没出来写博客了.过春节来了一直非常忙.尤其是近期,忙着做了好几个大单子.先是一个医院50TB的HP-EVA4400,接着是一个法院12TB的HP-P4500,前几天还有做了一个某游乐城12TB的V ...

  7. Android之——多线程下载演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...

  8. OCP-1Z0-051-题目解析-第21题

    21. Examine the description of the EMP_DETAILS table given below:  name               NULL           ...

  9. WCF探索之旅(五)——WCF与WebService的异同

    前几篇文章我们简单的介绍了WCF以及怎样使用它,今天我们来讨论一下WCF和WebService的异同. 相信大多数同学跟我一样,对于WebService有所了解.并且应该说你是先听说WebServic ...

  10. hbase shell经常使用命令

    hbase经常使用命令 /usr/local/cloud/hbase/bin/hbase shell 用shell来连接hbase exit 退出hbase shell version 查看hbase ...