为 ASP.NET 实现 HierarchicalDataBoundControl

发布于 2024-07-26 16:32:00 字数 737 浏览 5 评论 0原文

我想为 ASP.NET 实现分层数据绑定控件。

我使用 在自定义集合中实现 IHierarchy 支持 作为创建的基础我的分层集合。 我还为该集合创建了一个 HierarchicalDataSourceControl。 一切正常:我可以将其绑定到 ASP.NET TreeView,并且它可以正确呈现数据。

但是,我陷入了 HierarchicalDataBoundControl 的困境。 我找到了示例,但我对 c# / .Net 还太陌生,无法理解它们。 我不太明白如何实现这些示例:

渲染数据绑定 UL 菜单

也不是

HierarchicalDataBoundControl 类

有没有人有任何提示或实现这样的控件的更好示例?

I want to implement a Hierarchical data bound control for ASP.NET.

I used Implementing IHierarchy Support Into Your Custom Collection as a basis to create my hierarchical collection. I also created a HierarchicalDataSourceControl for the collection. Everything works: I can bind it to an ASP.NET TreeView and it renders the data correctly.

However, I'm stuck on the HierarchicalDataBoundControl. I found examples, but I am still too new at c# / .Net to understand them. I don't quite understand how to implement the examples:

Rendering a databound UL menu

nor

HierarchicalDataBoundControl Class

Does anyone have any tips, or better examples of implementing a control like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜访吸血鬼 2024-08-02 16:32:00

作为另一个 DataBound 控件,您必须重写 PerformDataBinding 方法,如下所示。

protected override void PerformDataBinding()
{
    base.PerformDataBinding();

    if (!IsBoundUsingDataSourceID && this.DataSource == null) return;

    HierarchicalDataSourceView view = this.GetData(string.Empty);
    if (view != null)
    {
        IHierarchicalEnumerable root = view.Select();
    }
}

虽然您的控件在这种情况下支持 DataSourceHierarchialDataSource 控件,但应检查是否有分配的 DataSource,并获取其 DataSourceView 完成数据绑定。 通过GetData方法即可实现。 然后调用视图的Select方法来获取根集合。

然后,您必须迭代根集合,该集合是 IHierarchialEnumerable 类型的对象。 它有一个名为 GetHierarchyData 的简单方法,该方法给出一个作为集合项目的对象,并返回一个描述节点的关联 IHierarchyData。 由于存在此方法,因此大多数时候您不知道项目的实际类型。 IHierarchyData 表示有关节点及其子节点的一些信息。

最后,您不需要编写新的 DataSource 控件。 如果您在创建自定义导航控件后,最好为 SiteMapDataSource 编写一个新的提供程序。

As another DataBound controls you have to override PerformDataBinding method as follows.

protected override void PerformDataBinding()
{
    base.PerformDataBinding();

    if (!IsBoundUsingDataSourceID && this.DataSource == null) return;

    HierarchicalDataSourceView view = this.GetData(string.Empty);
    if (view != null)
    {
        IHierarchicalEnumerable root = view.Select();
    }
}

Whereas your control supports DataSource and HierarchialDataSource control in this case, should check if there is an assigned DataSource, get its DataSourceView to accomplish data binding. It's practicable through GetData method. Then call view's Select method to get the root collection.

Afterwards, you have to iterate through root collection that is an object of type IHierarchialEnumerable. It has a simple method called GetHierarchyData that gives an object which is an item of the collection and returns an associated IHierarchyData that describes a node. This methods is there, therefore in most times you don't know the actual type of item. IHierarchyData represents some information about a node and its children.

Lastly, you don't need to write a new DataSource control. If you are after creating a custom navigation control, it's better to write a new provider for SiteMapDataSource.

执手闯天涯 2024-08-02 16:32:00

我遇到了类似的问题并创建了自己的原始 html。

    private void LoadCategory()
{
    String s = "";

// 准备数据集 DataSet ds
// 请注意,我的类别表具有 C_NAME 和 PC_NAME 列,请根据您的列 anme 更改它
ds.Relations.Add("rsParentChild", ds.Tables[0].Columns["C_NAME"], ds.Tables[0].Columns["PC_NAME"]);

    s = s + "\n   <table>";
    s = s + "\n       <tr>";
    s = s + "\n           <td class='br4'> ";
    s = s + "\n               <table>";
    s = s + "\n                   <tr>";
    s = s + "\n                       <td>";
    s = s + "\n                           <ul id='qm0' class='qmmc'>";
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        if (dr["PC_NAME"] == DBNull.Value)
        {
            s = s + "\n                                 <li><a class='qmparent' href='#'>" + dr["C_NAME"].ToString() + "</a>";
            s = s + "\n                                 <ul>" + PopulateTree(dr) + "</ul>";
        }
    }

    s = s + "\n                         <li class='qmclear'> </li></ul>";
    s = s + "\n                         <script type='text/javascript'>qm_create(0,true,0,500,'all',false,false,false,false);</script>";
    s = s + "\n                 </td>";
    s = s + "\n             </tr>";
    s = s + "\n         </table>";
    s = s + "\n     </td>";
    s = s + "\n </tr>";
    s = s + "\n </table>";
    Literal1.Text = s;
}
private String PopulateTree(DataRow dr)
{
    String s = "";
    String ss;
    foreach (DataRow row in dr.GetChildRows("rsParentChild"))
    {
        s = s + "                                <li><a href=\"javascript:FetchProducts(1,'" + row["C_NAME"].ToString() + "')\">" + row["C_NAME"].ToString() + "</a>\n";
        ss = PopulateTree(row);
        if (ss != "")
            s = s + "                                <ul>" + ss + "</ul></li>\n\n";
    }
    return s;
}

I faced similar problem and created my own raw html.

    private void LoadCategory()
{
    String s = "";

// Prepare your dataset DataSet ds
// Note That I have Category Table having C_NAME AND PC_NAME coloums, change it as per your column anme
ds.Relations.Add("rsParentChild", ds.Tables[0].Columns["C_NAME"], ds.Tables[0].Columns["PC_NAME"]);

    s = s + "\n   <table>";
    s = s + "\n       <tr>";
    s = s + "\n           <td class='br4'> ";
    s = s + "\n               <table>";
    s = s + "\n                   <tr>";
    s = s + "\n                       <td>";
    s = s + "\n                           <ul id='qm0' class='qmmc'>";
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        if (dr["PC_NAME"] == DBNull.Value)
        {
            s = s + "\n                                 <li><a class='qmparent' href='#'>" + dr["C_NAME"].ToString() + "</a>";
            s = s + "\n                                 <ul>" + PopulateTree(dr) + "</ul>";
        }
    }

    s = s + "\n                         <li class='qmclear'> </li></ul>";
    s = s + "\n                         <script type='text/javascript'>qm_create(0,true,0,500,'all',false,false,false,false);</script>";
    s = s + "\n                 </td>";
    s = s + "\n             </tr>";
    s = s + "\n         </table>";
    s = s + "\n     </td>";
    s = s + "\n </tr>";
    s = s + "\n </table>";
    Literal1.Text = s;
}
private String PopulateTree(DataRow dr)
{
    String s = "";
    String ss;
    foreach (DataRow row in dr.GetChildRows("rsParentChild"))
    {
        s = s + "                                <li><a href=\"javascript:FetchProducts(1,'" + row["C_NAME"].ToString() + "')\">" + row["C_NAME"].ToString() + "</a>\n";
        ss = PopulateTree(row);
        if (ss != "")
            s = s + "                                <ul>" + ss + "</ul></li>\n\n";
    }
    return s;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文