Here, some functions of the permission tree are implemented using the Treeview control of ASP. net2.0 combined with JavaScript.
Assume that the permission tree has the following three rules:
1. If the node is accessible, its parent node must also be accessible;
2. If this node can be accessed, its sub-nodes can also be accessed;
3. If the node is not accessible, its sub-nodes cannot be accessed.
CodeAs follows:
// Obtain the parent element of the specified tagname of an element.
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 parent;
}
// set the node's parent node cheched -- this node is accessible, and its parent node must also be accessible
function setparentchecked (objnode)
{< br> var objparentdiv = public_getparentbytagname (objnode, "Div");
If (objparentdiv = NULL | objparentdiv = "undefined ")
{< br> return;
}< br> var objid = objparentdiv. getattribute ("ID");
objid = objid. substring (0, objid. indexof ("nodes");
objid = objid + "checkbox";
var objparentcheckbox = EN en T. getelementbyid (objid);
If (objparentcheckbox = NULL | objparentcheckbox = "undefined")
{< br> return;
}< br> If (objparentcheckbox. tagname! = "Input" & objparentcheckbox. type = "checkbox")
return;
objparentcheckbox. checked = true;
setparentchecked (objparentcheckbox);
}
// Set the node's subnode uncheched -- this node is not accessible, and its subnodes are not accessible.
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 subnode cheched -- this node can be accessed, and its subnodes 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 the 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 ()");