Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > HOWTO: Persist Scroll Position in Panel Control on Postback part II

Reply
Thread Tools

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

 
 
joegoeke@gmail.com
Guest
Posts: n/a
 
      01-12-2007
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
aspanel control as shown:


<aspanel ID="pnlGallery" runat="server" ScrollBars="Vertical"
Width="200px" >
<!-- Add your controls to the Panel in order to activate the scroll
bars -->
</aspanel>
<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.Get Type(),"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);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP ME...Panel control not maintaining scroll position Sobin ASP .Net Building Controls 0 12-30-2007 05:48 PM
HOWTO: Persist Scroll Position in Panel Control on Postback Elroyskimms ASP .Net Web Controls 0 10-02-2006 12:30 AM
Scroll position in panel control Jason James ASP .Net Web Controls 1 03-26-2006 09:00 PM
Panel scroll position reset on postback =?Utf-8?B?cm11bnNvbjg=?= ASP .Net 3 09-24-2005 09:06 PM
maintaining scroll position of a scrollable panel control during postbacks prasad srirambhatla ASP .Net Web Controls 2 08-28-2003 04:47 PM



Advertisments