Hierarchical datasource and templates

4 posts, 0 answers
  1. Davis
    Davis avatar
    1 posts
    Member since:
    Sep 2008

    Posted 22 Sep 2008 Link to this post


    I have got a PanelBar to bind to a hierarchical datasource. However I'm not sure how to get this to work with template items.

    RadPanelBar1.DataSource = clients; 
    RadPanelBar1.DataFieldID = "ID"
    RadPanelBar1.DataFieldParentID = "ParentId"
    RadPanelBar1.DataTextField = "Label"

    That produces a PanelBar with default styling and three levels:

    Parent Name
    - Child Name
      - Child Child Name

    Now what I want to be able to do is turn each row into a template field so I style each level differently and add some other stuff, like button or image on each row.

    Parent Name [button]
    - Child Name [Image]
      - Child Child Name [Textbox]

    Thanks for any help you can provide.

  2. Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 22 Sep 2008 Link to this post

    Hi Davis,

    Using databinding and templates for different item levels is not possible. You can however subscribe to the ItemCreated event of the panelbar and add custom controls to the controls collection of the created item.

    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Alex Ruggeri
    Alex Ruggeri avatar
    4 posts
    Member since:
    Jan 2010

    Posted 29 Apr 2010 Link to this post

    Can you gve an example of this as I can't get it to work. I am trying to prepend the > symbol to the link created when the databinding occurs.


  4. Nikolay Tsenkov
    Nikolay Tsenkov avatar
    734 posts

    Posted 03 May 2010 Link to this post

    Hello Alex Ruggeri,

    Here is an example that should help you.
    The panelbar is bound to an sqlDataSource and we intercept the server-side event OnItemCreated.
    Here is the code-behind handler of this event:
    protected void RadPanelBar1_ItemCreated(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
        var createdItem = e.Item;
        var owner = createdItem.Owner;
        int levelInHierarchy = 0;
        while (owner != null)
            owner = owner.Owner;
        string color = System.Drawing.Color.Black.ToString();
        switch (levelInHierarchy)
            case 1:
            case 2:
                    createdItem.ForeColor = System.Drawing.Color.Blue;
                    color = System.Drawing.Color.Blue.ToString();
            case 3:
                    createdItem.ForeColor = System.Drawing.Color.Red;
                    color = System.Drawing.Color.Red.ToString();
                    createdItem.Text = "!" + createdItem.Text;
        // templating the line
        string markUp = "<pre>";
        for (int i = 0; i <= levelInHierarchy; i++)
            markUp += "     ";
        markUp += color + "</pre>";

    Attached is an example (a screenshot) of the output of this handler, right after the page is loaded (the sqlDataSource is connected to the Northwind - MS's sample DB, to table Employees with selected EmployeeID, LastName and ReportsTo columns).

    Hope that this will help you!

    All the best,
    Nikolay Tsenkov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top