Asp.net| Control
When doing permissions, the main implementation of the following functions
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 use of the number of checkbox with a good user experience, but the difficulty of programming is also a bit increased, if all have a service to complete, that point under the brush, certainly not, can only use Javascript,javascript debugging when depressed very, a word class, And a depressing recursion, trouble.
I used to use the following method
<script language= "JavaScript" >
<!--
Initializing nodes
Initchecknode (document.all ("Tvitemclientid"). Value,document.all ("Checked"). Value)
Initialize the selected node
function Initchecknode (tree,tvvalue)
{
Gets the string of nodes that need to initialize the selection state.
var selectednodes = Tvvalue;
var arraynodes = new Array ();
Arraynodes = Selectednodes.split (",");
var allrootnode=new Array ();
Allrootnode=document.getelementbyid (tree). GetChildren ();
Initializes the selected node
Findandcheckednodes (Allrootnode,arraynodes);
}
Initializes the selected node based on the string of the selected node
function Findandcheckednodes (Nodearray,nodedatas)
{
alert (nodearray.length);
if (parseint (nodearray.length) ==0)
{
Return
}
Else
{
For (Var i=0;i<parseint (nodearray.length); i++)
{
var cnode,nodedata;
Cnode=nodearray[i];
Initializes checked = True if the node is in Nodedatas;
Nodedata = Cnode.getattribute ("Nodedata");
for (Var j=0;j<nodedatas.length;j++)
{
if (nodedatas[j] = = nodedata)
{
Cnode.setattribute ("Checked", "true");
Break
}
}
If there are child nodes, continue to recursively
if (parseint (Cnode.getchildren (). Length)!=0)
Findandcheckednodes (Cnode.getchildren (), Nodedatas);
}
}
}
-->
Oncheck Events
function Tree_oncheck (tree)
{
var i;
var node=tree.gettreenode (Tree.clickednodeindex);
var pchecked=tree.gettreenode (Tree.clickednodeindex). getattribute ("checked");
SetCheck (node,pchecked);
SetParent (node,pchecked);
Window.alert (pchecked);
Document.all.checked.value = "";
if (Tree.getchildren (). length > 0)
{
For (I=0;i<=tree.getchildren (). length-1;i++)
{
if (Tree.getchildren () [I].getattribute ("Checked"))
{
Addchecked (Tree.getchildren () [i]);
}
Findcheckedfromnode (Tree.getchildren () [i]);
}
}
}
Set child node selected
function SetCheck (node,pchecked)
{
var i;
var childnode=new Array ();
Childnode=node.getchildren ();
if (parseint (childnode.length) ==0)
{
Return
}
Else
{
for (i=0;i<childnode.length;i++)
{
var Cnode;
Cnode=childnode[i];
Cnode.setattribute ("Checked", pchecked);
cnode.checked = pchecked;
if (parseint (Cnode.getchildren (). Length)!=0)
{
SetCheck (cnode,pchecked);
}
}
}
}
Set child nodes checked/canceled;
You also need to set the state of the parent node (if it is unchecked, just set this node and all its word contacts, not the parent contact)
function SetParent (NODE,PC)
{
var parentnode = node.getparent ();
if (parentnode)
{
var parentnodefather = parentnode.getparent ();
if (Parentnodefather)
{
SetParent (PARENTNODE,PC);
}
if (Pc)
{Parentnode.setattribute ("checked", Pc);}
Else
{
Checkbrother (Parentnode,pc,node.getattribute ("Nodedata"))
}
}
}
Check that the child contact has a selection, and if there is a choice, return true
Check only the first level nodes.
function Checkbrother (parentnode,pc,nodedata)
{
var childnodes = new Array ();
ChildNodes = Parentnode.getchildren ();
if (Childnodes.length >0)
{
var bchecked = true;
for (Var i=0;i<childnodes.length;i++)
{
if (Childnodes[i].getattribute ("checked") = = True && childnodes[i].getattribute ("Nodedata")!= Nodedata)
{
Alert (I+childnodes[i].getattribute ("Text"));
bchecked = false;
Break
}
}
if (bchecked)
{
Parentnode.setattribute ("Checked", Pc);
}
Else
{
All parent Node Selections
SetParent (parentnode,! PC)
}
}
Else
{
Parentnode.setattribute ("Checked", Pc);
}
}
//Get all node states
function Findcheckedfromnode (node)
{
var i = 0;
var nodes = new Array ();
nodes = Node.getchildren ();
for (i = 0; I <= nodes.length-1; i++)
{
if (nodes[i].getatt Ribute ("Checked"))
{
addchecked ( Nodes[i]);
}
if (parseint (Nodes[i].getchildren (). Length)!= 0)
{
Findcheckedfromnode (Nodes[i]);
}
}
}
///Add selected node
function addchecked (node)
{
document.all.checked.value = Node.getattribute ("Nodedata") + ",";
}
-->
</script> This method has a big problem, is that his client-side checkbox is not available on the server, and now can only traverse the tree at check time, and put the value of checked in a text, and then submitted to the server, The server then resolves the 1@2@ character.
Now I'm using asp.net2.0, and I'm using the following methods
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);
}
The biggest advantage of this approach is that the server can get JavaScript set checked value, do not have to point out all the trees, directly in the service to the end of the time on the line
Traversing child nodes
public void Getchildnode (TreeNode Node)
{
foreach (TreeNode node in node.childnodes)
{
if (node. Checked)
{
strchecked = node. Value+ "@";
}
Getchildnode (node);
}
}
I can still get the value of its check