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();
}
}