DataList 嵌套绑定CheckBoxList [记录, 以免忘记哈.]
.cs
1
//绑定datalist
2
protected void BindMenu()
3
{
4
ICollection<Org_SysMenusRecords> fMenu = org_first.getMenu();
5
Menu.DataSource = fMenu;
6
Menu.DataKeyField = "MenuNo";
7
Menu.DataBind();
8
}
9
//ItemDataBound 事件中绑定checkboxlist
10
rotected void Menu_ItemDataBound(object sender, DataListItemEventArgs e)
11
{//purview list
12
string parent = Menu.DataKeys[e.Item.ItemIndex].ToString();
13
BindPurview(parent, (CheckBoxList)e.Item.FindControl("two_menu"));
14
}
15
16
protected void BindPurview(string parent, CheckBoxList bb)
17
{
18
ICollection<Org_SysSecMenusRecords> sMenu = org_second.getMenuByParent(parent);
19
if (sMenu.Count > 0)
20
{
21
bb.DataSource = sMenu;
22
bb.DataTextField = "ChildMenuName";
23
bb.DataValueField = "ChildMenuNo";
24
bb.DataBind();
25
}
26
}
27
28
//绑定 checkboxlist的值
29
protected void BindPurviewSelected(int argid)
30
{
31
employee.Value = argid.ToString();
32
Org_UserRecords item = org_employees.SelectOrg_UserRecords(argid);
33
//string[] purview = item.purview.Split(new char[] { ',' });
34
string purview = item.purview;
35
foreach (DataListItem oDataListItem in Menu.Items)
36
{//注意这里. ListItem
37
foreach (ListItem oListItem in ((CheckBoxList)oDataListItem.FindControl("two_menu")).Items)
38
{
39
if (purview.Contains(oListItem.Value)) {
40
oListItem.Selected = true;
41
((CheckBox)oDataListItem.FindControl("opt")).Checked = true;
42
}
43
}
44
}
45
}
46
47
48
49
//得到checkboxlist的值
50
protected void purviewEdit()
51
{
52
if (string.IsNullOrEmpty(employee.Value))
53
return;
54
55
string resultValue = string.Empty;
56
foreach (DataListItem oDataListItem in Menu.Items)
57
{
58
if (((CheckBox)oDataListItem.FindControl("opt")).Checked)
59
{
60
foreach (ListItem item in ((CheckBoxList)oDataListItem.FindControl("two_menu")).Items)
61
{
62
if (item.Selected)
63
resultValue += item.Value + ",";
64
}
65
}
66
}
67
//resultValue.TrimEnd(new char[] { ',' });
68
if (!org_employees.UpdateOrg_UserRecords("Org_UserRecords.purview", resultValue.TrimEnd(new char[] { ',' }), int.Parse(employee.Value)))
69
lblRurviewError.InnerHtml = "An update error about purview";
70
}
71
//绑定datalist 2
protected void BindMenu()3
{4
ICollection<Org_SysMenusRecords> fMenu = org_first.getMenu();5
Menu.DataSource = fMenu;6
Menu.DataKeyField = "MenuNo";7
Menu.DataBind();8
}9
//ItemDataBound 事件中绑定checkboxlist10
rotected void Menu_ItemDataBound(object sender, DataListItemEventArgs e)11
{//purview list12
string parent = Menu.DataKeys[e.Item.ItemIndex].ToString();13
BindPurview(parent, (CheckBoxList)e.Item.FindControl("two_menu"));14
}15

16
protected void BindPurview(string parent, CheckBoxList bb)17
{18
ICollection<Org_SysSecMenusRecords> sMenu = org_second.getMenuByParent(parent);19
if (sMenu.Count > 0)20
{21
bb.DataSource = sMenu;22
bb.DataTextField = "ChildMenuName";23
bb.DataValueField = "ChildMenuNo";24
bb.DataBind();25
}26
}27

28
//绑定 checkboxlist的值29
protected void BindPurviewSelected(int argid)30
{31
employee.Value = argid.ToString();32
Org_UserRecords item = org_employees.SelectOrg_UserRecords(argid);33
//string[] purview = item.purview.Split(new char[] { ',' });34
string purview = item.purview;35
foreach (DataListItem oDataListItem in Menu.Items)36
{//注意这里. ListItem37
foreach (ListItem oListItem in ((CheckBoxList)oDataListItem.FindControl("two_menu")).Items)38
{39
if (purview.Contains(oListItem.Value)) {40
oListItem.Selected = true;41
((CheckBox)oDataListItem.FindControl("opt")).Checked = true;42
}43
}44
}45
} 46

47
48

49
//得到checkboxlist的值50
protected void purviewEdit()51
{52
if (string.IsNullOrEmpty(employee.Value))53
return;54

55
string resultValue = string.Empty;56
foreach (DataListItem oDataListItem in Menu.Items)57
{58
if (((CheckBox)oDataListItem.FindControl("opt")).Checked)59
{60
foreach (ListItem item in ((CheckBoxList)oDataListItem.FindControl("two_menu")).Items)61
{62
if (item.Selected)63
resultValue += item.Value + ",";64
}65
}66
}67
//resultValue.TrimEnd(new char[] { ',' });68
if (!org_employees.UpdateOrg_UserRecords("Org_UserRecords.purview", resultValue.TrimEnd(new char[] { ',' }), int.Parse(employee.Value)))69
lblRurviewError.InnerHtml = "An update error about purview";70
}71

js 全选 [没有反选]
//根据opt判断全选
//注意获取父级node 然后判断全选
//<javascript>
function onCheck(obj){
var nextNode = obj.parentNode;
var cb = nextNode.getElementsByTagName("input");
for(var i=0;i<cb.length;i++){
if(cb[i].type == "checkbox"){
if(obj.checked){
cb[i].checked = true;
}
else {
cb[i].checked = false;
}
}
}
}
// haha . 很喜欢这个效果.
function resetOpt(obj) {
var e = obj.getElementsByTagName("input");
var choose = false;
for(var i=0;i<e.length;i++)
{
if(e[i].type == "checkbox")
{
if(e[i].checked){
choose = true;
}
}
}
if(choose){
obj.parentNode.parentNode.getElementsByTagName("input")[0].checked = true;
}
else {
obj.parentNode.parentNode.getElementsByTagName("input")[0].checked = false;
}
}


浙公网安备 33010602011771号