AspNetPager1.PageSize=10; //设置每也显示的记录条数
AspNetPager1.RecordCount //总记录数
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
AlwaysShow="True" //总是显示分页控件,即使要分页的数据只有一页
OnPageChanged="AspNetPager1_PageChanged" //分页发生改变时触发事件
UrlPaging="true" //通过URL传递分页信息的方式来分页。如果设为true,禁用ViewState也能达到效果。如果设置为false,禁用了viewstate则无法实现分页.
NumericButtonTextFormatString="[{0}]" //页索引数字显示的格式
ShowCustomInfoSection="Left"> //显示当前页和总页数信息,默认值不显示,为left则将显示在页索引前,为right则为页索引后 </webdiyer:AspNetPager>
----------------------------------
protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
{
if (CheckBox1.Checked)
e.Cancel = true;//禁用分页
label1.Text = "PageChanging 事件被引发,NewPageIndex 的值是:" + e.NewPageIndex;//当前页索引
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
label2.Text = "PageChanged事件被引发,当前页索引是:" + AspNetPager1.CurrentPageIndex;//当前页
}
-------------------------------
ShowDisAbledButtons:true/false 设置当前页为1页时时候显示首页和上一页
ShowFirstLast: true/False 是否显示首页和尾页
ShowPrevNext: true/false 是否显示 上一页 和 下一页
ShowPageIndex:true/false 是否显示中间的页号
--------DataList 通过开始的记录数和当前页的结尾记录数 存储过程查询------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//存储过程查询 orders表中的记录数
int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
AspNetPager1.RecordCount = totalOrders;//获取总页数
bindData();
}
}
void bindData()
{
//获取 存储在 WebConfig中的 存储过程的名称 P_GetPagedOrders2000
DataList1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),//开始的记录数
new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));//结束的记录数
Response.Write(AspNetPager1.StartRecordIndex);
Response.Write("<br/>"+AspNetPager1.EndRecordIndex);
DataList1.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
bindData();
}
存储过程:查询 Orders表中的数据
CREATE procedure [dbo].[P_GetPagedOrders2000]
(@startIndex int, --开始号数
@endIndex int ----结束号数
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select orderid from orders order by orderid desc--从order表中查询orderid插入到@indextable表中的nid列
select O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID
inner join @indextable t on
O.orderid=t.nid
where t.id between @startIndex and @endIndex order by t.id
set nocount off
RETURN
----------------------------
SET ROWCOUNT:使 SQL Server 在返回指定的行数之后停止处理查询。
set nocount on :阻止在结果中返回可显示受 Transact-SQL 语句影响的行数的消息。
当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。当 SET NOCOUNT 为 OFF 时,返回计数。
即使当 SET NOCOUNT 为 ON 时,也更新 @@ROWCOUNT 函数。
当 SET NOCOUNT 为 ON 时,将不向客户端发送存储过程中每个语句的 DONE_IN_PROC 消息。使用由 SQL Server 2005 提供的实用工具执行查询时,其结果会防止在 Transact-SQL 语句(例如 SELECT、INSERT、UPDATE 和 DELETE)的末尾显示 nn rows affected。
如果存储过程中包含的一些语句并不返回许多实际数据,则该设置由于大量减少了网络流量,因此可显著提高性能。
SET NOCOUNT 设置是在执行或运行时设置,而不是在分析时设置。
@@ROWCOUNT:返回受上一语句影响的行数。
-------Reapeter分页----------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
AspNetPager1.RecordCount = totalOrders;
//bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次
}
}
void bindData()
{
Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
Repeater1.DataBind();
}
protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
bindData();
}
Dmeo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using Microsoft.ApplicationBlocks.Data; using System.Data; using System.Data.SqlClient; using GotDotNet.ApplicationBlocks.Data; namespace WebAppPageTest { public partial class _Default : System.Web.UI.Page { public static readonly string connStr = ConfigurationManager.ConnectionStrings[ "ConnectionString" ].ToString(); protected void Page_Load( object sender, EventArgs e) { if (!IsPostBack) { int totalOrders = ( int )SqlHelper.ExecuteScalar(connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings[ "P_GetOrderNumber" ].ToString(), new SqlParameter( "@pagesize " , AspNetPager1.PageSize), new SqlParameter( "@pageindex " , AspNetPager1.StartRecordIndex), new SqlParameter( "@docount" ,1)); AspNetPager1.RecordCount = totalOrders; //bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次 } } void bindData() { Repeater1.DataSource = SqlHelper.ExecuteDataset(connStr,CommandType.StoredProcedure, ConfigurationManager.AppSettings[ "pagedSPName" ].ToString(), new SqlParameter( "@startIndex" , AspNetPager1.StartRecordIndex), new SqlParameter( "@endIndex" , AspNetPager1.EndRecordIndex)); Repeater1.DataBind(); } protected void AspNetPager1_PageChanged( object sender, EventArgs e) { bindData(); } } } |
html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebAppPageTest._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 >AspNetPager示例—Repeater分页示例</ title > < style type="text/css"> /*网易风格*/ .anpager .cpb {background:#1F3A87 none repeat scroll 0 0;border:1px solid #CCCCCC;color:#FFFFFF;font-weight:bold;margin:5px 4px 0 0;padding:4px 5px 0;} .anpager a {background:#FFFFFF none repeat scroll 0 0;border:1px solid #CCCCCC;color:#1F3A87;margin:5px 4px 0 0;padding:4px 5px 0;text-decoration:none} .anpager a:hover{background:#1F3A87 none repeat scroll 0 0;border:1px solid #1F3A87;color:#FFFFFF;} /*拍拍网风格*/ .paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;} .paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px} .paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;} .paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none} .paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;} /*迅雷风格*/ .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;} .code{font-weight:bold;color:blue} </ style > </ head > < body > < form id="form1" runat="server"> < div > 该示例演示如何使用AspNetPager分页控件对Repeater控件进行分页 </ div > < br /> < webdiyer:AspNetPager id="AspNetPager1" runat="server" width="100%" urlpaging="true" showpageindexbox="Always" pageindexboxtype="DropDownList" textbeforepageindexbox="Go To Page: " horizontalalign="right" pagesize="5" onpagechanged="AspNetPager1_PageChanged" enabletheming="true" CssClass="anpager" CurrentPageButtonClass="cpb" FirstPageText="首页" LastPageText="尾页" NextPageText="后页" PrevPageText="前页"> </ webdiyer:AspNetPager > < asp:Repeater ID="Repeater1" runat="server"> < HeaderTemplate > < table width="100%" border="1" cellspacing="0" cellpadding="4" style="border-collapse: collapse"> < tr style="background-color: #CCCCFF"> < th style="width: 15%"> 专业编号 </ th > < th style="width: 15%"> 专业名称 </ th > < th style="width: 30%"> 备注信息 </ th > </ tr > </ HeaderTemplate > < ItemTemplate > < tr style="background-color: #FAF3DC"> < td > <%#DataBinder.Eval(Container.DataItem, "MajorID")%> </ td > < td > <%#DataBinder.Eval(Container.DataItem, "Name")%> </ td > < td > <%#DataBinder.Eval(Container.DataItem, "Remark")%> </ td > </ tr > </ ItemTemplate > < AlternatingItemTemplate > < tr style="background-color: #eaeaea"> < td > <%#DataBinder.Eval(Container.DataItem, "MajorID")%> </ td > < td > <%#DataBinder.Eval(Container.DataItem, "Name")%> </ td > < td > <%#DataBinder.Eval(Container.DataItem, "Remark")%> </ td > </ tr > </ AlternatingItemTemplate > < FooterTemplate > </ table > </ FooterTemplate > </ asp:Repeater > < webdiyer:AspNetPager ID="AspNetPager2" runat="server" CloneFrom="AspNetPager1" CssClass="paginator" CurrentPageButtonClass="cpb" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"> </ webdiyer:AspNetPager > </ form > </ body > </ html > |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步