Here, the TreeView control of asp.net2.0 is used in combination with JavaScript to implement some functions of the permission tree.
Assume that there are the following three rules in the permission tree:
1. If the node is accessible, its parent node must also be accessible;
2. If the node is accessible, its child nodes can also be accessed;
3. If the node is inaccessible, its child nodes are also inaccessible.
The code is as follows:
//Get the parent element of the specified tagName
function public_GetParentByTagName(element, tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//If this element is not the desired tag, continue tracing up
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//Set the node's parent node Cheched - if the node is accessible, its parent node must also be accessible
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);
}
//Set the node's child node uncheched - if the node is inaccessible, its child nodes will also be inaccessible.
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);
}
}
//Set the node's child node cheched - if the node can be accessed, its child nodes can also be accessed
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);
}
}
//trigger event
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);
}
}
Then bind the TreeView to the js event in the page_load event:
this.TreeView1.Attributes.Add("onclick", "CheckEvent()");