1. 选中所有子节点:
function OnTreeNodeChecked()
{
var ele = window.event.srcElement;
if(ele.type=='checkbox')
{
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
if(div==null)return;
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
checkBoxs[i].checked=ele.checked;
}
}
}
2. 如果要选中子节点又同时选中所有父节点:
function getParentByTagName(element,tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
function setParentChecked(objNode)
{
var objParentDiv = getParentByTagName(objNode,"div");
if(objParentDiv == null || objParentDiv == "undefined")
return;
var objID = objParentDiv.getAttribute("ID");
var objParentCheckBox = document.getElementById(objID.replace("Nodes","CheckBox"));
if(objParentCheckBox == null || objParentCheckBox == "undefined")
return;
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
function setChildCheckedState(div,state)
{
var objchild = div.childNodes;
var count = objchild.length;
for(var i=0;i<objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = state;
}
setChildCheckedState(tempObj,state);
}
}
function TreeNodeChecked()
{
var objNode = window.event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
return;
if(objNode.checked == true)
{
setParentChecked(objNode);
}
var objID = objNode.getAttribute("ID");
var objParentDiv = document.getElementById(objID.replace("CheckBox","Nodes"));
if(objParentDiv==null || typeof(objParentDiv) == "undefined")
return;
setChildCheckedState(objParentDiv,objNode.checked);
}
后台的给treeview添加onclick事件处理:
m_Tree.Attributes.Add("onclick", "TreeNodeChecked();");
3.每次只选中一个节点,相对与互斥,这个很好实现
function TreeNodeChecked()
{
var objNode = window.event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type !="checkbox")
return;
if(window.lastCheckNode != null)
window.lastCheckNode.checked = false;
window.lastCheckNode = objNode;
}
另一种:function OnTreeNodeChecked()
{
var ele = window.event.srcElement;
if(ele.type=='checkbox')
{
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
if(div==null)return;
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++)
{
if(checkBoxs[i].type=='checkbox')
checkBoxs[i].checked=ele.checked;
}
}
}
2. 如果要选中子节点又同时选中所有父节点:
function getParentByTagName(element,tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
function setParentChecked(objNode)
{
var objParentDiv = getParentByTagName(objNode,"div");
if(objParentDiv == null || objParentDiv == "undefined")
return;
var objID = objParentDiv.getAttribute("ID");
var objParentCheckBox = document.getElementById(objID.replace("Nodes","CheckBox"));
if(objParentCheckBox == null || objParentCheckBox == "undefined")
return;
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
function setChildCheckedState(div,state)
{
var objchild = div.childNodes;
var count = objchild.length;
for(var i=0;i<objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = state;
}
setChildCheckedState(tempObj,state);
}
}
function TreeNodeChecked()
{
var objNode = window.event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
return;
if(objNode.checked == true)
{
setParentChecked(objNode);
}
var objID = objNode.getAttribute("ID");
var objParentDiv = document.getElementById(objID.replace("CheckBox","Nodes"));
if(objParentDiv==null || typeof(objParentDiv) == "undefined")
return;
setChildCheckedState(objParentDiv,objNode.checked);
}
后台的给treeview添加onclick事件处理:
m_Tree.Attributes.Add("onclick", "TreeNodeChecked();");
3.每次只选中一个节点,相对与互斥,这个很好实现
function TreeNodeChecked()
{
var objNode = window.event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type !="checkbox")
return;
if(window.lastCheckNode != null)
window.lastCheckNode.checked = false;
window.lastCheckNode = objNode;
}
所用的javascript代码:
<script type="text/javascript">
function public_GetParentByTagName(element, tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode)
{
var objParentDiv = public_GetParentByTagName(objNode,"div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("Nodes"));
objID = objID+"CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = false;
}
setChildUnChecked(tempObj);
}
}
//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = true;
}
setChildChecked(tempObj);
}
}
//触发事件
function CheckEvent()
{
var objNode = event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
return;
if(objNode.checked==true)
{
setParentChecked(objNode);
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
setChildChecked(objParentDiv);
}
else
{
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
setChildUnChecked(objParentDiv);
}
}
</script>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.TreeView1.Attributes.Add("onclick", "CheckEvent()");
}
}
<script type="text/javascript">
function public_GetParentByTagName(element, tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode)
{
var objParentDiv = public_GetParentByTagName(objNode,"div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("Nodes"));
objID = objID+"CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = false;
}
setChildUnChecked(tempObj);
}
}
//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName=="INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = true;
}
setChildChecked(tempObj);
}
}
//触发事件
function CheckEvent()
{
var objNode = event.srcElement;
if(objNode.tagName!="INPUT" || objNode.type!="checkbox")
return;
if(objNode.checked==true)
{
setParentChecked(objNode);
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
setChildChecked(objParentDiv);
}
else
{
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+"Nodes");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
setChildUnChecked(objParentDiv);
}
}
</script>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.TreeView1.Attributes.Add("onclick", "CheckEvent()");
}
}