.net 简单分页代码

public class PageNums
{
    /// </summary>
    /// <param name="ds">DataSet实例</param>
    /// <param name="datalistname">DataList名称</param>
    /// <param name="pagesize">分页大小</param>
    public static string GetPageNum(DataTable ds, DataList datalistname, int pagesize)
    {
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds.DefaultView;
        objPds.AllowPaging = true;
        int total = ds.Rows.Count;
        objPds.PageSize = pagesize;
        int page;
        if (HttpContext.Current.Request.QueryString["page"] != null)
            page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
        else
            page = 1;
        objPds.CurrentPageIndex = page - 1;
        datalistname.DataSource = objPds;
        datalistname.DataBind();
        int allpage = 0;
        int next = 0;
        int pre = 0;
        int startcount = 0;
        int endcount = 0;
        string pagestr = "";
        if (page < 1) { page = 1; }
        //计算总页数
        if (pagesize != 0)
        {
            allpage = (total / pagesize);
            allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
            allpage = (allpage == 0 ? 1 : allpage);
        }
        next = page + 1;
        pre = page - 1;
        startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
        //中间页终止序号
        endcount = page < 5 ? 10 : page + 5;
        if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
        if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
        pagestr = "共" + allpage + "页&nbsp;&nbsp;&nbsp;&nbsp";
        pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首页</a>&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
       //中间页处理,这个增加时间复杂度,减小空间复杂度
        for (int i = startcount; i <= endcount; i++)
        {
            pagestr += page == i ? "&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>" : "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
        }
        pagestr += page != allpage ? "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a>&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
        return pagestr;
    }

前台代码:

<asp:DataList ID="Repeater1" runat="server">
    <ItemTemplate>

<table border="0" cellpadding="0" cellspacing="0" style="width:620px; line-height:15px" >
  <tr >
 
      <td align="left"><a href="NewsDetails.aspx?id=<%#Eval("id") %>">→ <%#Eval("title") %></a>   <%#Eval("imgimg") %></td>
  
    <td align="right" ><%#Eval("infotime") %></td>
  </tr>
</table>
<%-- <hr style="border-style:dashed; width:619px; color:#B9B9B9; height:0.5px"/>--%>
       <asp:Image ID="Image1" runat="server"  ImageUrl="~/img/xian.JPG" />
    </ItemTemplate>
    </asp:DataList>


<div id="PageInfo" runat="server" style="  margin-top:15px; margin-left:20px; font-size:12px; "></div>

cs代码
public void Databind()
    {
       
          string   sqlstr = "select   * from news where bigclassname=2 and flag=0 order by infotime desc";
      
       
        DataTable dt = zhongzhu.myDataAdapter(sqlstr);
        dt.Columns.Add("imgimg", typeof(string));
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
               
                if (i < 5)
                {
                    dt.Rows[i]["imgimg"] = "<img src=\"img/News.jpg\" style=\" margin-top:5px;margin_bottom:0px;width:28px;height:11px\"/>";
                }
            }
        }
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        this.PageInfo.InnerHtml = PageNums.GetPageNum(dt, Repeater1, 10);
    }

posted @ 2011-03-14 11:52  放哨De老鼠  阅读(565)  评论(0编辑  收藏  举报