Posts
6
Comments
22
Trackbacks
0
January 2010 Entries
ASP.Net TreeView - scroll to the expanded node
Technorati Tags: ,,

When we expand TreeView node JavaScript function TreeView_ToggleNode gets executed (it’s part of the TreeView control itself).

So we will “override” this function and add our own functionality to perform scroll to the expanded node.

To find source code of TreeView_ToggleNode function you can use IE Developer Toolbar or Firebug.

I removed some code from this function that I don’t need to make it shorter. Here is the result. Everything should be pretty self explanatory from the code.

 

Here is the modified function, just add it in you page:

   1:          TreeView_ToggleNode = function (data, index, node, lineType, children)
   2:          {
   3:              var img = node.childNodes[0];
   4:              var newExpandState;
   5:   
   6:              if (!$('#' + children.id).is(':visible'))
   7:              {
   8:                  children.style.display = "block";
   9:                  newExpandState = "e";
  10:                  img.src = data.images[5];
  11:                  img.title = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
  12:   
  13:                  //* 'divTreeViewScrollTo' is the id of the div where we put our tree view
  14:                  if (($(node).position().top + $(children).height() + 10) > $('#divTreeViewScrollTo').height())
  15:                  {
  16:                      if ($(children).height() > $('#divTreeViewScrollTo').height())
  17:                          $('#divTreeViewScrollTo').scrollTop($(node).position().top + $('#divTreeViewScrollTo').scrollTop() - 10);
  18:                      else
  19:                          $('#divTreeViewScrollTo').scrollTop($(children).height() + $('#divTreeViewScrollTo').scrollTop() + 20);
  20:                  }
  21:   
  22:                  $('#divTreeViewScrollTo').scrollLeft($(node).position().left + $('#divTreeViewScrollTo').scrollLeft() - 10);
  23:              }
  24:              else
  25:              {
  26:                  children.style.display = "none";
  27:                  newExpandState = "c";
  28:                  img.src = data.images[4];
  29:                  img.title = data.expandToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
  30:              }
  31:   
  32:              data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);
  33:          }

 

Result before node expanded and after node has been expanded ad scrolled top and left:

 TreeView_BeforeNodeExpanded TreeView_AfterNodeExpanded

 

Ask a question is something is not as clear.

Happy programming!

Posted On Wednesday, January 27, 2010 1:05 AM | Feedback (3)
ASP.Net TreeView - scroll to the selected node
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 | Feedback (7)
Get ASP.NET TreeView – get selected node id on the client side
Technorati Tags: ,,
TreeViewClientID_Data.selectedNodeID.value 

where TreeViewClientID_Data  is the name of the TreeView javascript object that has “selectedNodeID” property.

We can easily load this object and get selected node id like this:

   1:          Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(EndLoadedHandler);
   2:   
   3:          function EndLoadedHandler(sender, args)
   4:          {
   5:                   //* make sure that object is fully loaded
   6:                    var data = eval('<%=tvwScrollTo.ClientID %>_Data');
   7:                    if(!data)
   8:                         var selectedNodeID = data.selectedNodeID.value;
   9:           }

we have to make sure that TreeViewClientID_Data  is loaded.

or

document.getElementById('TreeViewClientID_SelectedNode').value 

where TreeViewClientID_SelectedNode is the id of the hidden field where selected node id is stored

 

Explained:

TreeViewClientID_Data has a lot of useful information. You can find this object in the source of the page:

TreeView_Data

We can clearly see that selected node id is stored in the hidden field with the name 'TreeViewClientID_SelectedNode'

So, lets grab it:

document.getElementById('TreeViewClientID_SelectedNode').value 

or dynamically

document.getElementById('<%=tvwScrollTo.ClientID %>_SelectedNode').value

 

Happy programming!

Posted On Monday, January 25, 2010 11:04 PM | Feedback (1)
Tag Cloud