当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net下Repeater使用 AspNetPager分页控件

asp.net下Repeater使用 AspNetPager分页控件

2017年12月12日  | 移动技术网IT编程  | 我要评论

一站到底20120809,杨棋涵三围,河源招聘

一、aspnetpager分页控件
分页是web应用程序中最常用到的功能之一,在asp.net中,虽然自带了一个可以分页的datagrid(asp.net 1.1)和gridview(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差、无法通过url实现分页功能等,而且有时候我们需要对datalist和repeater甚至自定义数据绑定控件进行分页,手工编写分页代码不但技术难度大、任务繁琐而且代码重用率极低,因此分页已成为许多asp.net程序员最头疼的问题之一。
aspnetpager针对asp.net分页控件的不足,提出了与众不同的解决asp.net中分页问题的方案,即将分页导航功能与数据显示功能完全独立开来,由用户自己控制数据的获取及显示方式,因此可以被灵活地应用于任何需要实现分页导航功能的地方,如为gridview、datalist以及repeater等数据绑定控件实现分页、呈现自定义的分页数据以及制作图片浏览程序等,因为aspnetpager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如sql server、oracle、access、mysql、db2等数据库以及xml文件、内存数据或缓存中的数据、文件系统等等。
二、基本属性:
alwaysshow:
默认情况下,当要分页的数据只有一页时,aspnetpager默认会自动危险期而不在页面上显示任何可见内容,将此属性值设为true时,即使总页数只有一页,aspnetpager也将显示分页导航元素。
firstpagetext:
获取或设置为第一页按钮显示的文本。
lastpagetext:
获取或设置为最后一页按钮显示的文本。
nextpagetext:
获取或设置为下一页按钮显示的文本。
prevpagetext:
获取或设置为上一页按钮显示的文本。
pagesize:
获取或设置每页显示的项数。(该值获取或设置数据呈现控件每次要显示数据表中的的数据的项数,aspnetpager根据该值和 recordcount 来计算显示所有数据需要的总页数,即 pagecount的值。 )
cssclass:
应用于控件的css类名
currentpagebuttonclass:
获取或设置aspnetpager分页控件当前页导航按钮的级联样式表 (css) 类。
pageindexboxtype:
或者或设置页索引框的显示类型,可以是允许用户手工输入的文本框和只能选择的下拉框。
showboxthreshold:
当 showpageindexbox 设为auto(默认)并且要分页的数据的总页数达到该值时会自动显示页索引输入文本框,默认值为30。该选项当 showpageindexbox 设为never或always时没有任何作用。
三、风格样式:
===== 网易风格 =====
.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;}
属性设置:cssclass="anpager" currentpagebuttonclass="cpb"
===== 拍拍网风格 =====
.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;}
属性设置:cssclass="paginator" currentpagebuttonclass="cpb"
===== 迅雷风格 =====
.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;}
属性设置:cssclass="pages" currentpagebuttonclass="cpb"
四、如何调用:
index.aspx文件
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2" %>
<%@ register assembly="aspnetpager" namespace="wuqi.webdiyer" tagprefix="webdiyer" %>
<%@ register src="hand.ascx" tagname="hand" tagprefix="uc1" %>
<!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="inc/css.css" rel="stylesheet" type="text/css" />
<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">
<uc1:hand id="hand1" runat="server" />
<div style="text-align:left; line-height:20px">
<asp:repeater id="softdown" runat="server">
<itemtemplate>
<div style="border-bottom:1px solid #000000"><%#eval("e_id")%> <%#eval("class")%><br><%#eval("downtitle")%><br><%#eval("pagehome")%></div>
</itemtemplate>
</asp:repeater>
<div style="text-align:right; padding-top:10px">
<webdiyer:aspnetpager id="aspnetpager1" runat="server" pagesize="5" cssclass="anpager" onpagechanged="aspnetpager1_pagechanged"
firstpagetext="首页" lastpagetext="尾页" nextpagetext="下一页" prevpagetext="上一页"
showmorebuttons="false" showpageindexbox="never">
</webdiyer:aspnetpager></div>
</div>
</form>
</body>
</html>

index.aspx.cs
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.data.sqlclient;
using ghsqlconn;
using wuqi.webdiyer;
public partial class default2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback) {
sqlconnection conn = db.getconnection();
conn.open();
sqlcommand count = new sqlcommand();
count.connection = conn;
count.commandtext = "select count(*) from t_softdown1";
aspnetpager1.recordcount = (int)count.executescalar();
//response.write(aspnetpager1.recordcount);
conn.close();
binddata();
}
}
public void binddata()
{
sqlconnection conn = db.getconnection();
string sql = "select * from t_softdown1 order by e_id desc";//这句在大型数据中应该用:select top查询语句
sqldataadapter da = new sqldataadapter(sql, conn);
dataset ds = new dataset();
da.fill(ds, aspnetpager1.pagesize * (aspnetpager1.currentpageindex - 1), aspnetpager1.pagesize, "temptbl");
datatable dt = ds.tables["temptbl"];
softdown.datasource=dt;
softdown.databind();
}
protected void aspnetpager1_pagechanged(object src, eventargs e)
{
//aspnetpager1.currentpageindex = e.newpageindex;
binddata();
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网