Solved

ASP.NET C#- Set page scroll position programmatically.

Posted on 2011-09-05
7
2,964 Views
Last Modified: 2012-05-12
I have a search page that displays the results in a gridview, based on selections a user makes in drop-downs at the top of the page.  I would like to scroll the page so that the top of the results gridview is at the top of the page, once the search has run.  I found this code, which scrolls the page back to the top (not the top of the grid):

<script type="text/javascript">
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(scrollToTop);

            function scrollToTop() {
                window.scrollTo(0, 0);
            }        
        </script>

It works, but it scrolls the page to the top for ALL postbacks, not just when the search is run or the grid page index is changed.  How can I get this to only fire when a search is done or the page index is changed?  I'm sure all I have to do is determine the position I want the page to scroll to, and change the ScrollTo coodinates, I just don't know how to implement that in codebehind.
0
Comment
Question by:cmerlo1
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:disrupt
ID: 36485388
0
 

Author Comment

by:cmerlo1
ID: 36485624
Using that, I can get it to work the second time I click the button, but not the first.  Also, I can't get it to fire when the gridview page index is changed...
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485863
are you placing it in GridView1_PageIndexChanged method?

an example:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindexchanged.aspx
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

by:disrupt
ID: 36485868
set the OnPageIndexChanged attribute in your aspx page and point it to the method you are trying to trigger
0
 

Author Comment

by:cmerlo1
ID: 36492491
Yeah- I did that.  Everything is inside an update panel (apologies for not noting that before) and I was able to get the button working 100% of the time by changing the function to this:

function scrollToTop()
            {
                setTimeout("window.scrollTo(0, 710)", 500);
            }

I still can't get the scroll function to run when the grid page index changes- I've tried calling the scroll during other grid events as well (databinding, databound) with the same results.  I'm sure its some kind of timing issue, where the javascript is firing before the grid changes, and the AJAX in the grid is moving the position back to the original one.  I just don't know how to override the AJAX, I think.
0
 

Accepted Solution

by:
cmerlo1 earned 0 total points
ID: 36516724
I figured it out- the script has to be registered to the script manager:

ScriptManager.RegisterStartupScript(Page, this.GetType(), "ScrollPage", "scrollToTop();", true);

Open in new window

0
 

Author Closing Comment

by:cmerlo1
ID: 36541403
Did more research and found how to make it work in Ajax.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question