Question

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!

Was it helpful?

Solution

It's even simpler:

GridView gridView = (GridView)sender;

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

OTHER TIPS

protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gv= sender as GridView;

    if(gv!=null){
        gridView.PageIndex = e.NewPageIndex;
        gridView.DataBind();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top