Hi,
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 asp.net code:
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
</telerik:RadScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers="true" runat="server">
<ContentTemplate>
<telerik:RadTreeView ID="RadTreeView1" Runat="server" SingleExpandPath="true"
OnNodeExpand="TreeView_NodeExpand" OnNodeCollapse="TreeView_NodeCollapse"
OnNodeClick="TreeView_NodeClick">
</telerik:RadTreeView>
</ContentTemplate>
</asp:UpdatePanel>
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);
nodeRow.Cells.Add(firstCell);
TableCell secondCell = new TableCell();
secondCell.Style["width"] = m_cell2Size + "px";
secondCell.DataBinding += new EventHandler(secondCell_DataBinding);
nodeRow.Cells.Add(secondCell);
nodeTable.Rows.Add(nodeRow);
container.Controls.Add(nodeTable);
}
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;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//first time, load root node
LoadTreeRoot();
}
}
private void LoadTreeRoot()
{
RadTreeView1.Nodes.Add(
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
SetNodeSelected(expandedNodeValue);
}
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
SetNodeSelected(collapsedNodeValue);
}
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
SetNodeSelected(clickedNodeValue);
}
#endregion
#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
rootNode.Nodes.Clear();
//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);
child1Template.InstantiateIn(child1);
rootNode.Nodes.Add(child1);
if ((targetNodeValue != "root" && targetNodeValue != "child1") ||
(targetNodeValue == "child1" && expanded))
{
//also expand this child node
child1.Expanded = true;
//add the childofchild nodes
//first
RadTreeNode child1ofchild1 = new RadTreeNode { Value = "child1ofchild1" };
TableTemplate child1ofchild1Template = new TableTemplate("child1ofchild1Cell1", "child1ofchild1Cell2", 180, 100);
child1ofchild1Template.InstantiateIn(child1ofchild1);
child1.Nodes.Add(child1ofchild1);
//second
RadTreeNode child2ofchild1 = new RadTreeNode { Value = "child2ofchild1" };
TableTemplate child2ofchild1Template = new TableTemplate("child2ofchild1Cell1", "child2ofchild1Cell2", 180, 100);
child2ofchild1Template.InstantiateIn(child2ofchild1);
child1.Nodes.Add(child2ofchild1);
}
}
//databind
RadTreeView1.DataBind();
}
private void SetNodeSelected(string selectedNodeValue)
{
RadTreeNode selectedNode = RadTreeView1.FindNodeByValue(selectedNodeValue);
if (selectedNode != null)
{
selectedNode.Selected = true;
}
}
#endregion
}
}