Showing posts with label Retain scrool position in tree view. Show all posts
Showing posts with label Retain scrool position in tree view. Show all posts

Tuesday, June 9, 2009

How to retain scrool position in tree view after postback

I have been looking for an elegant solution to this problem for a while here is the cleanest one I have found so far:

///
/// Handles the click event when a tree node is selected
///

/// treeview reference
///
protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
{
// Cast the sender to a treeview
TreeView T = sender as TreeView;
//Execute required code
//Finally register the start of JScript to load to selected treeview
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "selectNode", "var elem = document.getElementById('" + T.ClientID + "_SelectedNode');var node = document.getElementById(elem.value);node.scrollIntoView(true);elem.scrollLeft=0;", true);
}