3

I have multiple GridView on a page, and they are all pagable. I need to handle the paging in OnPageIndexChanging event, but I'd rather not write the same code for each GridView.

So how can I get the GridView object id from the sender? I'm trying to do something like the following....

protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gridView = (GridView)sender.ID;

    gridView.PageIndex = e.NewPageIndex;
    gridView.DataBind();
}

This way I could call the same event handler for all the GridViews and not have to write a new even handler for each one? I'm just not sure how to get the ID of the GridView firing the event :(

any help appreciated!

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
Stuart
  • 1,544
  • 5
  • 29
  • 45

2 Answers2

7

It's even simpler:

GridView gridView = (GridView)sender;

The sender argument is always the control that triggered the event.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gv= sender as GridView;

    if(gv!=null){
        gridView.PageIndex = e.NewPageIndex;
        gridView.DataBind();
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153