3

我有以下表格:
1-Categories:
-CategoryID
-CategoryName
-ParentID

2-Items:
-ItemId
-ItemName -CategoryID
类别

可以在分层视图中,彼此之间有许多子类别。
并且任何最后一个子类别都可以包含项目,因此只有最后一个子类别会在其下显示项目。
该视图就像一个树视图,当单击一个项目时,它将转到一个新页面。

我是这样写的:

<asp:Repeater runat="server" ID="rptCategories" OnItemDataBound="rptCategories_ItemDataBound" >
    <ItemTemplate>
        <div id="type_<%# Eval("Type") %>">
            <p >
                <a id="<%# Eval("CategoryID") %>" class="Categories">
                    <%# Eval("CategoryName") %></a>
            </p>
            <div id="ProjectsDiv_<%# Eval("CategoryID") %>" class="Projects">

                <asp:Repeater ID="rptProjects" runat="server">
                    <ItemTemplate>
                        <a id="<%# Eval("ProjectID") %>" class="ProjectLink">
                        <%# GetProject(Eval("ProjectID"))%>

                        </a>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>


但这代表了一个层次结构,
我的问题是如何将其作为树视图?

4

4 回答 4

1

为此,您需要两个类 - 一个用于树状控件,另一个用于将数据提供给层次结构中的控件。

这是一个关于如何从头开始构建 HierarchalDataBoundControl 的教程。

另一种选择是只使用递归并对其进行快速而肮脏的处理。我在这里回答了一个相关的问题。

于 2009-04-01T00:50:20.507 回答
1

如果(!IsPostBack){填充(TreeView1.Nodes);

        }
    }

  protected  void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeNode node = TreeView1.SelectedNode;
        string nodeId = node.Value;
        string nodeName = node.Text;

    }
    private void Populate(TreeNodeCollection nodes)
    {
        TreeNode parentNode = null;
        foreach (Category1 category in GetProductCategories())
        {
            parentNode = new TreeNode(category.Name, category.Id.ToString());
            if (category.Subcategories != null)
            {
                SubNodes(category, parentNode);
            }
            parentNode.Collapse();
            TreeView1.Nodes.Add(parentNode);
        }
    }
    private void SubNodes(Category1 category, TreeNode childNode)
    {
        foreach (Category1 c1 in category.Subcategories)
        {
            TreeNode subchildnode = new TreeNode(c1.Name, c1.Id.ToString());
            childNode.ChildNodes.Add(subchildnode);
            if (c1.Subcategories != null)
            {
                SubNodes(c1, subchildnode);
            }
        }
    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
    }
    #region unwanted

    // Show all checkboxes

    //TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
    //protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    //{
    //    PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
    //}
    //private void PopulateSubLevel(int parentid, TreeNode parentnode)
    //{
    //    Populate1( parentid,parentnode.ChildNodes);
    //}
    //private void Populate1(int k1,TreeNodeCollection nodes)
    //{
    //    foreach (var j in GetProductCategories())
    //    {
    //        if (j.ParentId == k1)
    //        {
    //            foreach (var k in j.Subcategories)
    //            {

    //                TreeNode tn = new TreeNode();
    //                tn.Text = k.Name;
    //                tn.Value = k.ParentId.ToString();
    //                nodes.Add(tn);
    //                //tn.PopulateOnDemand = ((int)(k.Subcategories.Count) > 0);

    //            }
    //           // break;
    //        }

    //    }
    //}
    #endregion
    #region class
    //public List<Category1> GetProductCategories()
    //{
    //    return new List<Category1> { 
    //        new Category1{Id=1,Name="Power Tools",ParentId=0,Sequence=1,Description="Power Tools",DisplayInHeader=true, 
    //            Subcategories = new List<Category1>
    //                {new Category1{Id=100,Name="Drills",ParentId=1,Sequence=1,Description="Drills",DisplayInHeader=true},
    //                new Category1{Id=101,Name="Drill Accessories",ParentId=1,Sequence=1,Description="Drill Accessories",DisplayInHeader=true},
    //                new Category1{Id=102,Name="Saws",ParentId=0,Sequence=1,Description="Saws",DisplayInHeader=true}  
    //                }},
    //                new Category1{Id=2,Name="Cordless Tools",ParentId=1,Sequence=1,Description="Cordless Tools",DisplayInHeader=true, 
    //            Subcategories = new List<Category1>
    //                {new Category1{Id=200,Name="Batteries/Chargers",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true} 

    //                }},new Category1{Id=3,Name="AirTools",ParentId=2,Sequence=1,Description="Air Tools",DisplayInHeader=true, 
    //            Subcategories = new List<Category1>
    //                {new Category1{Id=300,Name="Portable Compressors",ParentId=0,Sequence=1,Description="Portable Compressors",DisplayInHeader=true},

    //                }}
    //    };
    //}
    #endregion

    public List<Category1> GetProductCategories()
    {
        return new List<Category1> { 
            new Category1{Id=1,Name="Power Tools",ParentId=0,Sequence=1,Description="Power Tools",DisplayInHeader=true, 
                Subcategories = new List<Category1>
                    {new Category1{Id=100,Name="Drills",ParentId=1,Sequence=1,Description="Drills",DisplayInHeader=true},
                    new Category1{Id=101,Name="Drill Accessories",ParentId=1,Sequence=1,Description="Drill Accessories",DisplayInHeader=true,
                        Subcategories= new List<Category1>{new Category1{Id=1001,Name="Drill Accessories",ParentId=101,Sequence=1,Description="Drill Accessories",DisplayInHeader=true,
                        Subcategories= new List<Category1>{new Category1{Id=1001,Name="Drill Accessories",ParentId=101,Sequence=1,Description="Drill Accessories",DisplayInHeader=true}}}}},                              

                    new Category1{Id=102,Name="Saws",ParentId=0,Sequence=1,Description="Saws",DisplayInHeader=true}  
                    }},
                    new Category1{Id=2,Name="Cordless Tools",ParentId=1,Sequence=1,Description="Cordless Tools",DisplayInHeader=true, 
                Subcategories = new List<Category1>
                    {new Category1{Id=200,Name="Batteries/Chargers",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
                    Subcategories = new List<Category1>
                    {new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
                    Subcategories = new List<Category1>
                    {new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
                    Subcategories = new List<Category1>
                    {new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true,
                    Subcategories = new List<Category1>
                    {new Category1{Id=200,Name="Batteries",ParentId=2,Sequence=1,Description="Batteries/Chargers",DisplayInHeader=true}}}}}}}}} 

                    }},new Category1{Id=3,Name="AirTools",ParentId=2,Sequence=1,Description="Air Tools",DisplayInHeader=true, 
                Subcategories = new List<Category1>
                    {new Category1{Id=300,Name="Portable Compressors",ParentId=0,Sequence=1,Description="Portable Compressors",DisplayInHeader=true},

                    }},


        };
    }
于 2009-09-23T10:35:44.580 回答
0

如果您不知道层次结构有多深,那么中继器将变得复杂。为什么不直接使用 asp.net TreeView?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.aspx

您可以使用 asp.net ajax 和一个 updatepanel 来实现您想要的效果,而无需自己编写任何 javascript。

于 2009-04-01T00:46:21.857 回答
-1

Oracle 有一个专为分层查询设计的 CONNECT BY 语句。至少从 Sql Server 2005 开始,我不相信有直接的等价物,所以你不得不用存储过程来伪造它......

于 2009-04-01T00:52:53.467 回答