Asp.net operation Treeview

Source: Internet
Author: User

The younger brother was very clumsy in writing. If you have any experts, please give me some advice and let the younger brother learn and learn...

 

Load tree -- use recursion 

Protected void loadchannel ()
{
Datatable dt = bllchannel. getlist ();

Datarow [] parent = DT. Select ("parentid = 0 ");
Foreach (datarow parentrow in parent)
{
Treenode node = new treenode (parentrow ["title"]. tostring (), parentrow ["ID"]. tostring ());
Node. tooltip = parentrow ["title"]. tostring () + "=>" + parentrow ["ID"]. tostring (); // This is to use js to get the node's text and value.
Bindtree (DT, node, parentrow ["ID"]. tostring ());
Treeview1.nodes. Add (node );
}
}
Void bindtree (datatable DT, treenode node, string parentid)
{
Datarow [] ROW = DT. Select ("parentid =" + parentid );
Foreach (datarow R in row)
{
Treenode n = new treenode (R ["title"]. tostring (), R ["ID"]. tostring ());
N. tooltip = R ["title"]. tostring () + "=>" + R ["ID"]. tostring ();
Node. childnodes. Add (N );
Bindtree (DT, N, R ["ID"]. tostring ());
}
}

 

JS to get the value of the selected node and put it into two arrays respectively

 

VaR textarr = new array ();
VaR valuearr = new array ();
$ ("Input [type = 'checkbox']: checked"). Each (function (){
VaR TV = $ (this). ATTR ("title"). Split ("=>"); // The tooltip value above is very earthy.
Textarr. Push (TV [0]);
Valuearr. Push (TV [1]);
});

// Use join to convert the array into a string separated by "|" and fill in two text fields respectively.

$ Get ("catanam"). setattribute ("value", textarr. Join ("| "));
$ Get ("cataid"). setattribute ("value", valuearr. Join ("| "));

 

 

// When loading, use the recursive traversal tree to select the matching values

{

String [] cataidarray = row ["categoryid"]. tostring (). split (New String [] {"|"}, stringsplitoptions. removeemptyentries); // separate the values inserted into the database.
Checknode (treeview1.nodes, cataidarray );

}

// Recursive Method

Void checknode (treenodecollection tnodes, string [] cataidarray)
{
Foreach (treenode tn in tnodes)
{
For (INT I = 0; I <cataidarray. length; I ++)
{
If (TN. value = cataidarray. getvalue (I). tostring ())
{
Tn. Checked = true;
}
}
Checknode (TN. childnodes, cataidarray );
}
}

I beg the experts to help my younger brother give me some advice, because if there is a large amount of data, I am afraid I will not be able to afford it ....

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.