Solved

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

Posted on 2011-09-05
7
2,776 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

16 Experts available now in Live!

Get 1:1 Help Now