Intersoft WebGrid Documentation
Walkthrough: Maintaining scroll position
See Also Send comments on this topic.

Glossary Item Box

This walkthrough will show how to maintain the scroll position between page Postbacks.

During this walkthrough, you will learn how to do the following:

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to the Microsoft Access NorthWind database.
  • Visual Studio 2005 Application.

 Step-By-Step Instructions

  1. Bind WebGrid to AccessDataSource.
  2. In the InitializeLayout event, attach a client side event on 'OnBeforeRequest' event.
  3. C# Copy Code
    private void WebGrid1_InitializeLayout(object sender,
    ISNet.WebUI.WebGrid.LayoutEventArgs e)
    {
        e.Layout.ClientSideEvents.OnBeforeRequest = "DoBeforeRequest";
    }
    
  4. On the client-side, write these client script functions.
  5. JavaScript Copy Code
    function DoBeforeRequest(gridId,action)
    {
        // collecting the last position
        // of the vertical scrollbar
        // by obtaining the scrollTop property
            
        var grid = ISGetObject(gridId);
            
        // obtain the scrollTop property 
        // of the vertical scrollbar
        var vl = grid.RootTable.GetElement(WG40.BODY, WG40.HTMLDIV).scrollTop + "";
            
        // only send the scrollTop property
        // of the server if the action performed
        // by the grid OTFPB is 'Refresh'
        if(action == "Refresh")
        {
            // sending over the scrollTop property
            // to the server by using AddInput method
            grid.AddInput("scrollTopPos", vl);
        }
        
        return true;
    }
     
    function ApplyLatestScroll(x)
    {
        // set back the previous scrollTop property or
        // vertical scroll position with 
        // the parameter sent from the server side
            
        var grid = ISGetObject("WebGrid1");
            
        // manually configure the vertical scroll position
        grid.RootTable.GetElement(WG40.BODY, WG40.HTMLDIV).scrollTop = x;        
        return true;       
    }
     
    function DoRefresh()
    {
        // invoke data refresh in the grid
        // by calling Refresh()
            
        // the data refresh process is performed
        // using OnTheFly PostBack
       
    var grid = ISGetObject("WebGrid1"); grid.RefreshAll(); return true; }
  6. Invoke the InitializePostBack server side event.
  7. C# Copy Code
    private void WebGrid1_InitializePostBack(object sender,
    ISNet.WebUI.WebGrid.PostbackEventArgs e)
    {
        // only perform the below function
        // when the last action performed by the
        // grid OTFPB is 'Refresh'
        if(e.Action == "Refresh")
        {
            // prepare the parameter to be 
            // passed over to the client side
            // in order to set back the previous
            // vertical scroll position
            FunctionParameter[] parm = new FunctionParameter[1];
            parm[0].Type = "x";
            
            // obtain the value passed from the client
            // side using AddInput method
            parm[0].Value = Request.Form["scrollTopPos"];
            
            // invoke the js function 
            // in order to set the vertical scroll position
            WebGrid1.ClientAction.InvokeScript("ApplyLatestScroll", parm);
        }
    }
    

See Also

©2012 Intersoft Solutions Corp. All Rights Reserved.