Share: A small usage of the open-source control Treeview provided by Microsoft

Source: Internet
Author: User

Treeview-an open-source control provided by Microsoft, which must have been used by everyone. It is really good, but there are a few intolerable features that are not provided. I will mention one of them today;

I remember a user on csdn asked me some time ago how to obtain information about the currently selected node of the Treeview. At that time, I was confused, after you get the ID of the selected node, you can directly retrieve it from the background database. This problem also occurs today. It doesn't work in that way. Haha, I apologize to the old man and want to come and try again, I think there is always no way. Later I found that the selectednodeindex attribute is "0.1.0 "......, It just reminds me if I can do something on it.ArticleThe selected node is actually obtained in this way. For example, if selectednodeindex is "0.1.0", it is actually TV. nodes [0]. nodes [1]. nodes [0 ].AlgorithmIt's easy. In fact, the key part of the algorithm is to analyze selectednodeindex and control the Loop Based on it, but it seems that the same method has not been found on the Internet (maybe I did not look for it carefully, haha ), below isCode:

// Get current node
Private Microsoft. Web. UI. webcontrols. treenode getselectednode (Treeview TV)
{
String Strindex = TV. selectednodeindex;

String [] Strarray = Strindex. Split ( New   Char [] {'.'} );

Int Icount = Strarray. length;

Treenode TN = TV. nodes [convert. toint32 (strarray [ 0 ])];

// Note that when the selected contact is the root node (selectednodeindex = "0,
// The loop condition will not be met and the loop will not be executed
For ( Int I = 1 ; I <= Icount - 1 ; I ++ )
{
TN=Getnextnode (TN, strarray [I]);
}

Return TN;
}

// Obtain the parent node of the current node. Why not run it once? Pai_^
Private Microsoft. Web. UI. webcontrols. treenode getselectednodeparent (Treeview TV)
{
String Strindex = TV. selectednodeindex;

String [] Strarray = Strindex. Split ( New   Char [] {'.'} );

Int Icount = Strarray. length;

Treenode TN = TV. nodes [convert. toint32 (strarray [ 0 ])];

// Note that when the selected contact is the root node (selectednodeindex = "0,
// The loop condition will not be met and the loop will not be executed
For ( Int I = 1 ; I <= Icount - 2 ; I ++ )
{
TN=Getnextnode (TN, strarray [I]);
}

Return TN;
}

Private Microsoft. Web. UI. webcontrols. treenode getnextnode (treenode TN, String Index)
{
IntIindex=Convert. toint32 (INDEX );
ReturnTn. nodes [iindex];
}

Since the current node can be retrieved, what information cannot be obtained?

The following is the Demo code: Private   Void Btngetinfo_click ( Object Sender, system. eventargs E)
{
String Strnodeinfo =   String . Empty;

// Get current node
Treenode TN = Getselectednode (tvdemo );

Strnodeinfo + =   " The ID of the current node is " + Tn. ID + " " ;
Strnodeinfo + =   " <Br> the text of the current node is" " + Tn. Text + " " " ;

Lcurrentinfo. Text = Strnodeinfo;
}

Private   Void Button#click ( Object Sender, system. eventargs E)
{
String Strnodeinfo =   String . Empty;

// Get current node
Treenode TN = Getselectednodeparent (tvdemo );

Strnodeinfo + =   " The ID of the parent node of the current node is " + Tn. ID + " " ;
Strnodeinfo + =   " <Br> the text of the parent node of the current node is" " + Tn. Text + " " " ;

Lparentinfo. Text = Strnodeinfo;
}

The node ID structure is
A
A1
A101
A102
A103
A2
A3

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.