Asp.net 创建层次结构自定义服务器控件

Asp.net 创建层次结构自定义服务器控件,asp.net,visual-studio,c#-4.0,custom-controls,databound,Asp.net,Visual Studio,C# 4.0,Custom Controls,Databound,我有一个自参考表,我想使用以下方法将其可视化: <ol> <li> </li> </ol> 或实施 IHierarchicalDataSource 但我找不到任何例子,也找不到可以阅读的东西 有没有人能给我指一本书或一篇文章,或者分步向我解释一下需要怎么做 所需内容的摘要如下: 扩展HierarchicalDataSourceControl和实现IHeirarchicalDataSource的DataSourceControl的

我有一个自参考表,我想使用以下方法将其可视化:

 <ol>
   <li>
   </li>
 </ol>
或实施

IHierarchicalDataSource
但我找不到任何例子,也找不到可以阅读的东西


有没有人能给我指一本书或一篇文章,或者分步向我解释一下需要怎么做

所需内容的摘要如下:

扩展HierarchicalDataSourceControl和实现IHeirarchicalDataSource的DataSourceControl的控件。相信我,从提供的文档开始工作需要很多尝试和错误,但最终它是值得的。我一直在等待,但很快我将使用它完成一个项目,它将能够绑定到任何n深度结构,并使用Node.GetParent.GetChildren.Where在代码中导航。。等。这是共谋的,可能是你需要的过度杀伤力,你可能会回到中继器。考虑到stackoverflow允许的投递长度,我不能给你列出大约10万字符的完整代码

为了让您了解我的其他代码中的内容,这里是通用IHierachicalDataSourceControl:

    #region Generic Hierachical DatasourceControl
    /// <summary>
    /// Datasource control
    /// </summary>
    public class GenericHierachicalDataSourceControl<TDataContext, TNode, TEntity> : HierarchicalDataSourceControl, IHierarchicalDataSource
        where TDataContext : DataContext, new()
        where TNode : class,PNS.GenericLinqNodeHeirachy<TDataContext, TNode, TEntity>.IHeirachicalNode, new()
        where TEntity : class,PNS.GenericLinqNodeHeirachy<TDataContext, TNode, TEntity>.IHeirachyNodeEntity, new()
    {
        NodeDataSourceView view;
        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            view = new NodeDataSourceView(viewPath);
            return view;
        }

        public class NodeDataSourceView : HierarchicalDataSourceView
        {
            private string _viewPath;
            public NodeDataSourceView(string viewPath)
            {
                _viewPath = viewPath;
            }
            public override IHierarchicalEnumerable Select()
            {
                var hierarchy = new HierarchicalEnumerable();
                List<TNode> topNodes;
                if (String.IsNullOrEmpty(_viewPath))
                {
                    //get all top level nodes (ones without parents)
                    topNodes = GenericLinqNodeHeirachy<TDataContext, TNode, TEntity>.NodesDAL.GetTopLevelNodes().ToList();
                }
                else
                {
                    //get the last node in the path
                    string[] nodes = _viewPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    topNodes = new List<TNode>();

                    topNodes.Add(GenericLinqNodeHeirachy<TDataContext, TNode, TEntity>.NodesDAL.GetNode(nodes[nodes.Length - 1]));
                }
                //for each node in the path
                foreach (var node in topNodes)
                {
                    if (node.Entity != null)
                    {
                        hierarchy.Add(node.Entity);
                    }
                }
                return hierarchy;
            }
        }
        public class HierarchicalEnumerable : List<TEntity>, IHierarchicalEnumerable
        {
            public HierarchicalEnumerable()
                : base()
            {
            }
            public IHierarchyData GetHierarchyData(object enumeratedItem)
            {
                return enumeratedItem as IHierarchyData;
            }
        }
    }
最后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using BootstrapProject.CodeBase.DAL;
using PNS;

namespace BootstrapProject.CodeBase
{
    public class NodeDataSourceControl : HierarchicalDataSourceControl, IHierarchicalDataSource
    {

