Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled

I have created:

  • one master page and one content page called Detail.
  • On Button click event, displaying data in grid view.
  • In grid view, columns are autogenerated.
  • I wanted to show 11 column in grid view, but it is more than page size.

What to do for this?

I have created sql helper file for database connection code and calling that method, not using sqldatasource for connection.

When I trying to do paging, getting error:

The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.

like image 871
Jui Test Avatar asked Oct 30 '11 14:10

Jui Test


People also ask

How does ASP net handle PageIndexChanging in GridView?

In order to implement Paging in GridView, AllowPaging property is set to True and OnPageIndexChanging event has been handled. You will need to import the following namespaces. Inside the Page Load event of the page, the GridView is populated with records from the Customers table of the Northwind database.

What is PageIndexChanging?

The PageIndexChanging event is raised when one of the pager buttons is clicked, but before the GridView control handles the paging operation. This enables you to provide an event-handling method that performs a custom routine, such as canceling the paging operation, whenever this event occurs.


2 Answers

You need to declare a method on your code behind that handles the PageIndexChanging event.

Something similar to this:

protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs  e)
{
    GridView1.PageIndex = e.NewPageIndex;
    bindGridView(); //bindgridview will get the data source and bind it again
}

private void bindGridView()
{
     GridView1.DataSource=getData();
     GridView1.DataBind();
}

Providing sample code:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bindGridView(); //bindgridview will get the data source and bind it again
    }

    protected void Page_Load(object sender , EventArgs e)
    {
        if(!IsPostBack)
         bindGridView();

    }
    //this is some sample data 
    private void bindGridView()
    {
        DataTable t = new DataTable();
        t.Columns.Add("Col1");
        t.Columns.Add("Col2");
        DataRow r = null;
        for (int i = 0; i < 25; i++)
        {
            r = t.NewRow();
            r.ItemArray = new object[] { "Val" + i, " Another " + i };
            t.Rows.Add(r);
        }
        GridView1.DataSource = t;
        GridView1.DataBind();
    }

And this is the markup:

<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">

Produces this:

enter image description here

like image 80
Icarus Avatar answered Sep 21 '22 02:09

Icarus


you simply add this to your code :

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


}
like image 38
Imad Avatar answered Sep 19 '22 02:09

Imad