Datalist Pagination

时间:2010-11-16 17:35:21

标签: c# pagination datalist

使用此代码进行数据分页:

http://www.aspdotnetcodes.com/DataList_Dynamic_Paging_PagedDataSource.aspx

它的工作完美,但我的问题是:

我不想显示像这样的大页码1 2 3 4 5 6 7 8 9 10 11 12 ...

我希望得到一个4页1 2 3 4的集团,然后点击下一个集团

thx for help

2 个答案:

答案 0 :(得分:0)

你为什么不改变

 for (int i = 0; i < pds.PageCount; i++)

 for (int i = 0; i < pds.PageCount && i < 4; i++)

答案 1 :(得分:0)

这是一种c#2.0方式,您可以考虑使用Enumerable.Range而不是for循环

PagedDataSource = new PagedDataSource();
PagedDataSource.DataSource = _yourData;
PagedDataSource.AllowPaging = true;
PagedDataSource.PageSize = 20;
PagedDataSource.CurrentPageIndex = PageNumber; // get from session, viewstate, ...

if (PagedDataSource.PageCount > 1)
{
    rptDataPager.Visible = true;
    rptDataPager.DataSource = GetPageRange(10); // try with 4 as you requested
    rptDataPager.DataBind();
}
else
    rptDataPager.Visible = false;

yourRepeater.DataSource = PagedDataSource;
yourRepeater.DataBind();

// and then the method
private ArrayList GetPageRange(int pagesToDisplay)
{
    ArrayList pages = new ArrayList();
    if (PagedDataSource.PageCount <= pagesToDisplay)
    {
        for (int i = 0; i < PagedDataSource.PageCount; i++)
            pages.Add((i + 1).ToString());
    }
    else
    {
        if (PagedDataSource.CurrentPageIndex - (pagesToDisplay / 2) <= 0)
        {
            for (int i = 0; i < pagesToDisplay; i++)
                pages.Add((i + 1).ToString());
        }
        else if (PagedDataSource.CurrentPageIndex + (pagesToDisplay / 2) >= PagedDataSource.PageCount)
        {
            for (int i = PagedDataSource.PageCount - pagesToDisplay; i < PagedDataSource.PageCount; i++)
                pages.Add((i + 1).ToString());
        }
        else
        {
            for (int i = PagedDataSource.CurrentPageIndex - (pagesToDisplay / 2); i < PagedDataSource.CurrentPageIndex + (pagesToDisplay / 2); i++)
                pages.Add((i + 1).ToString());
        }
    }
    return pages;
}