Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

GridView paging takes two clicks instead of one

5.00/5 (3 votes)
17 Apr 2012CPOL 25.8K  
GridView paging takes two clicks (solved).

Introduction

If you are experiencing that GridView paging takes two clicks to work instead of 1 then please read on.

Background

This problem may effect only selected people and also happens due to other reasons as well which I am not going to cover in this article.

Using the code

In one of the projects I did; I noticed that paging of one of the GridView controls was taking two clicks instead of one to work.

I debugged the application and it was posting back and there was not a single difference between the two clicks but still the paging was working on the second click.

Then I noticed the following piece of code:

C#
protected void gvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    BindGrid();
    gvUsers.PageIndex = e.NewPageIndex;
}

So effectively I was binding the grid first and then issuing a new index.

I changed the order of issuing new index and binding statement.

C#
protected void gvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvUsers.PageIndex = e.NewPageIndex;
    BindGrid(); 
}

and voila!

The paging started working just by a single click instead of two.

So the Tip is to issue a new index before re-binding the grid.

Hope this helps :-)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)