13

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>
Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62

6 Answers6

13

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

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
  • +1 for letting us know about AllowPaging attribute. If this is is set to "true" can also cause the issue without the PageIndexChanging event. – Raymond Dumalaog May 01 '21 at 03:31
11

Your code should be inside On PageIndexChanging Event

  protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
        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" 
            OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging">
           </asp:GridView>
Arash
  • 889
  • 7
  • 18
3

try

OnPageIndexChanging="SubmitAppraisalGrid_PageIndexChanging"

instead of

onselectedindexchanging="SubmitAppraisalGrid_SelectedIndexChanging"


protected void SubmitAppraisalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    SubmitAppraisalGrid.PageIndex = e.NewPageIndex;
    BindGrid();
}
Darshana
  • 2,462
  • 6
  • 28
  • 54
0

Step by Step:

  1. Select gridview from design and go to property and fire the event (PageIndexChanging)
  2. Code : gridviewname.pageindex=e.NewPageIndex;
Echilon
  • 10,064
  • 33
  • 131
  • 217
Arjun Walmiki
  • 173
  • 1
  • 1
  • 13
0

You need to call the Pageindex changing event from selected index changing event of dropdown.

protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
     // Retrieve the pager row.
    GridViewRow pagerRow = SubmitAppraisalGrid.BottomPagerRow;

    // Retrieve the PageDropDownList DropDownList from the bottom pager row.
    DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");

    // Set the PageIndex property to display that page selected by the user.
    GridViewPageEventArgs evt = new GridViewPageEventArgs(pageList.SelectedIndex);
    SubmitAppraisalGrid_PageIndexChanging(sender, evt);
}
user2247929
  • 103
  • 5
0

insted of using

SubmitAppraisalGrid.PageIndex = e.NewSelectedIndex;

you must use

SubmitAppraisalGrid.PageIndex = e.NewPageIndex;

and if you got error again plese post the error too..

Ajay Mirge
  • 102
  • 2
  • 2
  • 12