[英]How the generate TreeView in asp.net?
Consider the following table. 请考虑下表。 It has 3 filed with the following data.
它有3个提交以下数据。 Now I want to show the data in TreeView Control in asp.net and c# I will select all the data in a DataTable.
现在我想在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
I recently posted some code on Code Review which answers this question. 我最近在Code Review上发布了一些代码来回答这个问题。 Here it is below, slightly modified for your table.
它在下面,略微修改为您的表。 I have tried to meet your needs by using a
DataTable
as the input, but I haven't used DataTables with LINQ before so you may need to edit slightly. 我已经尝试通过使用
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;
}
If this doesn't build then at least I hope it will point you in the right direction. 如果这不构建那么至少我希望它会指出你正确的方向。
Here are some useful tips for you: for implementing tree view using asp.net and c# 以下是一些有用的提示:使用asp.net和c#实现树视图
NOte: I did not implement and did not use your datatable data here .... NOte:我没有实现,也没有在这里使用你的数据表数据....
TreeView control expects a heirarchical data structure, so we cant bind the DataTable to menu directly. TreeView控件需要一个层次结构数据结构,因此我们无法直接将DataTable绑定到菜单。
Use data table Select where ParentID = ID 使用数据表选择ParentID = ID的位置
Please use a recursive method to create child items relevent to current menu item ID 请使用递归方法创建与当前菜单项ID相关的子项
Here is an working example that you may use to transfer your ralational data structure (TABLE) to hierarchical data structure (TREE) using the relationship between ParentID and 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.