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
- <asp:sqldatasource id="SqlDataSource1" runat= "server "
- connectionstring="<%$ connectionstrings:northwindconnectionstring%>"
- selectcommand="SELECT [CategoryID], [CategoryName] from [Categories]" >
- </asp:SqlDataSource>
- <asp:treeview id="TreeView1" runat="server" Maxdatabinddepth="2"
- ontreenodepopulate="treeview1_treenodepopulate" enableclientscript="true" Expanddepth="0" >
- <Nodes>
- <asp:treenode populateondemand="True" text="Product List" value="Product List" ></asp:TreeNode>
- </Nodes>
- </asp:TreeView>
Second, C #
- protected void Treeview1_treenodepopulate (object sender, TreeNodeEventArgs e)
- {
- if (E.node.childnodes.count = = 0)
- {
- switch (e.node.depth)
- {
- Case 0:
- Populatecategories (E.node);
- Break ;
- Case 1:
- Populateproducts (E.node);
- Break ;
- }
- }
- }
- void Populatecategories (TreeNode node)
- {
- SqlCommand sqlquery = new SqlCommand (
- "Select CategoryName, CategoryID from Categories");
- DataSet ResultSet;
- ResultSet = RunQuery (sqlquery);
- if (ResultSet.Tables.Count > 0)
- {
- foreach (DataRow row in resultset.tables[0]. Rows)
- {
- TreeNode NewNode = new
- TreeNode (row["CategoryName"). ToString (),
- row["CategoryID"]. ToString ());
- Newnode.populateondemand = true;
- Newnode.selectaction = Treenodeselectaction.expand;
- Node. Childnodes.add (NewNode);
- }
- }
- }
- void Populateproducts (TreeNode node)
- {
- SqlCommand sqlquery = new SqlCommand ();
- Sqlquery.commandtext = "Select ProductName from Products" +
- "Where CategoryID = @categoryid";
- SQLQUERY.PARAMETERS.ADD ("@categoryid", SqlDbType.Int). Value =
- Node. Value;
- DataSet ResultSet = RunQuery (sqlquery);
- if (ResultSet.Tables.Count > 0)
- {
- foreach (DataRow row in resultset.tables[0]. Rows)
- {
- TreeNode NewNode = new
- TreeNode (row["ProductName"). ToString ());
- Newnode.populateondemand = false;
- Newnode.selectaction = Treenodeselectaction.none;
- Node. Childnodes.add (NewNode);
- }
- }
- }
- private DataSet RunQuery (SqlCommand sqlquery)
- {
- string connectionString =
- Configurationmanager.connectionstrings
- ["NorthwindConnectionString"]. ConnectionString;
- SqlConnection DBConnection =
- new SqlConnection (connectionString);
- SqlDataAdapter dbadapter = New SqlDataAdapter ();
- Dbadapter.selectcommand = sqlquery;
- Sqlquery.connection = DBConnection;
- DataSet resultsDataSet = new DataSet ();
- Try
- {
- Dbadapter.fill (resultsDataSet);
- }
- Catch
- {
- Labelstatus.text = "Unable to connect to SQL Server.";
- }
- return resultsdataset;
- }
ASP. NET TreeView Dynamic load node (i)