Checkbox operations in ASP. net2.0 permission tree

Source: Internet
Author: User

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 ()");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.