While I was trying to play with Pagination Support in Repeater, PagedDataSource rescued me. I made the following method and would like to share with all of you whether there is any pitfall or is there any chance for more improvement.
Here it is,
/// <summary>
/// Create pagination for Repeater
/// </summary>
/// <param name="context">HttpContext</param>
/// <param name="obj">System.Collections.IEnumerable</param>
/// <param name="rptr">Repeater Control</param>
/// <param name="pgSize">How many records in each page</param>
/// <returns>Pagination String</returns>
public static String pagination(HttpContext context,Object obj,Repeater rptr ,int pgSize )
{
String rtn = String.Empty;
int curpage = 0;
PagedDataSource pds = new PagedDataSource();
pds.DataSource=(System.Collections.IEnumerable)obj;
pds.AllowPaging = true;
pds.PageSize = pgSize;
if (context.Request.QueryString["page"] != null)
{
curpage = Convert.ToInt32(context.Request.QueryString["page"]);
}
else
{
curpage = 1;
}
pds.CurrentPageIndex = curpage - 1;
if (!pds.IsFirstPage)
{
rtn = "<a href='?page=" + (curpage - 1).ToString() + "'>Prev</a> ";
}
if (curpage == 1 && pds.DataSourceCount > pds.PageSize)
rtn = "1";
else if (pds.DataSourceCount == 0)
rtn = "No data to display";
else if (curpage > 1 && pds.DataSourceCount > pds.PageSize)
rtn = rtn + "<a href='?page=1'>1</a> ";
for (int i = 2; i <= pds.PageCount; i++)
{
if (i == curpage)
rtn = rtn + " " + i.ToString();
else
rtn = rtn + " <a href='?page=" + i.ToString() + "'>" + i.ToString() + "</a>";
}
if (!pds.IsLastPage)
{
rtn += " <a href='?page=" + (curpage + 1).ToString() + ">Next</a>";
}
rptr.DataSource = pds;
rptr.DataBind();
return rtn;
}