Treeview (Classic recursive algorithm)

Source: Internet
Author: User
Front-end

  1. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "test_treeview.aspx.cs" inherits = "test_test_treeview" %>
  2. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head runat = "server">
  5. <Title> No title page </title>
  6. </Head>
  7. <Body>
  8. <Form ID = "form1" runat = "server">
  9. <Div>
  10. <Asp: Treeview id = "treeview1" runat = "server">
  11. </ASP: Treeview>
  12. </Div>
  13. </Form>
  14. </Body>
  15. </Html>

Background

  1. Using system;
  2. Using system. Data;
  3. Using system. configuration;
  4. Using system. collections;
  5. Using system. Web;
  6. Using system. Web. Security;
  7. Using system. Web. UI;
  8. Using system. Web. UI. webcontrols;
  9. Using system. Web. UI. webcontrols. webparts;
  10. Using system. Web. UI. htmlcontrols;
  11. Using system. Data. sqlclient;
  12. Public partial class test_test_treeview: system. Web. UI. Page
  13. {
  14. Private datatable tblsetorginfo = new datatable (); // store read data
  15. Protected void page_load (Object sender, eventargs E)
  16. {
  17. BIND ();
  18. }
  19. Private void BIND ()
  20. {
  21. String connstr = configurationmanager. connectionstrings ["hrmbookconnectionstring"]. tostring ();
  22. Sqlconnection conn = new sqlconnection (connstr );
  23. Try
  24. {
  25. Conn. open ();
  26. String sqlstr = "select internal Number, category, absindex, itemindex, itemlevel, parentindex, category number, unit number ,"
  27. + "Organization name, Pinyin code, Organization address, organization phone number, account bank, account, full name of account opening from organization code table order by absindex ";
  28. Sqldataadapter da = new sqldataadapter (sqlstr, Conn );
  29. Da. Fill (tblsetorginfo );
  30. Datatable tempdatatable = tblsetorginfo. Copy (); // save another copy of the Organization table as tempdatatable
  31. Dataview viewsetorginfo = new dataview (tempdatatable); // create a data view
  32. Viewsetorginfo. rowfilter = "itemlevel = 0 ";
  33. // Add all records in the dataset to the tree table one by one based on their relationships
  34. If (viewsetorginfo. Count> 0)
  35. {
  36. Foreach (datarowview myrow in viewsetorginfo)
  37. {
  38. String strenterprisename = myrow ["unit name"]. tostring (). Trim ();
  39. // Add the first node "Provincial Pharmaceutical Group"
  40. This. treeview1.nodes. Add (New treenode (strpolicisename ));
  41. // The initialization parameter here is the first node "Provincial/Pharmaceutical Group", and then this function recursively adds all subnodes
  42. Populatetreeview (strpolicisename, treeview1.nodes [0], myrow );
  43. // Treeview1.selectednode = treeview1.nodes [0]; // select the first node
  44. }
  45. }
  46. }
  47. Catch (exception ex)
  48. {
  49. Response. Write (ex. tostring ());
  50. }
  51. Finally
  52. {
  53. Conn. Close ();
  54. }
  55. }
  56. // --------------- Recursively call the input node information to add all nodes -----------------------
  57. Private void populatetreeview (string parentpart, treenode parentnode, datarowview parentrow)
  58. {
  59. String strenterprisename = "";
  60. Datatable tempdatatable = tblsetorginfo. Copy ();
  61. Dataview viewsetorginfo = new dataview (tempdatatable );
  62. // Filter the child items of the currently passed nodes and add them to the tree chart.
  63. // The judgment method is that if parentindex is equal to the absindex of the passed node, It is the subitem of the node.
  64. Viewsetorginfo. rowfilter = "parentindex = '" + parentrow ["absindex"]. tostring (). Trim () + "'";
  65. // Recursively add all subnodes of each node
  66. Foreach (datarowview myrow in viewsetorginfo)
  67. {
  68. Strenterprisename = myrow ["unit name"]. tostring (). Trim ();
  69. Treenode mynode = new treenode (strenterprisename );
  70. Parentnode. childnodes. Add (mynode );
  71. // Recursive function call. All nodes are added in order.
  72. Populatetreeview (strpolicisename, mynode, myrow );
  73. }
  74. }
  75. }

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.