Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 03, 2007 02:19 PM by cartoon
Nov 03, 2006 02:10 PM|LINK
Now this may seem like a very simple question and I'm hoping that is just me being thick but....
I have tried to update my website from version 1.0 of the adapters to 3.0 as I want to take advantage of the treeview improvements (namely viewstate). I have got one treeview that is placed on the sites masterpage so it should be fairly easy to implement
this but I'm having problems with is what to put in the OnTreeSelection Event. All my treeview is for is for navigation, no use of AJAX is needed so all I need to do is navigate to the url indicated in the sitemap that is attached to the treeview control.[:D]
Can anyone help?
Nov 03, 2006 07:16 PM|LINK
I'll be happy to try to help but I need to understand the situation a little better.
It seems like you are saying that you have a master page that has an <asp:TreeView> that is bound to a <asp:SiteMapDataSource>. In your sitemap you have siteMapNode elements that each have a unique url value. In that case, the tree should render with simple
links that help you navigate from page to page.
It doesn't seem like you need to handle selection events, etc. The navigation should simply work.
I'm guessing that the real issue is that you want the tree to expand automatically to some node, perhaps the node corresponding to the current page. Is that right? If so, I can suggest some techniques that you can use.
The view state enhancements only pertain to situation where a page posts back to itself. It doesn't pertain to navigating between different pages (i.e., it only pertains to HTTP POST, not HTTP GET). The tree can't re-expand to its old state if you move
between pages because that expansion info exists in the posted info (when the page posts back to itself). Without that posted info there is no expansion map to reexpand to.
Clear as mud?
Nov 04, 2006 02:10 PM|LINK
Your guess was correct. People have noted that it is somewhat annoying that when they use the treeview to navigate to another page it collapses back down again. Can we not store the details in the session variable and then re-expand it again? Because
the treeview is on the masterpage and its relative position on the page doesn't change, it makes matters worse as it looks like it should stay expanded.
Thanks for your help by the way!
Nov 04, 2006 02:35 PM|LINK
Session persistence isn't something we can use because it completely fails in situations where you deploy the site to a web farm. In those situations you can overcome the Session problem by using a database for your session store (you end up writing your
own session store provider class) but that isn't something we can do in this kit.
Let's see if there is a simpler way to handle this problem.
I know that it is easiest if it is handled automatically in the kit but in this case it may be necessary for you to do some extra coding in your own pages to make this work the way you want. Let me explain. I bet you find that it isn't too hard to do.
First, in your <asp:treeview> you need to add the OnTreeNodeDataBound attribute. Set this to the name of a method that you will add to your code behind (on whatever page or master page contains the tree). In your event handler method for OnTreeNodeDataBound you
will be passed an argument that has a reference to the current tree node that was just bound to its data. Take a look at
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.treenodeeventargs(VS.80).aspx. You can compare e.Node.NavigateUrl with the current url of the page you are on by doing something like this:
if (Request.Url.PathAndQuery == e.Node.NavigateUrl)
e.Node.Selected = true;
Basically, you are setting the Selected property to true on the tree node that corresponds to the current page.
Then, you need to expand the tree so that the selected tree node is showing. To do that, use a method like this:
private void ExpandToNode(TreeView treeView, TreeNode item)
if ((treeView != null) && (item != null))
TreeNode nodeToExpand = item.Parent;
while (nodeToExpand != null)
nodeToExpand.Expanded = true;
nodeToExpand = nodeToExpand.Parent;
And call it from your page (or master page) PreRender event (Page_PreRender) like this:
I've done this in the past so I know this implementation pattern works. However, I've not tested the code I've posted here so it might need to be tweaked slightly. Still, I bet it gives you the right idea.
The tree will expand to the selected node which will correspond to the current page. The tree will not retain its overall expansion state so you may see some of the parts of the tree collapse when you switch pages. Still, this solution looks pretty attractive
when I use it. I hope you find that it is an acceptable solution to your situation, too.
Nov 07, 2006 02:31 PM|LINK
Would it be possible to pass all the expanded nodes (as a string array) in a url query string so that we could attempt to persist session state that way? Thought it might be possible to catch the parameters in the masterpages page_Load event and then expand
the tree that way??
Does this seem do-able
Nov 07, 2006 02:32 PM|LINK
Does this seem do-able?
Nov 10, 2006 01:06 PM|LINK
Sep 03, 2007 02:19 PM|LINK
I have one TreeView in a Master Page.Now when i'm navigating to child pages that time the master page is not able to hold to treeview session. I tried with the event "OnDataBinding" and "SelectedNodeChanged" but
it is not firing those events.
so i tried with your code but in <asp:treeview> i'm not geting any event like "OnTreeNodeDataBound"
where i need to put the piece written below:
Thanks & Regards,
in <asp:treeview> i'm not geting any event like "OnTreeNodeDataBound"