生成repeater分页的页码,可以想datagrid一样分页。需要配合PagedDataSource 。

生成repeater分页的页码,可以想datagrid一样点击。需要配合PagedDataSource 。

public string CreatePagerLinks(PagedDataSource objPds, string BaseUrl)
  {
   int totalPageRanger = 3;//set page range is 5, means display five hyperlink for page selection
   int pageRanger = (totalPageRanger-1)/2;

   StringBuilder sbPager = new StringBuilder();

   if (!objPds.IsFirstPage)
   {
    // first page link
    sbPager.Append("<a href=\"");
    sbPager.Append(BaseUrl);
    sbPager.Append("\">|<</a> ");
    if (objPds.CurrentPageIndex != 1)
    {
     // previous page link
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(objPds.CurrentPageIndex.ToString());
     sbPager.Append("\" alt=\"Previous Page\"><<</a>  ");
    }
   }


   int intLow = objPds.CurrentPageIndex + 1 - pageRanger;//pageindex + 1 = pagenumber
   int intHigh = objPds.CurrentPageIndex + 1 + pageRanger;//pageindex + 1 = pagenumber

   if (intLow < 1) intLow = 1;
   if (intHigh > objPds.PageCount) intHigh = objPds.PageCount;

   if (intHigh - intLow < totalPageRanger)
   {
    intHigh = intLow + totalPageRanger - 1;
    if (intHigh > objPds.PageCount)
     intHigh = objPds.PageCount;
   }
   if (intHigh - intLow < totalPageRanger)
   {
    intLow = intHigh - totalPageRanger + 1;
    if (intLow < 1)
     intLow = 1;
   }

   for (int x = intLow; x <= intHigh ; x++)
   {
    // numeric links
    if (x == objPds.CurrentPageIndex + 1) sbPager.Append(x.ToString() + "  ");
    else
    {
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(x.ToString());
     sbPager.Append("\">");
     sbPager.Append(x.ToString());
     sbPager.Append("</a>  " );
    }
   }
   if (!objPds.IsLastPage)
   {
    if ((objPds.CurrentPageIndex + 2) != objPds.PageCount)
    {
     // next page link
     sbPager.Append("<a href=\"");
     sbPager.Append(BaseUrl);
     sbPager.Append("&page=");
     sbPager.Append(Convert.ToString(objPds.CurrentPageIndex + 2));
     sbPager.Append("\">>></a>  ");
    }
    // last page link
    sbPager.Append("<a href=\"");
    sbPager.Append(BaseUrl);
    sbPager.Append("&page=");
    sbPager.Append(objPds.PageCount.ToString());
    sbPager.Append("\">>|</a>");
   }
   // conver the final links to a string and assign to labels
   return sbPager.ToString();
  }


lilPage是一个literal,直接调用上面那个方法赋值就可以。

private void BindRepeater()
  {
   DataSet ds = new DataSet();

    ds = ......

   DataTable dt = ds.Tables[0];
   
   foreach (DataRow dr in dt.Rows)
   {
    if (dr["RewardImageUrl"]==null||dr["RewardImageUrl"].ToString().Length ==0)
     dr["RewardImageUrl"] = ConfigVar.Get("NO_PIC").ToString();
   }

   PagedDataSource pagedDataSource = new PagedDataSource();
   pagedDataSource.DataSource = ds.Tables[0].DefaultView;
   pagedDataSource.AllowPaging = true;
   pagedDataSource.PageSize = int.Parse(giftPageSize);

   //start paging
   if (GetPage() > -1)
   {
    // subtract 1 because the PagedDataSource uses a zero-based index
    int pageIndex = GetPage() - 1 ;
    // correct for a page index less than 0 or greater than the last page
    if (pageIndex < 0) pageIndex = 0;
    if (pageIndex > pagedDataSource.PageCount)
     pageIndex = pagedDataSource.PageCount - 1;
    pagedDataSource.CurrentPageIndex = pageIndex;
   }
   this.RepGiftList.DataSource = pagedDataSource;
   this.RepGiftList.DataBind();

   lilPage.Text = CreatePagerLinks(pagedDataSource, GetAbsolutUri());
  }

posted @ 2008-06-17 17:04  helloworld22  阅读(422)  评论(0编辑  收藏  举报