Finding Nodes at Runtime
To find a Node by Text, Value or Attribute, use the methods FindNodeByText(), FindNodeByValue() and FindNodeByAttribute(). Both RadTreeView and RadTreeView.Nodes have all three methods. In addition, RadTreeView has a FindNodeByUrl() method. For a step by step tutorial on using these find methods see Tutorial: Finding Nodes.
To use the code examples below, include a reference to Telerik.Web.UI in the "using" (C#) or the "Imports" (VB) section of code.
FindNodeByText()
The example below searches for a Node with a Text property of "Product Categories".
protected void btnFind_Click(object sender, EventArgs e)
{
RadTreeNode foundNode = RadTreeView1.FindNodeByText("Product Categories");
if (foundNode != null)
{
tbResults.Text = foundNode.Text;
}
}
FindNodeByValue()
The example below searches for a Node with a Value property of "1234".
protected void btnFind_Click(object sender, EventArgs e)
{
RadTreeNode foundNode = RadTreeView1.FindNodeByValue("1234");
if (foundNode != null)
{
tbResults.Text = foundNode.Text;
}
}
FindNodeByAttribute()
In the example below several Nodes are added to the RadTreeView. The first Node, "root1" has an attribute added to it. When a button is clicked, the FindNodeByAttribute() method looks for the Node with the identical key and value pair.
using Telerik.Web.UI;
namespace RadControls_NodesFind
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadTreeNode root1 = new RadTreeNode("root1");
root1.Attributes.Add("My Key", "My Value");
RadTreeView1.Nodes.Add(root1);
RadTreeNode root2 = new RadTreeNode("root2");
root2.Nodes.Add(new RadTreeNode("child1"));
root2.Nodes.Add(new RadTreeNode("child2"));
root2.Nodes.Add(new RadTreeNode("child3"));
RadTreeView1.Nodes.Add(root2);
RadTreeNode root3 = new RadTreeNode("root3");
RadTreeNode child3 = new RadTreeNode("child3");
child3.Nodes.Add(new RadTreeNode("child node under child3"));
root3.Nodes.Add(child3);
RadTreeView1.Nodes.Add(root3);
}
}
protected void btnFind_Click(object sender, EventArgs e)
{
RadTreeNode foundNode = RadTreeView1.FindNodeByAttribute("My Key", "My Value");
if (foundNode != null)
{
tbResults.Text = foundNode.Text;
}
}
}
}