The child and parent nodes of the Treeview are selected in cascade mode, traverse the Treeview, and search for and select nodes.

Source: Internet
Author: User

Cascade the child and parent nodes of the Treeview

Reference: (child node and parent node cascading selection of Treeview) http://blog.sina.com.cn/s/blog_56616d970100c483.html

(Treeview click Text select and click parent node select subnode) http://hi.baidu.com/lhlsxdx/blog/item/44988f8f95f006e4f01f3691.html

(C # traverse Treeview and find and select nodes) http://apps.hi.baidu.com/share/detail/616013

Aspx:

<HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Background code:

Protected void page_load (Object sender, eventargs e) {If (! Ispostback) {This. treeview1.attributes. add ("onclick", "postbackbyobject ()"); treeview1.attributes. add ("onselectednodechanged", "treeviewincluselectednodechanged"); BIND () ;}} void BIND () {list <contactgroupdomain> nodes = (list <contactgroupdomain>) helper. contactgroup (). getallgroup (UID); If (null = nodes) {return ;}for (INT I = 0; I <nodes. count; I ++) // cyclically {treenode node = new treenode (); node. value = nodes [I]. cgid; node. TEXT = nodes [I]. cgname; bindchildnode (node); treeview1.nodes. add (node) ;}} void bindchildnode (treenode node) {list <contactpersondomain> personlist = (list <contactpersondomain>) helper. contactperson (). getcontactbygid (UID, node. value); For (INT I = 0; I <personlist. count; I ++) {treenode childnode = new treenode (); childnode. value = personlist [I]. CPID; childnode. TEXT = personlist [I]. cpname; node. childnodes. add (childnode );}}

Protected void treeviewinclutreenodecheckchanged (Object sender, treenodeeventargs e) {setchildchecked (E. node); setparentchecked (E. node );}

/// <Summary> /// select the parent node, all its subnodes are also selected /// </Summary> /// <Param name = "parentnode"> </param> private void setchildchecked (treenode parentnode) {If (parentnode. checked = true) {foreach (treenode tn in parentnode. childnodes) {tn. checked = parentnode. checked; If (TN. childnodes. count> 0) {setchildchecked (TN) ;}} else {foreach (treenode tn in parentnode. childnodes) {tn. checked = parentnode. checked; If (TN. childnodes. count> 0) {setchildchecked (TN );}}}}

/// <Summary> /// select the subnode, parent node is also selected /// </Summary> /// <Param name = "childnode"> </param> private void setparentchecked (treenode childnode) {If (childnode. checked = true) {If (childnode. parent! = NULL) {childnode. Parent. Checked = childnode. Checked; setparentchecked (childnode. Parent) ;}} else {If (childnode. parent! = NULL) {If (! Checkchildallcheck (childnode. Parent) {childnode. Parent. Checked = childnode. Checked; setparentchecked (childnode. Parent );}}}}

/// <Summary> /// check whether the sibling node of the current node and Its subnodes are selected. If yes, true is returned directly. // If NO, continue recursion. /// </Summary> /// <Param name = "Node"> </param> /// <returns> </returns> private bool checkchildallcheck (treenode node) {bool ischeck = false; foreach (treenode childnode in node. childnodes) {If (childnode. checked) {return true;} else {ischeck = checkchildallcheck (childnode) ;}} return ischeck ;}

Protected void treeviewincluselectednodechanged (Object sender, eventargs e) {treenode = treeview1.selectednode ;}

The object set in the program is changed according to your actual needs.

:


Select "Level 3 Node 1", and all its parent nodes will be selected.

 

 

C # traverse the Treeview and find and select nodes

First, let's take a look at the sample program on msdn: print all the node names in a tree private void printrecursive (treenode) {// print the node. system. diagnostics. debug. writeline (treenode. text); MessageBox. show (treenode. text); // print each node recursively. foreach (treenode tn in treenode. nodes) {printrecursive (TN) ;}// call the procedure using the Treeview. private void callrecursive (Treeview) {// print each node recursively. treenodecollection nodes = Treeview. nodes; foreach (treenode N in nodes) {printrecursive (n );}}

The relationship between the Treeview class and the treenode class is described as follows: a read-only attribute in the Treeview class is nodes, which belongs to the treenodecollection type, the nodes attribute of a Treeview is the collection of the root node of the Treeview.

Then, I recursively traverse the method of searching for a tree node (because the program needs to search for it based on the imageindex attribute of the Tree node ):

Private treenode findtreenode (INT imageindex, treenode tnparent) {If (tnparent = NULL) return NULL; If (tnparent. imageindex = imageindex) return tnparent; treenode tnret = NULL; foreach (treenode tn in tnparent. nodes) {tnret = findtreenode (imageindex, TN); If (tnret! = NULL) break;} return tnret;} private treenode callfindnode (INT imageindex, Treeview) {treenodecollection nodes = Treeview. nodes; foreach (treenode N in nodes) {treenode temp = findtreenode (imageindex, n); If (temp! = NULL) return temp;} return NULL;} // <summary> // This Is A click event, with the selected node changed, /// when the selected node is changed, the selectednodechanged event is triggered /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void mycontroltransactions_addcustomer (Object sender, eventargs e) {treenode temp = callfindnode (3, treeview1); If (temp! = NULL) treeview1.selectednode = temp ;}

 

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.