簡體   English   中英

如何在asp.net中生成TreeView?

[英]How the generate TreeView in asp.net?

請考慮下表。 它有3個提交以下數據。 現在我想在asp.net中的TreeView Control中顯示數據,c#我將選擇DataTable中的所有數據。

category_id category_name   parents_id
1           Root            NULL
2           Teacher         1
3           Student         1
4           TeacherA        2
5           TeacherB        2
6           TeacherC        2
7           StudentA        3
8           StudentB        3
9           StudentC        3

我最近在Code Review上發布了一些代碼來回答這個問題。 它在下面,略微修改為您的表。 我已經嘗試通過使用DataTable作為輸入來滿足您的需求,但我之前沒有將DataTables與LINQ一起使用,因此您可能需要稍微編輯。

public TreeNode GenerateCategoriesTree(DataTable dt) {
  var s = new Stack<TreeNodeAndId>();
  var categories = dt.AsEnumerable();
  var rootCategory = categories.Single(o => o.Field<int?>("parents_id") == null);
  var rootNode = new TreeNode(rootCategory["category_name"].ToString(), rootCategory["category_id"].ToString());
  s.Push(new TreeNodeAndId { Id = Convert.ToInt32(rootCategory["category_id"]), TreeNode = rootNode });
  while (s.Count > 0) {
    var node = s.Peek();
    var current = categories.FirstOrDefault(o => o.Field<int?>("parents_id") == node.Id);
    if (current == null) {
      s.Pop();
      continue;
    }
    var child = new TreeNode(current["category_name"].ToString(), current["category_id"].ToString());
    node.TreeNode.ChildNodes.Add(child);
    s.Push(new TreeNodeAndId { Id = Convert.ToInt32(current["category_id"]), TreeNode = child });
    categories.Remove(current);
  }
  return rootNode;
}

struct TreeNodeAndId
{
  public TreeNode TreeNode;
  public int? Id;
}

如果這不構建那么至少我希望它會指出你正確的方向。

以下是一些有用的提示:使用asp.net和c#實現樹視圖

NOte:我沒有實現,也沒有在這里使用你的數據表數據....

  • TreeView控件需要一個層次結構數據結構,因此我們無法直接將DataTable綁定到菜單。

  • 使用數據表選擇ParentID = ID的位置

  • 請使用遞歸方法創建與當前菜單項ID相關的子項

  • 下面是一個工作示例,您可以使用ParentID和ID之間的關系將您的平台數據結構(TABLE)傳輸到分層數據結構(TREE)

     <%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script runat="server"> protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!this.IsPostBack) this.AddNodes(this.trvCategories.Nodes, 0, this.LoadData()); } private void AddNodes(TreeNodeCollection nodes, int level, System.Data.DataTable dt) { string filterExp = string.Format("ParentID='{0}'", level); foreach (System.Data.DataRow r in dt.Select(filterExp)) { TreeNode item = new TreeNode() { Text = r[1].ToString(), Value = r[2].ToString() }; this.AddNodes(item.ChildNodes, int.Parse(r[0].ToString()), dt); nodes.Add(item); } } private System.Data.DataTable LoadData() { /// /// For the simplicity I do build the datatable dynamically, /// But you may get this data from the DATABASE /// System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("ID", String.Empty.GetType()); dt.Columns.Add("[Name]", String.Empty.GetType()); dt.Columns.Add("[Value]", String.Empty.GetType()); dt.Columns.Add("ParentID", String.Empty.GetType()); dt.Columns.Add("[Order]", String.Empty.GetType()); int index = 9; for (int i = 0; i < 1100; i++) { string item = "{0},Item{1},Value{1},{2},{1}"; if (i < 110) { index = i < 10 ? 0 : int.Parse(Math.Ceiling((decimal)i / 10).ToString()); dt.Rows.Add((string.Format(item, i + 1, i, index)).Split(char.Parse(","))); } else { if (i % 10 == 0) index++; dt.Rows.Add((string.Format(item, i + 1, i, index)).Split(char.Parse(","))); } } return dt; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM