Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a node in Treeview ( ASP.NET ) and Highlight

Status
Not open for further replies.

ramindya1234

Programmer
Apr 7, 2010
2
0
0
US
****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);
}
}

}
 
all the information is on the client, so why not preform the search/presentation adjustments there? This would create a better user experience and save a round trip to the server. js libraries like jquery & prototype make this real simple.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
all the information is on the client, so why not preform the search/presentation adjustments there? This would create a better user experience and save a round trip to the server. js libraries like jquery & prototype make this real simple.
Good luck doing that with the html generated by the TreeView control...[smile]

But yes, you're right that is a much better approach.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top