博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简单的repeater分页代码

Posted on 2009-03-07 14:47  吕霖  阅读(1915)  评论(3编辑  收藏  举报

repeater控件相对于gridiew控件有很多优势,比如使用方便灵活,由服务端生成的代码量很少等等,今天记录下repeater的简单分页方法,以Northwind数据库的Customers表为例:

先演示下最终效果:

 

步骤如下:

1、创建分页的存储过程,我使用的MS SQL 2005:

Create PROCEDURE [dbo].[GetCustomer]
 (
 @pageCount int = 10,     --每页记录数
 @pageIndex int = 1,      --当前页
 @pageTotal int = 1 out     --返回页总数
 )
AS
Begin
 declare @recordTotal int     --记录总数
 select @recordTotal = count(*) from Customers
 if (@recordTotal % @pageCount = 0)     --计算是否可以除尽
 begin
  set @pageTotal = @recordTotal / @pageCount
 end
 else
 begin
  set @pageTotal = @recordTotal / @pageCount + 1  --否则加一
 end
 With CTE as     --VS2005的分页代码
 (
  select Customers.* , row_number() over (order by CustomerID) as rn from Customers
 )
 select * from CTE where rn between @pageCount * (@pageIndex -1) + 1 and  @pageCount * @pageIndex
End
return

 

2、新建web应用程序reapeater,在项目中添加类Customer,此处编写Customers的实体类代码。如下:

 

namespace reapeater
{
    public class Customer
    {
        public string CustomerID
        {
            get;
            set;
        }
        public string CompanyName
        {
            get;
            set;
        }
        public string ContactName
        {
            get;
            set;
        }
        public string ContactTitle
        {
            get;
            set;
        }
        public string Address
        {
            get;
            set;
        }
        public string City
        {
            get;
            set;
        }
        public string Region
        {
            get;
            set;
        }
        public string PostalCode
        {
            get;
            set;
        }
        public string Country
        {
            get;
            set;
        }

        public string Phone
        {
            get;
            set;
        }

        public string Fax
        {
            get;
            set;
        }
    }
}

3、前台aspx代码

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="reapeater._Default" EnableViewState="false" %>

<!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>
    <style type="text/css">
    body
    {
        text-align:center;
    }
    table
    {   
     border-collapse:collapse;
     font-family:宋体;     
    }
    thead td
    {
     font-style:inherit;
     font-size:10pt;       
        background-color: Blue;
     border:1px solid #000;
        color:White;
        height:26px;
     padding:3 5 3 5;
    }
    tbody tr
    {
        height:30px;
    }
    tbody td
    {
     border:1px solid #000;
     border-collapse:collapse;
     margin:0 0 0 0;
     padding:3 5 3 5;
    }
    a:link
    {   
        color : #039;
        text-decoration: none;
    }

    a:visited
    {
        color : #039;
        text-decoration: none;     
    }
    a:hover
    {
        color : #F30;
        text-decoration: none;
    }
    a:active
    {
    }
    .pager
    {
     text-align:center;
     width:300px;
     float:right;
     height:30px;
     margin-top:15px;
    }
    .page, .pageside
    {
     text-align:center;     width:20px;
     height:18px;
     border:1px solid #11ab01;
     background-color:#f5fe96;
     margin:5 3 2 3;
     color:#11ab01;
     float:left;
     font-size:11pt;
     display:block;
     vertical-align:middle;
     padding-top:3px;
    }
    .pageside
    {
     width:45px;
     background-color:#11ab01;
     color:#f5fe96;
     font-size:10pt;     
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="dd" class="pager"><asp:Literal ID="lt2" runat="server"></asp:Literal></div>
    <asp:Repeater ID="rptCustomers" runat="server">
    <HeaderTemplate>
    <table cellpadding="0" cellspacing="0">
    <thead>
    <td>CustomerID</td>
    <td>CompanyName</td>
    <td>ContactName</td>
    <td>ContactTitle</td>
<%--    <td>Address</td>
    <td>City</td>
    <td>PostalCode</td>
    <td>Country</td>
    <td>Phone</td>
    <td>Fax</td>--%>
    </thead>
    <tbody>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <td><%#Eval("CustomerID") %></td>
        <td><%#Eval("CompanyName") %></td>
        <td><%#Eval("ContactName") %></td>
        <td><%#Eval("ContactTitle") %></td>
<%--        <td><%#Eval("Address") %></td>
        <td><%#Eval("City") %></td>
        <td><%#Eval("PostalCode") %></td>
        <td><%#Eval("Country") %></td>
        <td><%#Eval("Phone") %></td>
        <td><%#Eval("Fax") %></td>--%>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </tbody>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </div>
    <div id="pp" class="pager"><asp:Literal ID="lt" runat="server"></asp:Literal></div>
    </form>
</body>
</html>

4、CS代码

 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

namespace reapeater
{
    public partial class _Default : System.Web.UI.Page
    {
        public int pageCount = 7;  //分页部分需要显示的标签数    可将该值写入webconfig文件动态获取
        public int pageIndex = 1;  //当前页码
        public int pageTotal = 1;  //总页数
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (null != Request.QueryString["id"])
                {
                    pageIndex = Convert.ToInt32(Request.QueryString["id"]);  //获取当前页码
                }
                BindData();
                SetPageString();
            }
        }

          //绑定数据
        public void BindData()
        {
            SqlConnection con =
                new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "GetCustomer";
            cmd.Parameters.Add(new SqlParameter("@pageCount", SqlDbType.Int, 4));
            cmd.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int, 4));
            cmd.Parameters.Add(new SqlParameter("@pageTotal", SqlDbType.Int, 4));
            cmd.Parameters["@pageCount"].Value = pageCount;     //每页记录数
            cmd.Parameters["@pageIndex"].Value = pageIndex;     //第几页
            cmd.Parameters["@pageTotal"].Direction = ParameterDirection.Output;     //指定参数为out类型,从存储过程取值(页总数)
            List<Customer> list = new List<Customer>();
            con.Open();
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    Customer cust = new Customer();
                    cust.CustomerID = dr.GetString(0);
                    cust.CompanyName = dr.GetString(1);
                    cust.ContactName = dr.GetString(2);
                    cust.ContactTitle = dr.GetString(3);
                    cust.Address = dr.IsDBNull(4)?"":dr.GetString(4);
                    cust.City = dr.IsDBNull(5)?"":dr.GetString(5);
                    cust.Region = dr.IsDBNull(6)? "":dr.GetString(6);
                    cust.PostalCode = dr.IsDBNull(7)?"":dr.GetString(7);
                    cust.Country = dr.IsDBNull(8)?"":dr.GetString(8);
                    cust.Phone = dr.IsDBNull(9)?"":dr.GetString(9);
                    cust.Fax = dr.IsDBNull(10) ?"": dr.GetString(10);
                    list.Add(cust);
                }
            }
            pageTotal = Convert.ToInt32(cmd.Parameters["@pageTotal"].Value);
            con.Close();
            this.rptCustomers.DataSource = list;
            rptCustomers.DataBind();
        }

          //设定分页页码及链接
        public void SetPageString()
        {
            StringBuilder sb = new StringBuilder();

            if (pageIndex <= (pageCount/2))
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    sb.Append("<div class='page'><a href='?id=" + i.ToString() + "'>" + i.ToString() + "</a></div>");
                }
                sb.Append("<div class='pageside'><a href='?id=" + pageTotal.ToString() + "'>尾页</a></div>");
            }
            else
            {
                if (pageIndex >= (pageTotal - pageCount / 2))
                {
                    sb.Append("<div class='pageside'><a href='?id=1'>首页</a></div>");
                    for (int i = pageTotal - pageCount; i <= pageTotal; i++)
                    {
                        sb.Append("<div class='page'><a href='?id=" + i.ToString() + "'>" + i.ToString() + "</a></div>");
                    }
                }
                else
                {
                    sb.Append("<div class='pageside'><a href='?id=1'>首页</a></div>");
                    for (int i = pageIndex - (pageCount / 2); i <= pageIndex + (pageCount / 2); i++)
                    {
                        sb.Append("<div class='page'><a href='?id=" + i.ToString() + "'>" + i.ToString() + "</a></div>");
                    }
                    sb.Append("<div class='pageside'><a href='?id=" + pageTotal.ToString() + "'>尾页</a></div>");
                }
            }
            this.lt.Text = sb.ToString();
            this.lt2.Text = sb.ToString();
        }
    }
}

代码没有经过整理,有不足之处,欢迎各位拍砖!

文件下载:reapeater.rar