Asp.net TreeView動態載入節點(一)

來源:互聯網
上載者:User

標籤: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動態載入節點(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.