This is a migrated thread and some comments may be shown as answers.

DataBinding RadTreeView resets NodeItemTemplate

2 Answers 75 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
CBARS
Top achievements
Rank 2
CBARS asked on 31 Oct 2008, 09:44 AM
I am dynamically generating a RadTreeView, and bind it to a dynamically generated XmlDataSource.

I need to set different nodes to different ItemTemplates.

To do this, I databind the RadTreeView, so each node item is generated inside the RadTreeView.Nodes collection. Then, I foreach through the Nodes collection, and set each node's template to a certain template. If I do this, the data from the XML file is overwritten with the data in the template. But if I databind the RadTreeView again afterwards, each node's template is lost again.

Here's some code :

foreach'ing through the nodes
foreach (Telerik.Web.UI.RadTreeNode tn in CellTree.Nodes) 
      tn.Expanded = true
      tn.ExpandChildNodes(); 
      //Set header/item templates 
      SetItemTemplate(tn); 

setting the template :
private void SetItemTemplate(Telerik.Web.UI.RadTreeNode Node) 
   if (Node.ParentNode == null) 
   { 
       MyTemplate template = new MyTemplate("Header"); 
       template.InstantiateIn(Node); 
   } 
   else 
   { 
       MyTemplate template = new MyTemplate("Item"); 
       template.InstantiateIn(Node); 
   } 
 
   foreach (Telerik.Web.UI.RadTreeNode tn1 in Node.Nodes) 
   { 
       //Use reflection to set each child item's template 
       SetItemTemplate(tn1); 
   }             

The template class :
public class MyTemplate : System.Web.UI.ITemplate 
    string TemplateType; 
 
    public MyTemplate(string type) 
    { 
        TemplateType = type
    } 
 
    public void InstantiateIn(Control container) 
    { 
        PlaceHolder ph = new PlaceHolder(); 
        ph.ID = "test"
        Label label1 = new Label();             
 
        switch (TemplateType) 
        { 
            case "Header": 
                ph.Controls.Add(new LiteralControl("<div style=\"background-image:url(images/Parent.jpg); background-repeat:no-repeat; padding-left:15px; padding-top:4px; width:400px; height:31px\">")); 
 
                label1.ID = "MenuItem"
                label1.Text = "Text"
                label1.Font.Size = 10
                label1.Font.Bold = true
                label1.ForeColor = Color.White;                     
                ph.Controls.Add(label1); 
                ph.DataBinding += new EventHandler(ph_DataBinding); 
                ph.Controls.Add(new LiteralControl("</div>")); 
                break; 
 
            case "Item": 
                ph.Controls.Add(new LiteralControl("<div style=\"background-image:url(images/item.bmp); background-repeat:no-repeat; padding-left:10px; width:400px; height:24px\">")); 
                label1.ID = "MenuItem"
                label1.Text = "Text"
                label1.Font.Size = 10
                label1.Font.Bold = true
                ph.Controls.Add(label1); 
                ph.DataBinding += new EventHandler(ph_DataBinding); 
                ph.Controls.Add(new LiteralControl("</div>")); 
                break; 
        }             
        container.Controls.Add(ph); 
    } 
 
    private void ph_DataBinding(object sender, EventArgs e) 
    { 
        PlaceHolder ph = (PlaceHolder)sender; 
        Telerik.Web.UI.RadTreeNode ri = (Telerik.Web.UI.RadTreeNode)ph.NamingContainer; 
        string menutext = (string)DataBinder.Eval(ri.DataItem, "Text"); 
        ((Label)ph.FindControl("MenuItem")).Text = menutext
    } 

Is there another way of setting each node's template to a certain template?

2 Answers, 1 is accepted

Sort by
0
CBARS
Top achievements
Rank 2
answered on 31 Oct 2008, 11:19 AM
I've used a workaround for my particular situation, by extracting the root element of my XML file, and setting that in a seperate <div> outside the treeview, and then using a common template for all the nodes in the treeview.
0
Veselin Vasilev
Telerik team
answered on 03 Nov 2008, 08:59 AM
Hi cbars,

This is an interesting solution. You can participate in our code-library with a project. This will be useful for the community members.

Best wishes,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
TreeView
Asked by
CBARS
Top achievements
Rank 2
Answers by
CBARS
Top achievements
Rank 2
Veselin Vasilev
Telerik team
Share this question
or