Code
// JScript 文件
function OnTreeNodeChecked(treeClientID)
{
var ele = event.srcElement;
if (ele.tagName=='A')
{
var id;
id = ele.id.replace(treeClientID,'').replace('t','');
ele = document.getElementById(treeClientID+'n'+id+'CheckBox');
ele.checked = ! ele.checked;
}
else if (ele.tagName=='IMG' && ele.parentNode.tagName=='A')
{
var id;
id = ele.parentNode.id.replace(treeClientID,'').replace('t','').replace('i','');
if (id.substring(0,1)=='n')
{
return;
}
ele = document.getElementById(treeClientID+'n'+id+'CheckBox');
ele.checked = ! ele.checked;
}
if(ele.type=='checkbox')
{
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
//设置子类复选框
if(div != null)
{
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox' && ele.checked)
checkBoxs[i].checked=ele.checked;
}
}
//设置父类复选框,未选中则设置父类复选框也未选中
if (!ele.checked)
{
var arrIDColl = GetIDCollByCheckBox(ele,treeClientID);
var div = document.getElementById(treeClientID);
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
{
var arrSubIDColl = GetIDCollByCheckBox(checkBoxs[i],treeClientID);
if (arrIDColl.contains(arrSubIDColl[arrSubIDColl.length-1]))
{
checkBoxs[i].checked=ele.checked;
}
}
}
}
//设置父类复选框,如子类复选框都全中,则选中父类复选框
else
{
SelectParentCheckBox(ele,treeClientID);
}
}
}
function SelectParentCheckBox(checkBox,treeClientID)
{
var arrIDColl = GetIDCollByCheckBox(checkBox,treeClientID);
//编号集合代表根节点,没有父复选框
if (arrIDColl.length<2)
return;
var div = document.getElementById(treeClientID);
//父类编号
var parentID = arrIDColl[arrIDColl.length-2];
//根据父类编号遍历所有子复选框
var checkBoxs = div.getElementsByTagName('INPUT');
//标记子类是否全选
var flagQuanXuan = true;
//顺便遍历出父类复选框
var parentCheckBox;
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
{
var arrSubIDColl = GetIDCollByCheckBox(checkBoxs[i],treeClientID);
//获得父类复选框
if (arrSubIDColl[arrSubIDColl.length-1]==parentID)
{
parentCheckBox = checkBoxs[i];
}
if (arrSubIDColl.length<2)
continue;
if (arrSubIDColl[arrSubIDColl.length-2]==parentID)
{
if (!checkBoxs[i].checked)
{
flagQuanXuan = false;
break;
}
}
}
}
parentCheckBox.checked = flagQuanXuan;
SelectParentCheckBox(parentCheckBox,treeClientID);
}
function GetIDCollByCheckBox(checkBox,treeClientID)
{
var id;
id = checkBox.id.replace('CheckBox','').replace(treeClientID,'').replace('n','');
var hrefid = treeClientID+'t'+id;
var hrefHTML = document.getElementById(hrefid).href;
var parentid = hrefHTML.replace("javascript:__doPostBack('"+treeClientID+"','","").replace("')","").replace("s","");
var arrParentID = parentid.split("\\\\");
return arrParentID;
}
// JScript 文件
function OnTreeNodeChecked(treeClientID)
{
var ele = event.srcElement;
if (ele.tagName=='A')
{
var id;
id = ele.id.replace(treeClientID,'').replace('t','');
ele = document.getElementById(treeClientID+'n'+id+'CheckBox');
ele.checked = ! ele.checked;
}
else if (ele.tagName=='IMG' && ele.parentNode.tagName=='A')
{
var id;
id = ele.parentNode.id.replace(treeClientID,'').replace('t','').replace('i','');
if (id.substring(0,1)=='n')
{
return;
}
ele = document.getElementById(treeClientID+'n'+id+'CheckBox');
ele.checked = ! ele.checked;
}
if(ele.type=='checkbox')
{
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
//设置子类复选框
if(div != null)
{
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox' && ele.checked)
checkBoxs[i].checked=ele.checked;
}
}
//设置父类复选框,未选中则设置父类复选框也未选中
if (!ele.checked)
{
var arrIDColl = GetIDCollByCheckBox(ele,treeClientID);
var div = document.getElementById(treeClientID);
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
{
var arrSubIDColl = GetIDCollByCheckBox(checkBoxs[i],treeClientID);
if (arrIDColl.contains(arrSubIDColl[arrSubIDColl.length-1]))
{
checkBoxs[i].checked=ele.checked;
}
}
}
}
//设置父类复选框,如子类复选框都全中,则选中父类复选框
else
{
SelectParentCheckBox(ele,treeClientID);
}
}
}
function SelectParentCheckBox(checkBox,treeClientID)
{
var arrIDColl = GetIDCollByCheckBox(checkBox,treeClientID);
//编号集合代表根节点,没有父复选框
if (arrIDColl.length<2)
return;
var div = document.getElementById(treeClientID);
//父类编号
var parentID = arrIDColl[arrIDColl.length-2];
//根据父类编号遍历所有子复选框
var checkBoxs = div.getElementsByTagName('INPUT');
//标记子类是否全选
var flagQuanXuan = true;
//顺便遍历出父类复选框
var parentCheckBox;
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
{
var arrSubIDColl = GetIDCollByCheckBox(checkBoxs[i],treeClientID);
//获得父类复选框
if (arrSubIDColl[arrSubIDColl.length-1]==parentID)
{
parentCheckBox = checkBoxs[i];
}
if (arrSubIDColl.length<2)
continue;
if (arrSubIDColl[arrSubIDColl.length-2]==parentID)
{
if (!checkBoxs[i].checked)
{
flagQuanXuan = false;
break;
}
}
}
}
parentCheckBox.checked = flagQuanXuan;
SelectParentCheckBox(parentCheckBox,treeClientID);
}
function GetIDCollByCheckBox(checkBox,treeClientID)
{
var id;
id = checkBox.id.replace('CheckBox','').replace(treeClientID,'').replace('n','');
var hrefid = treeClientID+'t'+id;
var hrefHTML = document.getElementById(hrefid).href;
var parentid = hrefHTML.replace("javascript:__doPostBack('"+treeClientID+"','","").replace("')","").replace("s","");
var arrParentID = parentid.split("\\\\");
return arrParentID;
}
调用方式:
例:treeCategory为TreeView的ID号
this.treeCategory.Attributes.Add("onclick", "OnTreeNodeChecked('"+treeCategory.ClientID+"');");