Tuesday, May 29, 2007

Flaw with buttons in the GridView pager

For unknown reason the GridView pager in Numeric and NumericFirstLast modes does not take into consideration values of the PreviousPageText and NextPageText properties from the pager settings. Instead an ellipsis is shown. Of course, you may say that the buttons with the ellipsis implement a bit different functionality. Yes, they do. But the problem leaves - text on these buttons is not customizable. Sometimes it becomes problematic especially if your customer is too pernickety. To solve this flaw you can use next approach:
GridView1.RowDataBound+=new GridViewRowEventHandler(GridView1_RowDataBound);
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
Table pagerTable = (Table)e.Row.Cells[0].Controls[0];
TableRow pagerRow = pagerTable.Rows[0];
PagerSettings pagerSettings = ((GridView)sender).PagerSettings;
int cellsCount = pagerRow.Cells.Count;
if (pagerSettings.Mode == PagerButtons.Numeric
|| pagerSettings.Mode == PagerButtons.NumericFirstLast)
{
int prevButtonIndex = pagerSettings.Mode==PagerButtons.Numeric ? 0 : 1;
int nextButtonIndex = pagerSettings.Mode==PagerButtons.Numeric ? cellsCount-1 : cellsCount-2;
if (prevButtonIndex < cellsCount)
{
//check whether previous button exists
LinkButton btnPrev = pagerRow.Cells[prevButtonIndex].Controls[0] as LinkButton;
if (btnPrev != null && btnPrev.Text.IndexOf("...") != -1)
btnPrev.Text = pagerSettings.PreviousPageText;
}
if (nextButtonIndex > 0 && nextButtonIndex < cellsCount)
{
//check whether next button exists
LinkButton btnNext = pagerRow.Cells[nextButtonIndex].Controls[0] as LinkButton;
if (btnNext != null && btnNext.Text.IndexOf("...") != -1)
btnNext.Text = pagerSettings.NextPageText;
}
}
}
}

Update: It turns that it is rather simply to completely simulate the Previous and Next buttons. Add following snippets to the code above.
if (btnPrev != null && btnPrev.Text.IndexOf("...") != -1)
{
...
btnPrev.CommandName = "Page";
btnPrev.CommandArgument = "Prev";
}
...
if (btnNext != null && btnNext.Text.IndexOf("...") != -1)
{
...
btnNext.CommandName = "Page";
btnNext.CommandArgument = "Next";
}

No comments:

Post a Comment