Solved

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

Posted on 2011-09-05
7
2,836 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now