Click here to Skip to main content
Click here to Skip to main content

A "stateful" scrollable panel

, 8 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
A scrollable panel retaining its scroll position across postbacks.

After postback before restoring scroll position

Scroll position restored

Introduction

Often, we wrap a GridView inside a panel with the "overflow:scroll" style set and expect the user to scroll and select a GridView item to work with. The problem is that whenever the page is posted back, the selected item is not visible because the panel scroll position has been reset.

At our latest project, we needed such a functionality, but could not find anything similar, so we decided to code it ourselves. The result is the "StatefulScrollPanel", a custom control that retains its scroll position across postbacks.

Background

Basically, all we need is some basic knowledge of JavaScript and an elementary overriding of two of asp:Panel's own methods. We start with two hidden fields that accompany our control and whose purpose is to keep track of the scrollTop and scrollLeft properties of the panel (div). These, along with the method that keeps track of the control's scroll position, are rendered just before our control, so we override the Render(HtmlTextWriter) method. Since we do want the control to be displayed with no trouble at design time, we simply delegate to the parent if in design mode:

protected override void Render(HtmlTextWriter writer)
{                        
    if (DesignMode)
    {
        base.Render(writer);
        return;
    }

    /*
     * Hidden inputs to persist scroll position(s)
     */
    writer.Write("<input type='hidden' name='" + 
                ScrollXInputId + "' id='" +
                ScrollXInputId + "' value='" + 
                ScrollXInputValue + "'/>");
    
    writer.Write("<input type='hidden' name='" + 
                 ScrollYInputId + "' id='" + 
                 ScrollYInputId + "' value='" + 
                 ScrollYInputValue + "'/>");
    
    //Trace method that populates the hidden inputs
    writer.Write("<script language="'Javascript'">function " + 
                 TraceMethod + "{" + GetElementAccessor(ScrollXInputId) + 
                 ".value = " + GetElementAccessor(ClientID) +  
                 ".scrollLeft;" + GetElementAccessor(ScrollYInputId) +  
                 ".value = " + GetElementAccessor(ClientID) + 
                 ".scrollTop;}" + "</script>");

    //Delegate core rendering to momma
    base.Render(writer);                      
}

The GetElementAccessor(String) method, and the Scroll[X|Y]InputId, Scroll[X|Y]InputValue, and TraceMethod properties are helpers used to reduce the code we have to write. Take a look at the control's code to see their definitions.

All we have to do now is to register the trace method with our control during the OnLoad(EventArgs) event and set the scroll position:

protected override void OnLoad(EventArgs e)
{
    //To enable scrolling in the first place
    base.Style.Add(HtmlTextWriterStyle.Overflow, "scroll");
    
    //Register our trace method
    base.Attributes.Add("onScroll", TraceMethod);            

    //On start up set scroll position(s) to the existing one
    Page.ClientScript.RegisterStartupScript(GetType(), AdjustScriptKey, 
        GetElementAccessor (ClientID) + ".scrollLeft = " + 
                ScrollXInputValue + ";" + 
            GetElementAccessor(ClientID) + ".scrollTop = " + 
                ScrollYInputValue + ";", 
        true);
    
    base.OnLoad(e);
}

That's it!

Using the code

Using the control is as simple as using the asp:Panel control itself. We drop it in our aspx pages, and set its Width and Height properties:

<cc1:StatefullScrollPanel ID="StatefullScrollPanel1" 
        runat="server" Width="250px" height="105px">
Panel elements go here           
</cc1:StatefullScrollPanel>

Note that we do not have to explicitly set the overflow style since the control takes care of that. Also note that we can use as many controls we need on the same page since the produced code is unique for each control instance. We have tested the code with Firefox 2.0 and IE 6.0, and seems to be working fine.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

s.oikonomopoulos
Architect www.neurocom.gr
Greece Greece
Working in sunny Greece as a software engineer since 1998. Currently spending most of my time coding solutions for the telecommunications sector using Java, .Net, Oracle and PostgresSQL.

Comments and Discussions

 
QuestionUnable to get this working in VS 2012 PinmemberMember 880869321-May-14 9:12 
GeneralThis was great Pinmemberlaurie nash17-Sep-09 6:22 
GeneralHaving a problem with this control inside a Multiview Pinmembersmolino18-Jun-08 7:36 
GeneralRe: Having a problem with this control inside a Multiview Pinmembers.oikonomopoulos18-Jun-08 9:56 
GeneralExactly what I need PinmemberMember 23813574-May-08 2:42 
NewsGreat Control - Improvement for only Vertical Scrolling PinmemberMarc Riffel26-Mar-08 4:53 
QuestionNext step - how to force scroll position to top or bottom programmatically? Pinmemberehinton3-Jan-08 7:30 
GeneralRe: Next step - how to force scroll position to top or bottom programmatically? Pinmembers.oikonomopoulos3-Jan-08 23:57 
GeneralRe: Next step - how to force scroll position to top or bottom programmatically? Pinmemberehinton4-Jan-08 4:51 
GeneralFixed: Re: Next step - how to force scroll position to top or bottom programmatically? Pinmemberehinton4-Jan-08 7:49 
GeneralRe: Fixed: Re: Next step - how to force scroll position to top or bottom programmatically? Pinmembers.oikonomopoulos7-Jan-08 3:28 
GeneralProblem (and fix) when hidden in a multiview PinmemberMrT99916-Oct-07 6:36 
Generalstateful scrollable panel PinmemberLarry Swarts26-May-07 6:38 
GeneralDoesn't Seem to Work in AJAX UpdatePanel Pinmembercavedog68-May-07 4:45 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel Pinmembers.oikonomopoulos8-May-07 23:28 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel Pinmemberzetarho11-May-07 0:48 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel Pinmembers.oikonomopoulos15-May-07 2:17 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel PinmemberMiaF18-May-07 5:29 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel Pinmembers.oikonomopoulos22-May-07 3:30 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel PinmemberMiaF23-May-07 3:11 
GeneralRe: Doesn't Seem to Work in AJAX UpdatePanel Pinmembersouthernpost28-Jun-07 7:18 
QuestionThis really works! PinmemberHans Wijtvliet5-Apr-07 5:32 
GeneralJust what I've been looking for! Pinmembersth_Weird29-Mar-07 21:05 
Generali have a problem PinmemberEEmaan14-Mar-07 20:54 
GeneralRe: i have a problem Pinmembers.oikonomopoulos15-Mar-07 11:29 
GeneralGreat solution PinmemberCurt Ophoven21-Nov-06 10:35 
GeneralSimple solution! PinmemberMarc Brooks8-Nov-06 14:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 8 Nov 2006
Article Copyright 2006 by s.oikonomopoulos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid