Geeks With Blogs
Stuff I do my notepad
Technorati Tags: ,,

If we have TreeView inside of the div with the fixed width and height we want selected node to be visible after postback.

Here is small JavaScript function that does this (you don’t have to use jQuery as I did to get the result):

   1:          function ScrollToSelectedNode()
   2:          {
   3:              //* get selected node id
   4:              //* 'tvwScrollTo' - is our tree view id
   5:              var selectedNodeID = $('#<%=tvwScrollTo.ClientID %>_SelectedNode').val();
   6:   
   7:              if (selectedNodeID != '')
   8:              {
   9:                  //* calculate selected top an left position (http://docs.jquery.com/CSS/position)
  10:                  //* in order to get correct relative position remember to set div position to absolute
  11:                  var scrollTop = $('#' + selectedNodeID).position().top;
  12:                  var scrollLeft = $('#' + selectedNodeID).position().left;
  13:                  
  14:                  //* here 'divTreeViewScrollTo' is the id of the div where we put our tree view
  15:                  $('#divTreeViewScrollTo').scrollTop(scrollTop);
  16:                  $('#divTreeViewScrollTo').scrollLeft(scrollLeft);
  17:              }
  18:          }

Run this function on postback:

   1:          protected void tvwScrollTo_SelectedNodeChanged(object sender, EventArgs e)
   2:          {
   3:              ScriptManager.RegisterStartupScript(Page, Page.GetType(), "ScrollToSelectedNode", "ScrollToSelectedNode();", true);
   4:          }

Result before node selected and after:

TreeView_NodeSelect TreeView_NodeSelect_AfterPostback

Posted on Wednesday, January 27, 2010 12:25 AM | Back to top


Comments on this post: ASP.Net TreeView - scroll to the selected node

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
I'm having the selected node problem and 'im trying to use your script but when i run it, throws me the next error:

Microsoft JScript: Syntax error, unrecognized expression: #[object Object]

Do you have and idea of whats going on?

Thanks
Left by Luis Vega on Jul 11, 2011 1:14 PM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
is there a way to get the correct relative position without setting the div to absolute?
Left by Luis Vega on Jul 13, 2011 11:23 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
If the PlayStation 3 feels extremely hot or the system will power off and the power light flashes red, this could be an indicator there is a temperature issue with the pink ps3 controller dualshock and it is not able to keep the system cool. Please attempt the following troubleshooting steps.
Left by gamebacker on Oct 18, 2011 3:28 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
yes,you have solved the problems confused me for a long time,before,I can not handle the issue related with ASP.NET TreeView jQuery
Left by wordpress website hosting on Dec 18, 2011 5:48 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
YES,YOU HAVE SOLVED THE PROBLEM CONFUSED FOR A LONG TIME,before,I am not well in the handling with jquery coding
Left by wordpress website hosting on Dec 18, 2011 5:50 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
YES,YOU HAVE SOLVED THE PROBLEM CONFUSED FOR A LONG TIME,before,I am not well in the handling with jquery coding
Left by wordpress website hosting on Dec 18, 2011 5:50 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
You are a star
Left by Eric Z on Mar 07, 2012 7:12 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
Great job.helped a lot.
Left by Carol on Apr 10, 2013 2:31 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
excellent code.... thanks a lot
Left by naga vara prasad on Dec 04, 2013 5:24 AM

# re: ASP.Net TreeView - scroll to the selected node
Requesting Gravatar...
Thanks, This helped me to fix my problem.
Left by Santosha Epili on Jan 12, 2015 2:20 AM

Your comment:
 (will show your gravatar)


Copyright © vladimirl | Powered by: GeeksWithBlogs.net