让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> </SeparatorTemplate> <FooterTemplate><br><br> <%# WriteLink()%></FooterTemplate></asp:Repeater>
代码2:ReapterDemo.aspx.cs
using AspDotNetStudy.DataAccess;
namespace AspDotNetStudy
    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"]));  }
            }
        }
        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
...{
    public class DBOpera
    ...{
        private SqlConnection _sqlCon;
        private SqlDataAdapter _sda;
        private System.Web.UI.WebControls.PagedDataSource _pds;
        public DBOpera()
        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();
            }
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值