Repeater + 分页控件 AspNetPager 研究

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

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="rpt1" runat="server">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <td>sno</td>
                            <td>sname</td>
                        </tr>
                    
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td><%# Eval("sno") %></td>
                        <td><%# Eval("sname") %></td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
 <webdiyer:aspnetpager ID="AspNetPager1" runat="server" AlwaysShow="True" 
                  FirstPageText="首页" LastPageText="末页" NextPageText="下一页" PrevPageText="上一页" 
                  onpagechanged="AspNetPager1_PageChanged" NumericButtonCount="4"  PageSize="5">
              </webdiyer:aspnetpager>

        </div>
    </form>
</body>
</html>

上面是aspx代码

下面是对应的cs代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DAL;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //执行sql语句获得数据总数A
            string sql = "select count(*) from testst";
            int A = (int)SqlHelper.ExecuteScalar(sql);
            AspNetPager1.RecordCount = A;
            bindData();//执行绑定数据
        }
    }

    void bindData()
    {
        //StartRecordIndex 当前数据记录的起始索引
        //EndRecordIndex  当前页最后一条记录的索引
        //
        //NumericButtonCount = "4"  
        int PageSize = AspNetPager1.PageSize;
        int NOPageSize = PageSize * (AspNetPager1.CurrentPageIndex - 1);
        string sql = "select top "+PageSize+" * from testst where sno not in(select top "+NOPageSize +" sno from testst )";
        DataTable dt = SqlHelper.ExecuteDatatable(sql);
        rpt1.DataSource = dt;
        rpt1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData(); //连续点击 反映速度太慢  不知道为啥
    }





}

使用需要sqlhelp 类库  对应的命名空间 using DAL;

看了好多大神的代码  好复杂 自己结合repeater控件的学习 研究了一翻

AlwayShow=true              总显示分页控件

CurrentPageIndex =1     当前页的索引

FirstPageText = 首页      第一页按钮上显示的文本

LastPageText = 末页   最后一页按钮上显示的文本

PrevPageText =上一页  上一页按钮上显示的文本

NextPageText =下一页  下一页按钮上显示的文本

PageChanged = AspNetPager1_PageChanged

页面已更改事件

NumericButtonCount = 4  要显示的页索引值的按钮的数目

PageSize =5      每页显示的记录数

CustomInfoHTML    要显示在用户自定义信息区的[用户自定义HTML信息文本]

原文地址:https://www.cnblogs.com/enych/p/7792618.html