        NodeDataSourceView view;

        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            view = new NodeDataSourceView(viewPath);
            return view;
        }
    }

    public class NodeDataSourceView : HierarchicalDataSourceView
    {

        private string _viewPath;
        public NodeDataSourceView(string viewPath)
        {
            _viewPath = viewPath;
        }

        public override IHierarchicalEnumerable Select()
        {
            var hierarchy = new CMSPageHierarchicalEnumerable();
            List<DAL.Node> topNodes;
            if (String.IsNullOrEmpty(_viewPath))
            {
                //get all top level nodes (ones without parents)
                topNodes = CMS.NodesDAL.GetTopLevelNodes().ToList();
            }
            else
            {
                //get the last node in the path
                string[] nodes = _viewPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                topNodes = new List<DAL.Node>();
                topNodes.AddRange(CMS.NodesDAL.GetNode(nodes[nodes.Length - 1]).NodeChildren);
            }
            //for each node in the path
            foreach (var node in topNodes)
            {
                if (node.Page != null)
                {
                    hierarchy.Add(node.Page);
                }
            }
            return hierarchy;
        }
    }
}

另一种方法:使用转发器:@TimSchmelter在您需要之前,这一切都很好。如果您需要,我将向您发送/链接到我一直在构建的一组服务器控件,其中包括一个引导项目,该项目演示服务控件库中包含的控件。谢谢Paul,这非常有用,您可以将链接发送给我吗,你刚才说的?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace BootstrapProject.CodeBase
{
    public class GenericHierarchicalDataboundControl : HierarchicalDataBoundControl
    {
        private string _textField;
        public string TextField
        {
            get { return _textField; }
            set { _textField = value; }
        }
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
        protected override void CreateChildControls()
        {
            if (null != Page && Page.IsPostBack && null != ViewState["_!DataBound"])
            {
                this.RequiresDataBinding = true;
                this.EnsureDataBound();
            }
        }
        protected override void PerformDataBinding()
        {
            IHierarchicalEnumerable dataSource = GetData(string.Empty).Select();
            this.PerformDataBinding(0, this.Controls, dataSource);
            this.MarkAsDataBound();
        }
        protected virtual void PerformDataBinding(int level, ControlCollection controls, IHierarchicalEnumerable dataSource)
        {
            if (null != dataSource)
            {
                //controls.Clear();
                HtmlGenericControl ul = new HtmlGenericControl("ul");
                foreach (object value in dataSource)
                {
                    var itemData = dataSource.GetHierarchyData(value);
                    Control item = CreateAndBindControl(level, value);
                    ul.Controls.Add(item);

                    var data = dataSource.GetHierarchyData(value);
                    if (data != null && data.HasChildren)
                    {
                        IHierarchicalEnumerable childData = data.GetChildren();
                        PerformDataBinding(1 + level, item.Controls, childData);
                    }

                    controls.Add(ul);
                }
            }
        }
        protected virtual Control CreateAndBindControl(int level, object dataItem)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            string text = String.Empty;
            if (!String.IsNullOrEmpty(TextField))
            {
                text = DataBinder.GetPropertyValue(dataItem, TextField).ToString();
            }
            else
            {
                text = dataItem.ToString();
            }
            li.Attributes.Add("rel", text);
            li.Controls.Add(new HyperLink { Text = text });
            return li;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using BootstrapProject.CodeBase.DAL;
using PNS;

namespace BootstrapProject.CodeBase
{
    public class NodeDataSourceControl : HierarchicalDataSourceControl, IHierarchicalDataSource
    {

        NodeDataSourceView view;

        protected override HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            view = new NodeDataSourceView(viewPath);
            return view;
        }
    }

    public class NodeDataSourceView : HierarchicalDataSourceView
    {

        private string _viewPath;
        public NodeDataSourceView(string viewPath)
        {
            _viewPath = viewPath;
        }

        public override IHierarchicalEnumerable Select()
        {
            var hierarchy = new CMSPageHierarchicalEnumerable();
            List<DAL.Node> topNodes;
            if (String.IsNullOrEmpty(_viewPath))
            {
                //get all top level nodes (ones without parents)
                topNodes = CMS.NodesDAL.GetTopLevelNodes().ToList();
            }
            else
            {
                //get the last node in the path
                string[] nodes = _viewPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                topNodes = new List<DAL.Node>();
                topNodes.AddRange(CMS.NodesDAL.GetNode(nodes[nodes.Length - 1]).NodeChildren);
            }
            //for each node in the path
            foreach (var node in topNodes)
            {
                if (node.Page != null)
                {
                    hierarchy.Add(node.Page);
                }
            }
            return hierarchy;
        }
    }
}