Solved

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

Posted on 2011-09-05
7
2,897 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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