码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net TreeView动态加载节点(一)

时间:2017-06-21 11:32:39      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:track   obj   hid   ==   产品   new t   article   cts   导致   

1.TreeNode的PopulateOnDemand="True"后节点就是动态从后台加载的.

2.但是如果上层TreeView的EnableClientScript="false",就会导致页面Postback

3.TreeView的TreeNodePopulate是PopulateOnDemand="True"并且TreeNode内已有数据才触发的

4.TreeNode的Depth属性是指从根结点算起到现在节点的层数,根结点层数为0

5.TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(),row["CategoryID"].ToString())的第一个参数设置了

TreeNode类的Value 属性值.这个属性值会在发生TreeNodePopulate()事件的TreeNode对像回传用到.sqlQuery.Parameters.Add

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

 

 

附代码:

一、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="产品列表" Value="产品列表"></asp:TreeNode>
  9.             </Nodes>
  10.         </asp:TreeView>

 

 

 

二、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动态加载节点(一)

标签:track   obj   hid   ==   产品   new t   article   cts   导致   

原文地址:http://www.cnblogs.com/www56/p/7057980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!