Invoke javascript on page change event in a Gridview

Posted on 2009-04-23
Medium Priority
Last Modified: 2012-05-06
Is it possible to fire off a javascript event on a Gridview when the user changes the page index that they want to look at?

I know with buttons and some other controls, you can add attributes for things like "onclick" or "onchange".   Is it possible to do something similiar for a gridview on a page change?

Basically what I'm trying to do is execute javascript to take the user to the top of the page (window.scrollTo(0,0);)   I am using MaintainScrollPositionOnPostback="true" for this page which is why I need to somehow override that.   Currently, if they change the page number of a grid that they are on, it doesn't really make sense to still be looking at the bottom of the results after the page loads.   I'd like to force the page back to the top when the page index changes.
Question by:amb1313
  • 2
  • 2
LVL 12

Expert Comment

ID: 24217381
I've done it like this. Capture the row data bound event, and loop through the page cells that contain the link buttons
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.Pager)
                // make sure the PagerTableExists
                TableRow tr = (TableRow)e.Row.Controls[0].Controls[0].Controls[0];
                foreach (TableCell td in tr.Cells)
                    foreach (Control c in td.Controls)
                        if (c.GetType().ToString().Equals("System.Web.UI.WebControls.DataControlPagerLinkButton"))
                            LinkButton lb = (LinkButton)c;
                            lb.OnClientClick = "return confirm('Do you really wish to change the page?');";

Open in new window

LVL 12

Accepted Solution

wht1986 earned 400 total points
ID: 24217409
sorry in your example change the onclientclick of my example to

lb.OnClientClick = "window.scrollTo(0,0);";

Author Closing Comment

ID: 31573821
Thanks.  That was exactly what I needed.

Author Comment

ID: 24218098
Thanks. That was exactly what I needed.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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