ASP.NET分页探索之一:使用PagedDataSource控件

为了将大量的数据分为多页显示,提高用户浏览体验,我们经常需要对数据进行分页。DataGrid就具有分页功能,而要让DataList和DataRepeater具备类似功能,我们能够使用的最直接、快速的方法是利用PageDataSource控件。在数据进行分页的时候还需要对数据页索引做一个导航,类似于“首页 上一页 下一页 末页”或者“1 2 3 4 5 6 7 8 9 10 ……”,在这个例子中创建了一个类,它封装了一个PagedDataSource控件和输出的数据索引导航条。下面是其实现:

创建PagedDataSourceExtension类:PagedDataSourceExtension.cs
ContractedBlock.gifExpandedBlockStart.gifCode
  1using System;
  2using System.Data;
  3using System.Data.OleDb;
  4using System.Data.SqlClient;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Text;
 10
 11ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 12///PagedDataSourceExtension 的摘要说明
 13/// </summary>

 14public class PagedDataSourceExtension
 15ExpandedBlockStart.gifContractedBlock.gif{
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 17    /// 分页参数的查询字符串
 18    /// </summary>

 19    private string queryStringName = "pageIndex";
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 21    /// 设置或获取分页参数的查询字符串
 22    /// </summary>

 23    public string QueryStringName
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        get return this.queryStringName; }
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        set this.queryStringName = value; }
 27
 28    }

 29ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 30    /// 数据存储组件
 31    /// </summary>

 32    private PagedDataSource ps;
 33ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 34    /// 分页索引文本
 35    /// </summary>

 36    private String pagerText;
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 38    /// 以每页记录大小为参数构造一个实例
 39    /// </summary>
 40    /// <param name="pSize"></param>

 41    public PagedDataSourceExtension(int pSize)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 43        ps = new PagedDataSource();
 44        ps.AllowPaging = true;
 45        ps.PageSize = pSize;
 46    }

 47ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 48    /// 获取从查询字符串中得来的页数索引,默认为1
 49    /// </summary>

 50    int curIndex
 51ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 52        get
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54            HttpContext ctx = HttpContext.Current;
 55            int index = 1;
 56            if (ctx.Request.Params[this.queryStringName] != null)
 57                int.TryParse(ctx.Request.Params[this.queryStringName], out index);
 58            if (index == 0)
 59                index = 1;
 60            return index;
 61        }

 62    }

 63ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 64    /// 为数据消费者提供数据
 65    /// </summary>

 66    public PagedDataSource Data
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        get return ps; }
 69    }

 70ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 71    /// 获取分页索引文本作为导航
 72    /// </summary>

 73    public String PagerText
 74ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        get return pagerText; }
 76    }

 77ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 78    /// 给PagedDataSource填充数据
 79    /// </summary>
 80    /// <param name="ds">DataSet参数</param>

 81    public void FillData(DataSet ds)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 83        ps.DataSource = ds.Tables[0].DefaultView;
 84        if (this.curIndex >= ps.PageCount) ps.CurrentPageIndex = ps.PageCount - 1;
 85        else if (curIndex <= 0) ps.CurrentPageIndex = 0;
 86        else ps.CurrentPageIndex = curIndex;
 87        CreatePagerText();
 88    }

 89ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 90    ///  给PagedDataSource填充数据
 91    /// </summary>
 92    /// <param name="sql">Select语句</param>
 93    /// <param name="connstr">连接字符串</param>

 94    public void FillData(string sql, string connstr)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 96        IDataAdapter ida;
 97        if (connstr.ToLower().Contains("jet"|| connstr.ToLower().Contains("provider"))
 98
 99            ida = new OleDbDataAdapter(sql, connstr);
100        else
101            ida = new SqlDataAdapter(sql, connstr);
102        DataSet ds = new DataSet();
103        ida.Fill(ds);
104        ps.DataSource = ds.Tables[0].DefaultView;
105        if (curIndex >= ps.PageCount) ps.CurrentPageIndex = ps.PageCount - 1;
106        else if (curIndex <= 0) ps.CurrentPageIndex = 0;
107        else ps.CurrentPageIndex = curIndex - 1;
108        CreatePagerText();
109
110    }

111ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
112    /// 生成导航文本
113    /// </summary>

114    void CreatePagerText()
115ExpandedSubBlockStart.gifContractedSubBlock.gif    {
116        StringBuilder sb = new StringBuilder();
117        int startPage = this.curIndex / 10;
118        startPage = (this.curIndex % 10 == 0? startPage - 1 : startPage;
119        if (startPage > 0)
120ExpandedSubBlockStart.gifContractedSubBlock.gif        {
121            for (int j = 0; j < startPage; j++)
122ExpandedSubBlockStart.gifContractedSubBlock.gif            {
123                sb.Append(string.Format("<a href='?{1}={0}' title='第{0}页'>{0}</a>&nbsp;&nbsp;", j * 10 + 1this.queryStringName));
124            }

125        }

126
127        if (this.curIndex > 10 * startPage && this.curIndex > 10)
128            sb.Append(string.Format("<a title='第{0}页' href='?{1}={0}'></a>&nbsp;&nbsp;", startPage * 10this.queryStringName));
129
130        for (int i = startPage * 10 + 1; i <= ps.PageCount; i++)
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {
132
133            if (i <= startPage * 10 + 10 || i == ps.PageCount)
134ExpandedSubBlockStart.gifContractedSubBlock.gif            {
135                if (i == this.curIndex)
136                    sb.Append(string.Format("<a href='?{1}={0}' title='第{0}页'><b><u>{0}</u></b></a>&nbsp;&nbsp;", i, this.queryStringName));
137                else
138                    sb.Append(string.Format("<a href='?{1}={0}' title='第{0}页'>{0}</a>&nbsp;&nbsp;", i, this.queryStringName));
139            }

140            else if (i == startPage * 10 + 11)
141                sb.Append(string.Format("<a href='?{1}={0}' title='第{0}页'></a>&nbsp;", i, this.queryStringName));
142        }

143
144        pagerText = sb.ToString();
145
146    }

147}

 

PageDataSourceExtension从一个包含每页记录数的Int创建实例,并从一个已有的DataSet对象或者一个SELECT语句和ConnectionString获取数据。其Data属性返回PagedDataSource对象供数据消费者使用,而PagerText属性则返回为数据消费者创建的导航文本。

后台使用方法为:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
    protected void Page_Load(object sender, EventArgs e)
    {
        PagedDataSourceExtension pdse 
= new PagedDataSourceExtension(2);
        pdse.FillData(
"select * from products","Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Request.MapPath("app_data\\ebiz.mdb"));
        
this.PagerNav.Text = pdse.PagerText;
        
this.DataList1.DataSource = pdse.Data;
        
this.DataList1.DataBind();        
       
    }

 

使用效果:


 

双导航条显示:

 

使用PagedDataSource控件进行分页并不是理想的方案,特别是在数据量大的时候非常不经济。因为该控件是每次访问页面是都使用全部的数据,而高效的分页应该是用多少数据取多少数据。

 

转载于:https://www.cnblogs.com/jf_dai/archive/2009/11/22/1605651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值