The GridView fired event PageIndexChanging which wasn't handled

user1037134 picture user1037134 · Mar 16, 2012 · Viewed 42.4k times · Source

i have allowed paging and added the below codes but got the error. Does anyone know what could be the problem?

Code:

  protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;
        SubmitAppraisalGrid.DataBind();

    }

Design:

<asp:GridView ID="SubmitAppraisalGrid" runat="server" 
                AutoGenerateColumns="False" BorderWidth="0px" 
                onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False" 
                style="margin-right: 0px" AllowPaging="True" PageSize="1" 
                onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging">
               </asp:GridView>

Answer

Niranjan Singh picture Niranjan Singh · Mar 16, 2012

If you have set a gridviews AllowPaging attribute to “true” and do not handle the PageIndexChanging event then this error raise.

To work with paging add the PageIndexChanging event handler to grid and change your markup and code as:

<asp:GridView ID="SubmitAppraisalGrid" runat="server" 
                AutoGenerateColumns="False" BorderWidth="0px" 
                onrowcreated="SubmitAppraisalGrid_RowCreated" ShowHeader="False" 
                style="margin-right: 0px" AllowPaging="True" PageSize="1" 
                onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"
                OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
               </asp:GridView>

///

protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
    SubmitAppraisalGrid.DataBind();

    //bindGrid(); 
    //SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
    //SubmitAppraisalGrid.DataBind();
}

protected void SubmitAppraisalGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
   /// you selected index related logic here.
}

This event is not raised when you programmatically set the PageIndex property. Check MSDN documentation of GridView.PageIndexChanging Event

For reference: The GridView fired event PageIndexChanging which wasn't handled