Using Javascript in ASP. net2.0 to select all checkboxes in Treeview

Source: Internet
Author: User
Implements full selection of the checkbox in the Treeview control. After the parent item in the menu selection box is checked, all the subitem selection boxes under the menu are checked. If one of the Child items is not checked, the parent item selection box is blank. For the main effect, see:

The main implementation principle is to use js to select all operations in the onclick event of the Treeview control.

<Script language = "JavaScript" type = "text/JavaScript">
Function ontreenodechecked ()
{
VaR ele = event. srcelement;
If (Ele. type = 'checkbox ')
{
VaR childrendivid = ELE. Id. Replace ('checkbox', 'nodes ');
VaR DIV = Document. getelementbyid (childrendivid );
If (Div! = NULL)
{
VaR checkboxs = div. getelementsbytagname ('input ');
For (VAR I = 0; I <checkboxs. length; I ++)
{
If (checkboxs [I]. type = 'checkbox ')
Checkboxs [I]. Checked = ELE. checked;
}
}
Else
{
VaR DIV = getparentbytagname (Ele, 'div ');
VaR checkboxs = div. getelementsbytagname ('input ');
VaR parentcheckboxid = div. Id. Replace ('nodes ', 'checkbox ');
VaR parentcheckbox = Document. getelementbyid (parentcheckboxid );
For (VAR I = 0; I <checkboxs. length; I ++)
{
If (checkboxs [I]. type = 'checkbox' & checkboxs [I]. Checked)
{
Parentcheckbox. Checked = true;
Return;
}
}
Parentcheckbox. Checked = false;
}

}
}

Function getparentbytagname (element, tagname)
{
VaR parent = element. parentnode;
VaR uppertagname = tagname. touppercase ();
While (parent & (parent. tagname. touppercase ()! = Uppertagname ))
{
Parent = parent. parentnode? Parent. parentnode: parent. parentelement;
}
Return parent;
}

</SCRIPT>

The rest is the onclick event bound to the Treeview control. <Asp: Treeview id = "treeview1" runat = "server" onclick = "ontreenodechecked () "showcheckboxes =" all "expanddepth =" 2 "width =" 100% "Height =" 100% ">
</ASP: Treeview>

So far, we have implemented all the problems of using js to implement full selection and partial selection of the Treeview control.

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.