views:

600

answers:

3

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?

A: 

Hi,

I found your post after looking for the same kind of thing.

I found this which has helped me out: http://dotnetslackers.com/VB_NET/re-17169_Rendering_a_databound_UL_menu.aspx

HTH

Mark Redman
I already had the same article in my original question link "Rendering a databound UL menu" which points to the original question.
Breadtruck
A: 

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'>&nbsp;</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;
}
Manjoor
Thanks, this is what I actually did myself based on some other posts I found. I implemented it in a user control. It really doesn't answer the question though. I still want to work it into a a HierarchicalDataBoundControl correctly
Breadtruck
A: 

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.

Mehdi Golchin