You could try using the FindNode method of the TreeView control, Examples
here this will return the TreeNode, then you can call the Expand method of the node, Example
here
It depends on what basis you have to find the node, if you know the ValuePath of the node then you can use FindNode like this -
TreeNode
node= TreeView1.FindNode("Root/Leaf");
node.Selected = true;
else if you want to select a node on the basis of node text, then you will have to iterate through the Treeviews NodesCollection and match on the basis of text and then select the matching node. If you need to do like this then i can provide the function
i used.
else if you know the index of your node then you can do like this
TreeView1.Nodes[4].Selected =
true;
Similar to what Mikael said(but I think that will not work as TreeView.SelectedNode is only a Get property)
Hope it helps.....
When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit for being helpful (and makes search more relevant too).
****THIS IS A ASP.NET Web APPLICATION NOT WINDOWS FORMS*************
Problem/ Question: To search a child node anywhere in the treeview. I need to type a Node name in the textbox and search the node name in the Treeview and highlight the node name on finding. I don’t how to do it. I tried for sometime but didn’t find the
solutions.So, can any body give some idea ?
FYI : I have created a ASP.NET Web application treeview to populate Parent nodes and corresponding child nodes for each Parent Node. The user can add any number of child nodes in the treeview.
Given below is the code that I have done till now for populating the treeview for any number of childs and child levels.
Code behind file :
public partial class _Default : System.Web.UI.Page
if (!Page.IsPostBack)// Checks if the page is loaded first time
{
PopulateRootLevel(); // Populates the root or parent
}
}
public void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(@"");
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber
where dh.ParentID = 0 ", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}
public void PopulateSubLevel(int ParentID, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber
where dh.ParentID=@ParentID", objConn);
Pztydr
Member
744 Points
210 Posts
Treeview - Select Node Programatically
Jan 23, 2007 11:54 AM|LINK
Hi,
Can anyone please provide some insight into how I could select a specific node programatically, a code snippet would be nice.
Regards..
Peter..
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Treeview - Select Node Programatically
Jan 23, 2007 12:21 PM|LINK
TreeNodeCollection nodes = YourTreeView.Nodes;
YourTreeView.SelectedNode = nodes[3];
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Hope4sun
Participant
1139 Points
201 Posts
Re: Treeview - Select Node Programatically
Jan 23, 2007 12:22 PM|LINK
You could try using the FindNode method of the TreeView control, Examples here this will return the TreeNode, then you can call the Expand method of the node, Example here
I hope this helps you.
Treeview FindNode TreeNode
akjoshi
Contributor
3636 Points
615 Posts
Re: Treeview - Select Node Programatically
Jan 23, 2007 02:26 PM|LINK
Hi peter,
It depends on what basis you have to find the node, if you know the ValuePath of the node then you can use FindNode like this -
TreeNode
node= TreeView1.FindNode("Root/Leaf");node.Selected = true;
else if you want to select a node on the basis of node text, then you will have to iterate through the Treeviews NodesCollection and match on the basis of text and then select the matching node. If you need to do like this then i can provide the function i used.
else if you know the index of your node then you can do like this
TreeView1.Nodes[4].Selected =
true;Similar to what Mikael said(but I think that will not work as TreeView.SelectedNode is only a Get property)
Hope it helps.....
Pztydr
Member
744 Points
210 Posts
Re: Treeview - Select Node Programatically
Jan 23, 2007 10:01 PM|LINK
Hi,
Have resolved my issue, found another entry here http://forums.asp.net/thread/948974.aspx that pointed me in the right direction..
Regards..
Peter.
ramito.kumar
Member
4 Points
3 Posts
Find Node name in Treeview (ASP.NET Web application )
Apr 06, 2010 07:30 PM|LINK
****THIS IS A ASP.NET Web APPLICATION NOT WINDOWS FORMS*************
Problem/ Question: To search a child node anywhere in the treeview. I need to type a Node name in the textbox and search the node name in the Treeview and highlight the node name on finding. I don’t how to do it. I tried for sometime but didn’t find the solutions.So, can any body give some idea ?
FYI : I have created a ASP.NET Web application treeview to populate Parent nodes and corresponding child nodes for each Parent Node. The user can add any number of child nodes in the treeview.
Given below is the code that I have done till now for populating the treeview for any number of childs and child levels.
Code behind file :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)// Checks if the page is loaded first time
{
PopulateRootLevel(); // Populates the root or parent
}
}
public void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(@"");
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID = 0 ", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);
}
public void PopulateSubLevel(int ParentID, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID=@ParentID", objConn);
objCommand.Parameters.Add("@ParentID", SqlDbType.Int).Value = ParentID;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}
public void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}
public void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["CustomerName"].ToString();
tn.Value = dr["RowId"].ToString();
nodes.Add(tn);
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}
}