Casual ideas for web development

Flaw with buttons in GridView pager

29 May 2007
For unknown reason GridView pager in Numeric and NumericFirstLast modes does not take into consideration values of the PreviousPageText and NextPageText properties from the pager settings. Instead ellipsis is shown. Of course, you may say that the buttons with ellipsis implement a little 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;
            }
        }
    }
}

25 Jul 2007
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";
}

About this blog

This is a personal blog of Mykola Tarasyuk, a web developer from Ukraine. It includes articles about ASP.Net, C#, Javascript and anything else that tickles the fancy of the author ...

Top 6 of 2009


All Articles

Locations of visitors to this page
Copyright © Mykola Tarasyuk, 2007-2010