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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a treeview node index to a querystring 1

Status
Not open for further replies.

mrathi

Technical User
Oct 27, 2003
115
0
0
US
Hi There.

I have a treeview that i am population with data from a database.
Each treenode will have a dirrerent navigateUl assigned to it and the tree has multiple levels.

Basically i understand that the index structure of the tree works like 1.0.3 which will mean that i have the second node -> first node -> fourth node selected.

What i want to know is how do i add a section onto the navigateurl so that when i go to a different page the expanded nodes will still be expanded.
ex page2.aspx?s=1.0.3

Thanks a million
 
Where and how do you populate the NavigateURL so that it knows to go to Page2.aspx?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I don't, that is what I want to know. So far I am populating my treeview from several tables from a database. When I said navigate url, I meant that by clicking on any node in the treeview, I want to open a page specific to the selected node.

Thanks for your reply.
 
OK - I haven't used the treeview control myself yet so I'm just taking a few guesses here.

Could you add an "onclick" attribute (or if there is something similar to navigateURL) to each item when it is created? - is there a ItemCreated method or anything like that?

If not I'll have to have a look into using the control myself (probably should do that anyway!).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
There is a selectedIndexChange event for the treeview control, but I still don't know how to accomplish what I am trying to..

Thanks
 
OK - I've just downloaded the controls.

I notice that there is a NavigateURL for each node - maybe this could be set in the PreRender method (probably not the recommended way but as I've just set it up myself I haven;t had time to check it out properly yet).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Actually could you post your code of how you bind the treeview to the data as there appears to be several ways this may be possible.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I populate my dataset and then using the dataset I populate my treeview.

For Each rowAuthor In ds.Tables("Authors").Rows
nodeAuthor = New TreeNode
nodeAuthor.Text = rowAuthor("au_lname") & ", " _
& rowAuthor("au_fname")
TreeView1.Nodes.Add(nodeAuthor)
For Each rowTitle In rowAuthor.GetChildRows("AuthorTitle")
nodeTitle = New TreeNode
nodeTitle.Text = rowTitle("Title")
nodeAuthor.Nodes.Add(nodeTitle)
Next
Next

Thanks
 
Thanks for all the responses. I figured it out. It was not that bad.

Thanks
 
Oops, I figured out how to make the nodes hyperlink and how to transfer values of the nodes in querystring, however, I am still stuck at my initai question.

How to keep the status of the treeview on the new page. Also, I want to know if I should make the database connection to populate the tree on every page, or is there a better way.

Thanks
 
Thanks for all the responses. I figured it out. It was not that bad.

I take it you did something like:

Code:
nodeTitle = New TreeNode
nodeTitle.Text = rowTitle("Title")
nodeTitle.NavigateURL = "AnotherPage.aspx?SelectedItem=" & nodeTitle.ID
nodeAuthor.Nodes.Add(nodeTitle)

Is this correct?

If so, then on "AnotherPage.aspx" you just have to expand the node that is passed through by the "SelectedItem" querystring.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yes, that is how I am doing. However on another page I need the rowTitle("Title") and that is what I am passing. If a user clicks on the fourth level on the treeview, in the querystring, I pass all the four values(in text, not node).

Thanks
 
I am sorry, I still haven't figured out the follwoing:

What i want to know is how do i add a section onto the navigateurl so that when i go to a different page the expanded nodes will still be expanded.
ex page2.aspx?s=1.0.3

Thanks
 
mrathi,

I am wondering if you got the solution to your problem.
I am also haveing the identical problem, how to keep my tree view nodes expanded when user click on the tree node to navigate to different page.

I would appreciate if you share your solution.


Thanks
 
I am sorry, I did not get any solution so far. I got busy with some other stuff. It looks like it will be a tough task to figure that out.

Good Luck. By the way, if you get a solution, will you please let me know.

Thanks
 
You would simply use the querystring value like I suggested in my last post...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Hi mrathi and others!

I've had the same problem, and I think I may have figured out a solution WITHOUT using querystrings.

I created a recursive function to solve the problem. The code is below:
Code:
private string GetNavigatedNodeIndex( TreeNodeCollection tnc ) 
{
	foreach ( TreeNode tn in tnc ) 
	{
		if ( tn.NavigateUrl.Length > 0 && (
			Request.Url.AbsolutePath.IndexOf( tn.NavigateUrl ) > -1 ||
			Request.Url.AbsoluteUri.IndexOf( tn.NavigateUrl ) > -1 ) )
		{
			return( tn.GetNodeIndex() );
		}

		string index = GetNavigatedNodeIndex( tn.Nodes );
		if ( index != null ) 
		{
			tn.Expanded = true;
			return( index );
		}
	}
	return( null );
}
Now, just handle the TreeView's PreRender event and call the function:
Code:
private void TreeView1_PreRender(object sender, System.EventArgs e)
{
	TreeView1.SelectedNodeIndex = GetNavigatedNodeIndex( TreeView1.Nodes );
}

Let me know if this solution works for you! Good luck!
 
Sorry, just realized that in function GetNavigatedNodeIndex, you don't need to check Request.Url.AbsolutePath, since Request.Url.AbsoluteUri contains the path. So the correct code is:

Code:
private string GetNavigatedNodeIndex( TreeNodeCollection tnc ) {
...
        if ( tn.NavigateUrl.Length > 0 && 
            Request.Url.AbsoluteUri.IndexOf( tn.NavigateUrl ) > -1 )
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top