DataBinding RadTreeView resets NodeItemTemplate

3 posts, 0 answers
  1. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 31 Oct 2008 Link to this post

    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. CBARS
    CBARS avatar
    55 posts
    Member since:
    Sep 2008

    Posted 31 Oct 2008 Link to this post

    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.
  3. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 03 Nov 2008 Link to this post

    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.
Back to Top