RadioButtonList

1.用法

<asp:RadioButtonList ID="rblIsLock" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
                        <asp:ListItem Selected="True" Value="0">启用 </asp:ListItem>
                        <asp:ListItem Value="1">禁用 </asp:ListItem>
                    </asp:RadioButtonList><label>*禁用的用户将无法登录</label>

2.RadioButtonList控件里的每项asp:ListItem添加单独的ToolTip也就是鼠标悬停的提示信息

protected void Page_Load(object sender, EventArgs e)
    {
        RadioButtonList1.Items[0].Attributes.Add("title", "111");
        RadioButtonList1.Items[1].Attributes.Add("title", "111");
    }

3.使用复杂绑定完成 CheckBoxList 和 RadioButtonList 控件的绑定

View Code
前台:

<body>

    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        </asp:RadioButtonList>
        <br />
        <br />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        </asp:CheckBoxList>
    </div>
    </form>

</body>


后台:

 protected void Page_Load(object sender, EventArgs e)
        {
            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();

            this.CheckBoxList1.DataSource = dt;
            this.CheckBoxList1.DataTextField = "Provinces";
            this.CheckBoxList1.DataValueField = "PId";
            this.CheckBoxList1.DataBind();
        }


SQLHelper类:
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(sql,connStr);
        if (pms != null)
        {
            adapter.SelectCommand.Parameters.AddRange(pms);
        }
        adapter.Fill(dt);
        return dt;
    }

4.当选项被选中时,更改选中选项的前景色

View Code
 <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;");
            }
        }
    }
原文地址:https://www.cnblogs.com/LifeKingcn/p/2825120.html