Strange behaviour with TreeView and Templates

1 posts, 0 answers
  1. Kristof
    Kristof avatar
    12 posts
    Member since:
    Apr 2009

    Posted 15 Jun 2009 Link to this post


    We downloaded the evaluation version of RadControls for ASP.Net and are using the RadTreeView. We are planning to purchase the full version soon, but we ran into a strange issue with the RadTreeView control.

    Our TreeView control needs each node to be template as a Table. But each level of the tree needs a different template, so not one single template for the whole tree. Also, all nodes are created dynamically at runtime, and the SingleExpandPath property is set to true.

    Now we experience the following issue:

    When we select the second node below a templated node, the nodeValue we get at serverside is the value of the first node!

    I was able to reproduce this with a simpler version of the code we use for real, when you run the code below, you can expand the tree until the second level, and then, when you select the “child2ofchild1” node, the code behind will tell you that “child1ofchild1” nodes was selected. Could you guys look into this and tell us what we are doing wrong?


    The code:


    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">

    <asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" runat="server">
     <telerik:RadTreeView ID="RadTreeView1" Runat="server" SingleExpandPath="true"
      OnNodeExpand="TreeView_NodeExpand" OnNodeCollapse="TreeView_NodeCollapse"





    The codebehind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;

    namespace WebApplication1
        public partial class _Default : System.Web.UI.Page
            #region Node Template
            class TableTemplate : ITemplate
                private string m_cell1Text;
                private string m_cell2Text;
                private int m_cell1Size;
                private int m_cell2Size;

                public TableTemplate(string cell1Text, string cell2Text,
                    int cell1Size, int cell2Size)
                    m_cell1Text = cell1Text;
                    m_cell2Text = cell2Text;
                    m_cell1Size = cell1Size;
                    m_cell2Size = cell2Size;

                public void InstantiateIn(Control container)
                    Table nodeTable = new Table();
                    TableRow nodeRow = new TableRow();

                    TableCell firstCell = new TableCell();
                    firstCell.Style["width"] = m_cell1Size + "px";
                    firstCell.DataBinding += new EventHandler(firstCell_DataBinding);

                    TableCell secondCell = new TableCell();
                    secondCell.Style["width"] = m_cell2Size + "px";
                    secondCell.DataBinding += new EventHandler(secondCell_DataBinding);


                private void firstCell_DataBinding(object sender, EventArgs e)
                    TableCell target = (TableCell)sender;
                    target.Text = m_cell1Text;

                private void secondCell_DataBinding(object sender, EventArgs e)
                    TableCell target = (TableCell)sender;
                    target.Text = m_cell2Text;

            protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack)
                    //first time, load root node

            private void LoadTreeRoot()
                    new RadTreeNode
                            Value = "root",
                            Text = "root",
                            ExpandMode = TreeNodeExpandMode.ServerSide

            #region TreeView Events
            public void TreeView_NodeExpand(object sender, RadTreeNodeEventArgs e)
                //get the value of the node that is expanded
                string expandedNodeValue = e.Node.Value;
                //refresh the tree, with this node selected
                RefreshTree(expandedNodeValue, true);

                //set this node as selected

            public void TreeView_NodeCollapse(object sender, RadTreeNodeEventArgs e)
                //get the value of the node that is collapsed
                string collapsedNodeValue = e.Node.Value;
                //refresh the tree, with this node selected
                RefreshTree(collapsedNodeValue, false);

                //set this node as selected

            public void TreeView_NodeClick(object sender, RadTreeNodeEventArgs e)
                //get the value of the clicked node
                string clickedNodeValue = e.Node.Value;

                //refresh the tree
                RefreshTree(clickedNodeValue, false);

                //set this node as selected

            #region TreeView Refresh
            private void RefreshTree(string targetNodeValue, bool expanded)
                //find the root node
                RadTreeNode rootNode = RadTreeView1.FindNodeByValue("root");

                if (rootNode != null)
                    //clear the nodes

                    //refresh the nodes below this root
                    //child nodes
                    RadTreeNode child1 = new RadTreeNode { Value = "child1", ExpandMode = TreeNodeExpandMode.ServerSide };
                    TableTemplate child1Template = new TableTemplate("child1Cell1", "child1Cell2", 200, 100);

                    if ((targetNodeValue != "root" && targetNodeValue != "child1") ||
                        (targetNodeValue == "child1" && expanded))
                        //also expand this child node
                        child1.Expanded = true;

                        //add the childofchild nodes
                        RadTreeNode child1ofchild1 = new RadTreeNode { Value = "child1ofchild1" };
                        TableTemplate child1ofchild1Template = new TableTemplate("child1ofchild1Cell1", "child1ofchild1Cell2", 180, 100);

                        RadTreeNode child2ofchild1 = new RadTreeNode { Value = "child2ofchild1" };
                        TableTemplate child2ofchild1Template = new TableTemplate("child2ofchild1Cell1", "child2ofchild1Cell2", 180, 100);


            private void SetNodeSelected(string selectedNodeValue)
                RadTreeNode selectedNode = RadTreeView1.FindNodeByValue(selectedNodeValue);

                if (selectedNode != null)
                    selectedNode.Selected = true;

Back to Top