ASP. NET TreeView Dynamic load node (i)

Source: Internet
Author: User
Tags connectionstrings

1.TreeNode populateondemand= "True" after the node is dynamically loaded from the background.

2. However, if the enableclientscript= "false" in the upper TreeView causes the page to postback

3.TreeView treenodepopulate is populateondemand= "True" and the data in the TreeNode is triggered

The depth attribute of 4.TreeNode refers to the number of layers from the root node to the current nodes, and the root node layer is 0.

5.TreeNode NewNode = new TreeNode (row["CategoryName"]. ToString (), row["CategoryID"]. ToString ()) sets the first parameter of the

The value of the TreeNode class. This property value is used in the TreeNode of the Treenodepopulate () event for the callback. SQLQUERY.PARAMETERS.ADD

("@categoryid", SqlDbType.Int). Value =node. Value;

Attached code:

First, the HTML

  1. <asp:sqldatasource id="SqlDataSource1" runat= "server "
  2. connectionstring="<%$ connectionstrings:northwindconnectionstring%>"
  3. selectcommand="SELECT [CategoryID], [CategoryName] from [Categories]" >
  4. </asp:SqlDataSource>
  5. <asp:treeview id="TreeView1" runat="server" Maxdatabinddepth="2"
  6. ontreenodepopulate="treeview1_treenodepopulate" enableclientscript="true" Expanddepth="0" >
  7. <Nodes>
  8. <asp:treenode populateondemand="True" text="Product List" value="Product List" ></asp:TreeNode>
  9. </Nodes>
  10. </asp:TreeView>

Second, C #

  1. protected void Treeview1_treenodepopulate (object sender, TreeNodeEventArgs e)
  2. {
  3. if (E.node.childnodes.count = = 0)
  4. {
  5. switch (e.node.depth)
  6. {
  7. Case 0:
  8. Populatecategories (E.node);
  9. Break ;
  10. Case 1:
  11. Populateproducts (E.node);
  12. Break ;
  13. }
  14. }
  15. }
  16. void Populatecategories (TreeNode node)
  17. {
  18. SqlCommand sqlquery = new SqlCommand (
  19. "Select CategoryName, CategoryID from Categories");
  20. DataSet ResultSet;
  21. ResultSet = RunQuery (sqlquery);
  22. if (ResultSet.Tables.Count > 0)
  23. {
  24. foreach (DataRow row in resultset.tables[0]. Rows)
  25. {
  26. TreeNode NewNode = new
  27. TreeNode (row["CategoryName"). ToString (),
  28. row["CategoryID"]. ToString ());
  29. Newnode.populateondemand = true;
  30. Newnode.selectaction = Treenodeselectaction.expand;
  31. Node. Childnodes.add (NewNode);
  32. }
  33. }
  34. }
  35. void Populateproducts (TreeNode node)
  36. {
  37. SqlCommand sqlquery = new SqlCommand ();
  38. Sqlquery.commandtext = "Select ProductName from Products" +
  39. "Where CategoryID = @categoryid";
  40. SQLQUERY.PARAMETERS.ADD ("@categoryid", SqlDbType.Int). Value =
  41. Node. Value;
  42. DataSet ResultSet = RunQuery (sqlquery);
  43. if (ResultSet.Tables.Count > 0)
  44. {
  45. foreach (DataRow row in resultset.tables[0]. Rows)
  46. {
  47. TreeNode NewNode = new
  48. TreeNode (row["ProductName"). ToString ());
  49. Newnode.populateondemand = false;
  50. Newnode.selectaction = Treenodeselectaction.none;
  51. Node. Childnodes.add (NewNode);
  52. }
  53. }
  54. }
  55. private DataSet RunQuery (SqlCommand sqlquery)
  56. {
  57. string connectionString =
  58. Configurationmanager.connectionstrings
  59. ["NorthwindConnectionString"]. ConnectionString;
  60. SqlConnection DBConnection =
  61. new SqlConnection (connectionString);
  62. SqlDataAdapter dbadapter = New SqlDataAdapter ();
  63. Dbadapter.selectcommand = sqlquery;
  64. Sqlquery.connection = DBConnection;
  65. DataSet resultsDataSet = new DataSet ();
  66. Try
  67. {
  68. Dbadapter.fill (resultsDataSet);
  69. }
  70. Catch
  71. {
  72. Labelstatus.text = "Unable to connect to SQL Server.";
  73. }
  74. return resultsdataset;
  75. }

ASP. NET TreeView Dynamic load node (i)

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.