This uses the Asp.net2.0 TreeView control to implement some of the permissions tree's functions in conjunction with JavaScript.
Suppose there are three rules in the permission tree:
1, the node can be accessed, then his parent node will also be able to access;
2, the node can be accessed, then his child nodes can also be accessed;
3, the node is inaccessible, and his child nodes are inaccessible.
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 this element is not the desired tag, continue to trace
while (Parent && (Parent.tagName.toUpperCase ()!= uppertagname))
{
Parent = Parent.parentnode? Parent.parentNode:parent.parentElement;
}
return to parent;
}
Set the parent node of the node cheched--the node is accessible, his parent node will 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);
}
//Set child nodes of a node uncheched--the node is inaccessible, his child nodes cannot access
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 child nodes of a node cheched--the node can be accessed, and his child nodes 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);
}
}
Triggering events
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 ()");