Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-05
7
Medium Priority
?
3,256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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