利用 PagedDataSource 实现 Repeater 分页

本文介绍了一种使用Repeater控件结合PagedDataSource组件实现分页功能的方法,并提供了完整的示例代码,包括页面配置、事件处理及数据操作等关键部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一直很忙,看来7月份又只写了两篇日志。没时间更新 blog ,其实也是没什么可写的,前几天群里有个朋友问我Repeater控件分页的问题,我大概给他讲了一下,说DataGrid可以实现分页,是因为其下封装了PagedDataSource,我们可以利用它来对Repeater进行分页,但这个朋友说网上找到的资料多数只写出了“上一页”和“下一页”,没有总页数的列表,能够点击任意的页数进行查询,所以我就做了一份,代码如下。

代码:

 

ReapterDemo.aspx 页面 (Repeater 设置) :


<asp:Repeater id="rptData" runat="server">
                    
<ItemTemplate>
                        
<font color="#ff3333">用户ID:<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>
                            
<br>
                            名字:
&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "ShipName")%>
                            
<br>
                            地址:
&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "ShipAddress")%>
                        
</font>
                    
</ItemTemplate>
                    
<AlternatingItemTemplate>
                        
<font color="#3300ff">用户ID:<%# DataBinder.Eval(Container.DataItem, "CustomerID")%>
                            
<br>
                            名字:
&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "ShipName")%>
                            
<br>
                            地址:
&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "ShipAddress")%>
                        
</font>
                    
</AlternatingItemTemplate>
                    
<SeparatorTemplate>
                        
<hr size="1">
                    
</SeparatorTemplate>
                    
<FooterTemplate>
                        
<br>
                        
<br>
                        
<%# WriteLink()%>
                    
</FooterTemplate>
                
</asp:Repeater>


代码2:ReapterDemo.aspx.cs


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspDotNetStudy.DataAccess;

namespace AspDotNetStudy
{
    
/// <summary>
    
/// ReapterDemo 的摘要说明。
    
/// </summary>

    public class ReapterDemo : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Repeater rptData;

        
private DBOpera dbo;
        
private PagedDataSource _pds;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!this.IsPostBack)
            
{
                
if(Request["id"== null)
                
{
                    
this.BindReapter(1);
                }

                
else
                
{
                    
this.BindReapter(Convert.ToInt32(Request.QueryString["id"]));
                }

            }

        }


        
Web 窗体设计器生成的代码

        
private void BindReapter(int recordPos)
        
{
            dbo 
= new DBOpera();
            _pds 
= dbo.GetDataSource("select * from Orders", recordPos);
            Response.Write(
"查询结果占用页数:" + _pds.PageCount);
            Response.Write(
" 当前页号为:" + (_pds.CurrentPageIndex + 1).ToString());
            rptData.DataSource 
= _pds;
            rptData.DataBind();
        }


        
public string WriteLink()
        
{
            
string s = null;
            
for(int i = 1;i <= _pds.PageCount;i++)
            
{
                s 
+= "<a href=ReapterDemo.aspx?id=" + i.ToString() + 
                    
">" + i.ToString() + "页</a>&nbsp;";
            }

            
return s;
        }

    }

}



代码3:DBOpera.cs


using System;

using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace AspDotNetStudy.DataAccess
{
    
/// <summary>
    
/// DBOpera 的摘要说明。
    
/// </summary>

    public class DBOpera
    
{
        
private SqlConnection _sqlCon;
//        private SqlDataReader _sdr;
//        private SqlCommand _sqlCommand;
        private SqlDataAdapter _sda;
        
private System.Web.UI.WebControls.PagedDataSource _pds;

        
public DBOpera()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public void CreateConnection()
        
{
            _sqlCon 
= new SqlConnection("Server=.;Database=Northwind;Uid=sa;pwd=");
            _sqlCon.Open();
        }


        
public void DisConnection()
        
{
            _sqlCon.Close();
            _sqlCon.Dispose();
        }


        
public PagedDataSource GetDataSource(string strSql, int recordPos)
        
{
            DataTable tempTable 
= new DataTable();
            
try
            
{
                
this.CreateConnection();
                _sda 
= new SqlDataAdapter(strSql, _sqlCon);
                _sda.Fill(tempTable);
                _pds 
= new System.Web.UI.WebControls.PagedDataSource();
                _pds.DataSource 
= tempTable.DefaultView;
                _pds.AllowPaging 
= true;
                _pds.PageSize 
= 30;
                _pds.CurrentPageIndex 
= recordPos - 1;
                
return _pds;
            }

            
catch(Exception e)
            
{
                Console.WriteLine(e.Message);
                
return null;
            }

            
finally
            
{
                _sda.Dispose();
                _sqlCon.Dispose();
            }

        }

    }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值