ASP.NET:Paging when deleting rows

Posted by Niels Bosma on Stack Overflow See other posts from Stack Overflow or by Niels Bosma
Published on 2009-09-29T12:14:16Z Indexed on 2010/04/03 5:33 UTC
Read the original article Hit count: 294

Filed under:
|
|

I have a datagrid where a row can be deleted (using ajax). I'm have problem with the pager in the following scenario:

Lets say my PageSize is 10, I have 101 rows so that's 11 pages with the last page with an single element. Let no assume that I'm on page 10 (PageIndex=9) and delete a row. Then I go to the 11'th page (who's now empty and doesn't really exist). ASP now shows me the EmptyDataTemplate and no pager so I can't go back.

My approach (which isn't working) is to detect this scenario and step one page back:

public void Bind()
{
    gridMain.DataBind();
}

public void SetPage(int page)
{
    gridMain.PageIndex = page;
    gridMain.DataBind();
}

protected void ldsGridMain_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    selectArgs = e;
    e.Result = (new EnquiryListController()).GetEnquiryList(OnBind(this), supplier);
}

protected void ldsGridMain_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    totalRows = selectArgs.Arguments.TotalRowCount;

    //Detect if we need to update the page: 
    if (gridMain.PageIndex > 0 && (gridMain.PageSize * gridMain.PageIndex + 1) > totalRows) SetPage(gridMain.PageIndex - 1);
}

protected void gridMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    SetPage(e.NewPageIndex);    
}

I can see that SetPage is called with the the right page index, but the databind doesn't seem to called as I still get the EmptyDataTemplate.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET