(转) .NET实现Repeater控件+AspNetPager控件分页

时间:2021-04-03 04:42:46

SqlConnection (.NET C#) 连接及分页 
.net的访问数据机制决定了访问大量数据时会致使客户端机器消耗大量资源,因此有必要对数据进行分页显示,开发工具vs.net+sqlserver,语言c#

1.加入引用

将AspNetPager控件引入到项目中,即在aspx页面里添加引用,把AspNetPager的dll文件加到Bin文件夹目录下
using System.Data.SqlClient;
using Wuqi.Webdiyer;

2.前台显示页面aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!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>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate><ul></HeaderTemplate>
            <ItemTemplate>
                <li><%#"客户ID: "+Eval("customerid") %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%#"公司名称: "+Eval("companyname") %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%#"联系地址: "+Eval("address") %></li>
            </ItemTemplate>
        <FooterTemplate></ul></FooterTemplate>
        </asp:Repeater>
        <br />
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="" LastPageText=""
            NextPageText="下一页" OnPageChanging="AspNetPager1_PageChanging" CssClass="pages" CurrentPageButtonClass="cpb" PrevPageText="上一页">
        </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

3.后台代码部分aspx.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;
using System.Data.SqlClient;
using Wuqi.Webdiyer;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindsql();
        }
    }
    private void bindsql()
    {
        SqlConnection con = new SqlConnection("Data Source=20090731-5465;Initial Catalog=Northwind;Integrated Security=True");
        con.Open();
        SqlDataAdapter sqlda = new SqlDataAdapter("select * from customers", con);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        PagedDataSource pdsList = new PagedDataSource();
        pdsList.DataSource = ds.Tables[0].DefaultView;
        pdsList.AllowPaging = true;//数据源允许分页
        pdsList.PageSize = this.AspNetPager1.PageSize;//取控件的分页大小
        pdsList.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//显示当前页
        //设置控件
        this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;//记录总数
        this.AspNetPager1.PageSize = 10;
        this.Repeater1.DataSource = pdsList;
        this.Repeater1.DataBind();

}
    protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        bindsql();
    }
}

4.CSS文件样式表

body {
}
.pages { color: #999; }
.pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
.pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
.pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;}