Select or cancel all the Treeview controls of ASP. NET.

Source: Internet
Author: User

<Script language = "JavaScript">
/*
Function Name: checknode (currentnode), parentnode (currentnode), childnode (currentnode)
Author: Chang Dongfang
Created on:
Function: implements the Treeview with checkbox.
1. Select all child nodes of the parent node.
2. After the selection of all child nodes is canceled, the selection of the parent node is also canceled.
Usage: 1. Add checknode (currentnode), parentnode (currentnode), and childnode (currentnode) in the middle of
2. Add yourtreeview. Attribute. Add ("oncheck", "checknode (yourtreeview. gettreenode (yourtreeview. clickednodeindex)") to the *. aspx. CS page_load () event ))")

*/


// Recursively traverse all subnodes
Function checknode (currentnode)
{
VaR childnode = new array ();
VaR parentnodechild = new array ();
VaR ischecked;
Childnode = currentnode. getchildren ();
If (currentnode. getattribute ('checked '))
{
Ischecked = true;
}
Else
{
Ischecked = false;
}
// Process the parent node
If (currentnode. getparent ()! = NULL)
{
// Select and Process
If (currentnode. getattribute ('checked '))
{
Parentnode (currentnode );
}
Else
// Deselect
{
Childnode (currentnode );
}
}
Else
{
// Do nothing
}
// Subnode Processing
If (childnode. length> 0)
{
For (VAR I = 0; I <childnode. length; I ++)
{
Childnode. setattribute ("checked", ischecked );
If (childnode. getchildren (). length> 0)
{
Checknode (childnode );
}
}
}

}
// Recursively select the parent node
Function parentnode (currentnode)
{
If (currentnode. getparent ()! = NULL)
{
Currentnode. getparent (). setattribute ('checked', true );
// Recursively call parentnode (currentnode) to traverse the parent node of the previous Layer
Parentnode (currentnode. getparent ());
}
}
// Recursively deselect the parent node
Function childnode (currentnode)
{
If (currentnode. getparent ()! = NULL)
{
VaR checkedcount = 0;
VaR childnode = currentnode. getparent (). getchildren ();
For (VAR I = 0; I <childnode. length; I ++)
{
If (childnode. getattribute ('checked '))
{
Checkedcount ++;
}
}
If (checkedcount = 0)
{
Currentnode. getparent (). setattribute ('checked', false );
}
// Recursively call childnode (currentnode) to traverse the parent node of the previous Layer
Childnode (currentnode. getparent ());
}
}

</SCRIPT>

How does JS recursively traverse all subnodes of a node in the Treeview
VaR allrootnode = new array ();
Allrootnode = treeview1.getchildren ();
Alertnode (allrootnode );

Function alertnode (nodearray)
{
If (parseint (nodearray. Length) = 0)
Return;
Else
{
For (I = 0; I <nodearray. length; I ++)
{
VaR cNode;
CNode = nodearray;
Alert (cNode. getattribute ("text "));
If (parseint (cNode. getchildren (). Length )! = 0)
Alertnode (cNode. getchildren ());
}
}
}

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.