The operation of Checkbox in the Asp.Net2.0 permission tree
- 2020-05-05 11:08:23
- OfStack
The TreeView control of asp.net2.0 is used here in conjunction with JavaScript to implement part of the privilege tree.
Suppose there are three rules in the permission tree:
1. If the node can be accessed, its parent node must also be accessed;
2. If the node can be accessed, its children can also be accessed;
3. If the node is not accessible, its children cannot be accessed either.
The code is as follows:
// gets the parent element
of the element specified tagName
function public_GetParentByTagName(element, tagName)
{
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
// if the element is not the desired tag, continue to
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
// set the parent node Cheched of the node -- if this node is accessible, its parent must also be able to access
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);
}
// sets the child node uncheched of the node -- if this node is not accessible, his child node cannot access
either
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);
}
}
// sets the node's child node cheched -- if this node can access
, all of its children can also access
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 TreeView to js in the page_load event:
this.TreeView1.Attributes.Add("onclick", "CheckEvent()");