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

J

joegoeke

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);
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top