Asp.Net CheckBox嵌套绑定CheckBoxList

 CheckBoxList.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxList.aspx.cs" Inherits="checkBoxList" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>嵌套绑定CheckBoxList</title>
    <link href="../style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript">
    function CheckChange(obj)
    {
        //注意获取父级节点parentNode,然后判断全选
        var paretNode = obj.parentNode.parentNode;
        var tagName = paretNode.getElementsByTagName("input");
        for(var i=0;i<tagName.length;i++)
        {
            if(tagName[i].type == "checkbox")
            {
                if(obj.checked)
                {
                    tagName[i].checked = true;
                }
                else
                {
                    tagName[i].checked = false;
                }
            }
        }   
    }


    </script>
</head>
<body>
   
    <form id="form1" runat="server">
        <asp:Repeater ID="DataList1" runat="server"
            onitemdatabound="DataList1_ItemDataBound">
        <ItemTemplate>
        <div>
            <asp:CheckBox runat="server" ID="moitor_type_id" Text='<%#Eval("monitor_type_name") %>' Font-Bold="true" οnclick="CheckChange(this);"  />
            <div>
                <asp:CheckBoxList ID="monitor_id" runat="server" name="monitor_id" RepeatDirection="Horizontal" RepeatLayout="Flow" >
               
                </asp:CheckBoxList>
            </div>
       </div>
       </ItemTemplate>
       </asp:Repeater>
    <asp:Label ID="Label1" runat="server" ></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="取值" />
    </form>
</body>
</html>

 

 CheckBoxList.aspx.cs

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class checkBoxList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_CheckBoxList();
            CheckBoxList_Init();
        }
    }

    protected void Bind_CheckBoxList()
    {
        string sql = "select monitor_type_id,monitor_type_name from monitor_type  where status=1 order by orderby desc,monitor_type_id";
        DbConn conn = new DbConn();
        DataTable dt = conn.DataTable(sql);
        DataList1.DataSource = dt;
        DataList1.DataBind();
        dt.Dispose();
        conn.Close();
    }
   
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string monitor_type_id = ((DataRowView)e.Item.DataItem).Row["monitor_type_id"].ToString();
        CheckBoxList monitor_id = (CheckBoxList)e.Item.FindControl("monitor_id");
        if (monitor_id != null)
        {
            string sql = "select monitor_id,monitor_name from monitor_config  where monitor_type_id=" + monitor_type_id;
            DbConn conn = new DbConn();
            DataTable dt = conn.DataTable(sql);
            monitor_id.DataSource = dt;
            monitor_id.DataTextField = "monitor_name";
            monitor_id.DataValueField = "monitor_id";
            monitor_id.DataBind();
            dt.Dispose();
            conn.Close();
        }
    }

    protected void getCheckBoxListValue()
    {
        //取得CheckBoxList选中项的值
        string returnValue = "";
        foreach (RepeaterItem DataItem in DataList1.Items)
        {
            foreach (ListItem item in ((CheckBoxList)DataItem.FindControl("monitor_id")).Items)
            {
                if (item.Selected == true)
                {
                    if (returnValue == "") returnValue = item.Value;
                    else returnValue += "," + item.Value;
                }
            }
        }
        Label1.Text = returnValue;
        Session["CheckBoxListValue"] = returnValue;
    }//End

    protected void CheckBoxList_Init()
    {
        //初始化CheckBoxList选中项
        foreach (RepeaterItem DataItem in DataList1.Items)
        {
            foreach (ListItem item in ((CheckBoxList)DataItem.FindControl("monitor_id")).Items)
            {
                if (("," + Session["CheckBoxListValue"] + ",").IndexOf("," + item.Value + ",") != -1)
                {
                    item.Selected = true;
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        getCheckBoxListValue();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值