HOWTO: Persist Scroll Position in Panel Control on Postback part II

Discussion in 'ASP .Net Web Controls' started by joegoeke@gmail.com, Jan 12, 2007.

  1. Guest

    If found this great posting from Elroyskimms and wanted to continue it,
    but the tread has been closed because it's older than 60 days or
    such... seems like a short window.

    Anyway, since Microsoft can't seem to get it's act together on
    documenting anything, I am following up with a post for those who would
    like a C# answer and also it uses the vertical scroll instead of the
    horizontal one in Elroyskimms original post. I've summerized his post
    here as well.

    Enjoy!

    -----------

    I have a Panel control in a user control. This Panel contains several
    other controls with buttons and dropdownlists. Anytime the user would
    click one of these button or dropdown controls, a postback would occur
    and the Panel scroll position would be lost. Some MVP's recommended:

    Page.MaintainScrollPositionOnPostBack = True

    This is the .Net 2.0 replacement for Page.SmartNavigation = True.
    However, this only seems to apply to maintaining the scrollbar position

    of the Page and not the Panel control. (BTW - If anyone knows how to
    apply MaintainScrollPositionOnPostBack to a Panel, please let me
    know!).

    The solution requires an ASP.Net HiddenField control to save the scroll

    position when the form submits and of course, a Panel control with
    scrollbars. In this case, I'm using horizontal scroll bars and my
    example code will only work for horizontal positioning (although
    vertical positioning is easy as I will show you). Here are the
    controls:

    In your .aspx file put the a asp:hiddenfield tag right after your
    asp:panel control as shown:


    <asp:panel ID="pnlGallery" runat="server" ScrollBars="Vertical"
    Width="200px" >
    <!-- Add your controls to the Panel in order to activate the scroll
    bars -->
    </asp:panel>
    <asp:HiddenField ID="hfScrollPosition" runat="server" />


    In your .cs file put the following under your Page_Load event handler
    function:

    String script;
    ClientScriptManager CSManager = Page.ClientScript;
    if
    (!CSManager.IsOnSubmitStatementRegistered(this.GetType(),"SaveScrollPosition"))
    {
    script = "var HiddenField = document.getElementById('" +
    hfScrollPosition.ClientID + "');\n\r";
    script += "var ScrollElement = document.getElementById('" +
    pGridView.ClientID + "');\n\r";
    script += "HiddenField.value = ScrollElement.scrollTop;\n\r";

    CSManager.RegisterOnSubmitStatement(this.GetType(),
    "SaveScrollPosition",script);
    }
    if
    (!CSManager.IsStartupScriptRegistered(this.GetType(),"RetrieveScrollPosition"))
    {
    script = "var HiddenField = document.getElementById('" +
    hfScrollPosition.ClientID + "');\n\r";
    script += "var ScrollElement = document.getElementById('" +
    pGridView.ClientID + "');\n\r";
    script += "if(HiddenField.value != '')\n\r";
    script += "{\n\r";
    script += "ScrollElement.scrollTop = HiddenField.value;\n\r";
    script += "}\n\r";

    CSManager.RegisterStartupScript(this.GetType(),
    "RetrieveScrollPosition",script, true);
    }
     
    , Jan 12, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cm11bnNvbjg=?=

    Panel scroll position reset on postback

    =?Utf-8?B?cm11bnNvbjg=?=, Sep 24, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    20,845
    Chris Botha
    Sep 24, 2005
  2. Sobin

    HELP ME...Panel control not maintaining scroll position

    Sobin, Dec 30, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    822
    Sobin
    Dec 30, 2007
  3. prasad srirambhatla
    Replies:
    2
    Views:
    331
    Teemu Keiski
    Aug 28, 2003
  4. Jason James

    Scroll position in panel control

    Jason James, Mar 26, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    254
    Steve C. Orr [MVP, MCSD]
    Mar 26, 2006
  5. Elroyskimms

    HOWTO: Persist Scroll Position in Panel Control on Postback

    Elroyskimms, Oct 2, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    877
    Elroyskimms
    Oct 2, 2006
Loading...

Share This Page