Datalist Pagination
im doing data pagination by using this code :
http://www.aspdotnetcodes.com/DataList_Dynamic_Paging_PagedDataSource.aspx
its work perfectly, but my problem is :
i dont want to show large page numbers like this 1 2 3 4 5 6 7 8 9 10 11 12 ...
i want just get a blo开发者_Python百科c of 4 pages 1 2 3 4 and after clicking next the next bloc apear
thx for helping
Why don't you just change
for (int i = 0; i < pds.PageCount; i++)
to
for (int i = 0; i < pds.PageCount && i < 4; i++)
This is a c# 2.0 way, you might consider using an Enumerable.Range instead of for loops
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;
}
精彩评论