The GridView 'GridView1' fired event PageIndexChanging which wasn't handled Error solution in asp.net

Introduction:  In this article i am going to explain how to solve/handle the exception/error "The GridView 'GridView1' fired event PageIndexChanging which wasn't handled".  In previous similar articles i explained How to Solve Error Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server and The files use a different language,which is not allowed since they need to be compiled together and The SelectCommand property has not been initialized before calling 'Fill' Gridview and Solution to FileUpload control is not working in UpdatePanel and  How to solve WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Paging error in asp.net gridview
Click on image to enlarge
Description: As we know GridView is a data control to display data on web page. Sometimes the data may be large to display on a single page. So it is recommended to split the number of records for each page E.g. if we have 1000 records to display in GridView then we can show 10 records on each page with the help of paging. Paging is inbuilt in GridView and can be implemented easily by setting some simple properties and handling an event.

But if it is not properly configured then it will show/generate an asp.net exception/ error "The GridView 'GridView1' fired event PageIndexChanging which wasn't handled"  like shown in figure above.


Solution to the error:

To handle paging in GridView following properties and event must be set and handled.
  • AllowPaging property (Must be set to True)
  • PageSize property (Must be set to some number e.g. 5 if you want to show 5 records per page)
  • Onpageindexchanging event (Must be handled through code)  
So it should be like mentioned below:
<asp:GridView ID="grdEmp" runat="server" AutoGenerateColumns="False" AllowPaging="True" onpageindexchanging="grdEmp_PageIndexChanging" PageSize="5">
</asp:GridView>

C#.Net Code for handling paging in GridView Data control
  •  In the Code behind file (.aspx.cs) write the following code on PageIndexChanding event of GridView as:
protected void grdEmp_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEmp.PageIndex = e.NewPageIndex;
        //Call bind gridview function
        bindGridView();
    }

VB.Net Code for handling paging in GridView Data control

  • In the design page(.aspx) place a gridview control  :

<asp:GridView ID="grdEmp" runat="server" AutoGenerateColumns="False" AllowPaging="True"  PageSize="5">
</asp:GridView>
  • In the Code behind file (.aspx.vb) write the following code on PageIndexChanding event of GridView as

Protected Sub grdEmp_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmp.PageIndexChanging
grdEmp.PageIndex = e.NewPageIndex
'Call bind gridview function
  bindGridView() 
End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..