ASP: When you click a node in the Treeview, the child node is automatically selected (or not selected) and the parent node is selected.

Source: Internet
Author: User

About ASP: When Treeview selects a parent node, it automatically selects all subnodes. There are many methods on the Internet. The following is a method:

< 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 ) Return ;
VaR Checkboxs = Div. getelementsbytagname ( ' Input ' );
For ( VaR I =   0 ; I < Checkboxs. length; I ++ ){
If (Checkboxs [I]. Type =   ' Checkbox ' )
Checkboxs [I]. Checked = Ele. checked;
}
}
}
< / SCRIPT>

 

There is also a slightly complex requirement below: When a Treeview selects any node, if there are subnodes, it will automatically select (or all are not selected) The subnode, at the same time, determine whether or not to select the parent node or grandfather node. If a child node under the parent node is selected, select the parent node as well. For example, if a sub-menu is selected during menu maintenance, the parent menu must be selected. Otherwise, the parent menu cannot be seen. How can this problem be solved? If you select the corp node, you must automatically select all the above three layers.

Implemented JSCodeAs follows:

The code is mainly added with the bold part: (three layers are judged up)

< 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 ){

}
Else {
VaR Checkboxs = Div. getelementsbytagname ( ' Input ' );
For ( VaR I =   0 ; I < Checkboxs. length; I ++ ){
If (Checkboxs [I]. Type =   ' Checkbox ' )
Checkboxs [I]. Checked = Ele. checked;
}
}
VaRParentdiv= Event. srcelement. parentelement. parentelement;
VaRParentparentdiv=
event. srcelement. parentelement. parentelement. parentelement. parentelement. parentelement. parentelement;
var parentparentparentdiv =
event. srcelement. parentelement. parentelement. parentelement. parentelement. parentelement. parentelement. parentelement;
If (parentdiv ! = null & parentdiv ! =
undefined) {
var parentcheckboxid = parentdiv. id. replace ( ' nodes ' , ' checkbox '
);
VaRCheckboxs=Parentdiv. getelementsbytagname ('Input'
);
VaRChildnodechecked= False
;< br> for ( var I = 0 ; I checkboxs. length; I ++ ){
If(Checkboxs [I]. Type= 'Checkbox' &&
Checkboxs [I]. Checked)
Childnodechecked= True
;
}
If(Document. getelementbyid (parentcheckboxid)! = Null
){
Document. getelementbyid (parentcheckboxid). Checked=
childnodechecked;
}< BR >}< br> If (parentparentdiv ! = null & parentparentdiv ! =
undefined) {
var parentparentcheckboxid = parentparentdiv. id. replace ( ' nodes ' , ' checkbox '
);
VaRCheckboxs=Parentparentdiv. getelementsbytagname ('Input'
);
VaRChildnodechecked= False
;< br> for ( var I = 0 ; I checkboxs. length; I ++ ){
If(Checkboxs [I]. Type= 'Checkbox' &&
Checkboxs [I]. Checked)
Childnodechecked= True
;< BR >}

If (document. getelementbyid (parentparentcheckboxid) ! = null ) {
document. getelementbyid (parentparentcheckboxid ). checked = childnodechecked;
}< BR >}

If (parentparentparentdiv ! = null & parentparentparentdiv ! = undefined) {
var parentparentparentcheckboxid = parentparentparentdiv. id. replace ( ' nodes ' , ' checkbox '
);
VaRCheckboxs=Parentparentparentdiv. getelementsbytagname ('Input'
);
VaRChildnodechecked= False
;< br> for ( var I = 0 ; I checkboxs. length; I ++ ){
If(Checkboxs [I]. Type= 'Checkbox' &&
Checkboxs [I]. Checked)
Childnodechecked= True
;
}

If(Document. getelementbyid (parentparentparentcheckboxid)! = Null ){
Document. getelementbyid (parentparentparentcheckboxid). Checked=
Childnodechecked;
}
}
}
}
< / SCRIPT>

 

 

 

 

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.