简体   繁体   English

如何从头开始构建ASP.NET TreeView ......?

[英]How to build an ASP.NET TreeView From Scratch…?

I am trying to build a nested/threaded comment system in ASP.NET. 我试图在ASP.NET中构建一个嵌套/线程注释系统。 I don't know how the PHP guys do it. 我不知道PHP人员是怎么做到的。 Its much harder than first imagined. 它比最初想象的要困难得多。

I am trying my damnedest get Hierarchical data to output to the user, but its not working. 我正在尝试我最大的获取分层数据输出给用户,但它无法正常工作。

I have a table with text, a itemID and a parentID. 我有一个带有文本的表,一个itemID和一个parentID。

I want to display the information in a tree view format, but asp.net's standard control just doesn't work... 我想以树视图格式显示信息,但asp.net的标准控件不起作用...

Can anyone point me in the right direction of how to output this into a treeview. 任何人都可以指出我如何将其输出到树视图的正确方向。 I have tried nesting repeaters, straight out html building from codebehind and the treeview control. 我已经尝试过嵌套转发器,直接从代码隐藏和树视图控件构建html。

I still haven't found a solution... Can anyone help me out? 我还没有找到解决方案......任何人都可以帮助我吗?

Quickly, by head (could not check in VS2k8 now), I would do it something like this using Linq to SQL 很快,通过头部(现在无法检入VS2k8),我会使用Linq to SQL做这样的事情

private void BuildTree()
{
   List<Item> items = from item in dataContext.Items
                      select item;

   List<Item> rootItems = items.FindAll(p => p.ParentID == null );

   foreach ( Item item in rootItems )
   {
      TreeViewNode tvi = new TreeViewNode(item.text);
      BuildChildNodes(tvi, items, item.ID);
      YourTreeNodeName.Nodes.Add(tvi);
   }   
}

private void BuildChildNodes(TreeViewNode parentNode, List<Item> items, long parentID)
{
   List<Item> children = items.FindAll ( p => p.ParentID = parentID );
   foreach( Item item in children)
   {
      TreeViewNode tvi = new TreeViewNode(item.text);
      parentNode.Nodes.Add(tvi);
      BuildChildNodes(tvi, items, item.ID);         
   }
}

As I understand it, if you have a database table with hierarchical data, you have two options: create your own custom data source, or programmatically bind the treeview to the database table. 据我所知,如果您有一个包含分层数据的数据库表,您有两个选择:创建自己的自定义数据源,或以编程方式将树视图绑定到数据库表。

I don't have code for you, but you could use following these steps: 我没有您的代码,但您可以使用以下步骤:

  1. declare treeview control within asp.net page 在asp.net页面中声明树视图控件
  2. populate a DataTable (via a SqlDataAdapter) with your heirarchical data (SELECT itemID, parentID FROM...) 使用您的层次数据填充DataTable(通过SqlDataAdapter)(SELECT itemID,parentID FROM ...)
  3. use that same DataTable to create a DataView of top-level items (parentID will be null) 使用相同的DataTable创建顶级项的DataView(parentID将为null)
  4. for each row of the DataView, recursively add treeview items by filtering another DataView where each DataViewRow's parentID is equal to the row you're looping through in step 4 对于DataView的每一行,通过过滤另一个DataView递归添加treeview项,其中每个DataViewRow的parentID等于您在步骤4中循环的行

pretty much all of this is done using TreeView.Nodes.Add(theNewNode), where theNewNode is an instance of the TreeNode object 几乎所有这些都是使用TreeView.Nodes.Add(theNewNode)完成的,其中NewNode是TreeNode对象的一个​​实例

I know this all sounds very confusing, but I've done it in the past. 我知道这一切听起来很混乱,但我过去做过。 I found great info in Stephen Walther ASP.NET Unleashed book, which has entire sections devoted to accomplishing this. 我在Stephen Walther ASP.NET Unleashed一书中找到了很好的信息,该书有完整的部分专门用于实现这一目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM