注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

hurt0759的个人主页

人生常态--跋涉.人生暂态--歇息.

 
 
 

日志

 
 

c# 分页  

2011-12-14 12:03:20|  分类: IT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1.       首先提供一个用于分页的类,DatalistPage.cs,代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

/// <summary>

/// DatalistPage 的摘要说明

/// </summary>

public class DatalistPage

{

    /// <summary>

    /// <param name="dt">DataTable实例</param>

    /// <param name="webcontrol">数据控件名称</param>

    /// <param name="pagesize">分页大小</param>

    /// </summary>

    public static string GetPageNum(DataTable dt, Object webcontrol, int pagesize)

    {

        PagedDataSource objPds = new PagedDataSource();

        objPds.DataSource = dt.DefaultView;

        objPds.AllowPaging = true;

        int total = dt.Rows.Count;

        objPds.PageSize = pagesize;

        int page = 1;

        string Url = HttpContext.Current.Request.Url.PathAndQuery;//路径

        string Query = Url.Substring(Url.IndexOf("aspx") + 4);//aspx后的字符串

        if (Query == "")

        {

            Query += "?";

        }

        else

        {

            if (HttpContext.Current.Request.QueryString["page"] != null)

            {

                page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);

                Query = Query.Substring(0, Query.LastIndexOf("page"));

            }

            else

            {

                Query += "&";

                page = 1;

            }

        }

        objPds.CurrentPageIndex = page - 1;

        string type = webcontrol.GetType().Name;

        switch (type)

        {

            case "Repeater":

                ((Repeater)webcontrol).DataSource = objPds;

                ((Repeater)webcontrol).DataBind();

                break;

            case "DataList":

                ((DataList)webcontrol).DataSource = objPds;

                ((DataList)webcontrol).DataBind();

                break;

            case "GridView":

                ((GridView)webcontrol).DataSource = objPds;

                ((GridView)webcontrol).DataBind();

                break;

        }

        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 + "页      ";

        pagestr += page > 1 ? "<a href="" + HttpContext.Current.Request.CurrentExecutionFilePath + Query + "page=1">首页</a>  <a href="" + HttpContext.Current.Request.CurrentExecutionFilePath + Query + "page=" + pre + "">上一页</a>" : "首页上一页";

        //中间页处理,这个增加时间复杂度,减小空间复杂度

        for (int i = startcount; i <= endcount; i++)

        {

            pagestr += page == i ? "  <font color="#ff0000">" + i + "</font>" : "  <a href="" + HttpContext.Current.Request.CurrentExecutionFilePath + Query + "page=" + i + "">" + i + "</a>";

        }

        pagestr += page != allpage ? "  <a href="" + HttpContext.Current.Request.CurrentExecutionFilePath + Query + "page=" + next + "">下一页</a>  <a href="" + HttpContext.Current.Request.CurrentExecutionFilePath + Query + "page=" + allpage + "">末页</a>" : " 下一页末页";

        return pagestr;

    }

 

}

 

2.      aspx文件中提供一个数据控件,如datalist控件,然后添加一个用于显示分页的div,

<div runat="server" id="pageInfo"></div>

3.      aspx.cs文件中绑定数据库,使用分页类。

在绑定完数据库之后,添加代码:

pageInfo.InnerHtml = DatalistPage.GetPageNum(dt, Repeater1, 10);

三个参数中 dt指绑定的数据,Repeater1是数据控件对象,10表示分页条数。

4.      列举实例:

Default.aspx文件:

 

<%@ Page Language="C#" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>分页技术</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            &nbsp;<asp:Repeater ID="Repeater1" runat="server">

                <HeaderTemplate>

                    <ul>

                </HeaderTemplate>

                <ItemTemplate>

                    <li>

                        <%#("title") %>

                    </li>

                </ItemTemplate>

                <FooterTemplate>

                    </ul></FooterTemplate>

            </asp:Repeater>

        </div>

        <div runat="server" id="pageInfo"></div>

    </form>

</body>

</html>

 

Defuakt.aspx.cs文件:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using DataAccess;

 

public partial class Default2 : System.Web.UI.Page

{

    DatabaseHelper mydbhelp = new DatabaseHelper(ConfigurationManager.ConnectionStrings["connstr"].ToString());

    protected void Page_Load(object sender, EventArgs e)

    {

        string sql = "select * from 表 ";

        DataTable dt = mydbhelp.ExecuteDataTable(sql);

        pageInfo.InnerHtml = DatalistPage.GetPageNum(dt, Repeater1, 10);

    }

 

}

  评论这张
 
阅读(1742)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017