0 Replies - 758 Views - Last Post: 16 October 2009 - 02:51 PM Rate Topic: -----

#1 jmprattmba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-November 08

Search Treeview text

Post icon  Posted 16 October 2009 - 02:51 PM

I have a small C# ASP.NET web app that includes a TreeView called "tvOptions" which is databound to an XML file. I need to be able to have the user input a search term in the text box "txtSearch", click the button "btnSearch", and locate ALL nodes that contain that text. By "locate" I'd like to see the node expanded and perhaps change the text color.

I've tried several code snippets but nothing works well and I'm beginning to suspect I'm not calling the procedure properly. Here's my latest code:


public partial class _Default : System.Web.UI.Page 
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}
	protected void btnNodeTextSearch_Click(object sender, EventArgs e)
	{
		FindByText();
	}
	protected void FindByText()
	{
		TreeNodeCollection nodes = tvOptions.Nodes;
		foreach (TreeNode n in nodes)
		{
			// if the text properties match, color the item 
			if (n.Text.Contains(this.txtSearch.Text) == true)
			{
				n.Parent.Expanded = true;
				lblResult.Text = n.Text;
			}
			else
			{
				n.Expand();
				lblResult.Text = "Not Found";
			}
		}
	}
}




Many thanks for your kind help.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1