Add nodes dynamically in the Treeview in Asp.net 2.0

Source: Internet
Author: User

The main code is as follows:

 

  1. <HTML>
  2. <Head>
  3. </Head>
  4. <Body>
  5. <Form ID = "form1" runat = "server">
  6. <Div>
  7. <Asp: treeviewrunat = "server" expandimageurl = "images/closed.gif"
  8. Collapseimageurl = "images/open.gif"
  9. Ontreenodepopulate = "node_populate" id = "tvwauthors">
  10. <Nodes>
  11. <Asp: treenodetext = "Authors" populateondemand = true
  12. Value = "0"/>
  13. </Nodes>
  14. </ASP: Treeview>
  15. </Div>
  16. </Form>
  17. </Body>
  18. </Html>

Background code:

  1. Void node_populate (objectsender, system. Web. UI. webcontrols. treenodeeventargs E)
  2. {
  3. If (E. node. childnodes. Count = 0)
  4. {
  5. Switch (E. node. Depth)
  6. {
  7. Case 0:
  8. Fillauthors (E. node );
  9. Break;
  10. Case 1:
  11. Filltitlesforauthors (E. node );
  12. Break;
  13. }
  14. }
  15. }
  16. Void fillauthors (treenode node)
  17. {
  18. String connstring = system. configuration. configurationsettings.
  19. Connectionstrings ["northwindconnnection"]. connectionstring;
  20. Sqlconnection connection = new sqlconnection (connstring );
  21. Sqlcommand command = new sqlcommand ("select * from
  22. Authors ", connection );
  23. Sqldataadapter adapter = new sqldataadapter (command );
  24. Dataset authors = new dataset ();
  25. Adapter. Fill (authors );
  26. If (authors. Tables. Count> 0)
  27. {
  28. Foreach (datarow row in authors. Tables [0]. Rows)
  29. {
  30. Treenode newnode = new
  31. Treenode (row ["au_fname"]. tostring () + "" +
  32. Row ["au_lname"]. tostring (),
  33. Row ["au_id"]. tostring ());
  34. Newnode. populateondemand = true; // whether to dynamically fill nodes
  35. Newnode. selectaction = treenodeselectaction. Expand; // events triggered when a node is selected (switching the expand and collapse statuses of nodes .)
  36. Node. childnodes. Add (newnode );
  37. }
  38. }
  39. }
  40. Void filltitlesforauthors (treenode node)
  41. {
  42. String authorid = node. value;
  43. String connstring = system. configuration. configurationsettings.
  44. Connectionstrings ["northwindconnnection"]. connectionstring;
  45. Sqlconnection connection = new sqlconnection (connstring );
  46. Sqlcommand command = new sqlcommand ("select T. title,
  47. T. title_id from titles t "+
  48. "Inner join titleauthor Ta on
  49. T. title_id = TA. title_id "+
  50. "Where ta. au_id = '" + authorid + "'", connection );
  51. Sqldataadapter adapter = new sqldataadapter (command );
  52. Dataset titlesforauthors = new dataset ();
  53. Adapter. Fill (titlesforauthors );
  54. If (titlesforauthors. Tables. Count> 0)
  55. {
  56. Foreach (datarow row in titlesforauthors. Tables [0]. Rows)
  57. {
  58. Treenode newnode = new treenode (
  59. Row ["title"]. tostring (), row ["title_id"]. tostring ());
  60. Newnode. populateondemand = false; // whether to dynamically fill the node
  61. Newnode. selectaction = treenodeselectaction. None;
  62. Node. childnodes. Add (newnode );
  63. }
  64. }
  65. }

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.