Get GridView Object from Sender

Stuart picture Stuart · Apr 25, 2013 · Viewed 7.8k times · Source

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!

Answer

Tim Schmelter picture Tim Schmelter · Apr 25, 2013

It's even simpler:

GridView gridView = (GridView)sender;

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