AspNetPager7.3.2版本控件分页实例
下载AspNetPagerhttp://www.webdiyer.com/Controls/AspNetPager
将AspNetPager.dll和AspNetPager.xml文件复制到asp.net网站根目录下的bin文件夹下。(或者添加网站的“添加应用”)
前台页面AspNetPager732.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AspNetPager732.aspx.cs"Inherits="XXH_FCKeditor.AspNetPager732" %>
<%@ 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分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" OnPageChanged="AspNetPager1_PageChanged" UrlPaging="true" NumericButtonTextFormatString="[{0}]"ShowCustomInfoSection="left" PageSize="3">
<%--//总是显示分页控件,即使要分页的数据只有一页//分页发生改变时触发事件//通过URL传递分页信息的方式来分页。如果设为true,禁用ViewState也能达到效果。如果设置为false,禁用了viewstate则无法实现分页.//页索引数字显示的格式//显示当前页和总页数信息,默认值不显示,为left则将显示在页索引前,为right则为页索引后--%>
</webdiyer:AspNetPager>
<asp:Repeater ID="Rep_ReadingPager" runat="server">
<HeaderTemplate><div style="height:40px; font-weight:300; font-family:华文楷体; font-size:40px;background-color:Green; text-align:center;">博客文章阅读</div></HeaderTemplate>
<ItemTemplate>
<div style="height:5px; background-color:#00FF00;"></div>
<div style="height:30px;"><a href="~/Login.aspx" target="_self">首页</a>>><%# Eval("Title")%> | 副标题:<%# Eval("subTitle")%> | 编辑:<%# Eval("Editor")%> | 作者:<%# Eval("Author")%> | 时间:<%# Eval("Modifytime")%></div>
<div><%# Eval("[Content]")%></div>
</ItemTemplate>
<FooterTemplate><div style="height:20px; background-color:#00FF00;"></div></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
后台AspNetPager732.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;
namespace XXH_FCKeditor
{
public partial class AspNetPager732 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
BindGridView();
//Rep_getArticles(); //原来的普通数据绑定方法调用
}
}
public void BindGridView()
{
string sql = "select * from Articles";//自定义的SQL语句
int recordcount;
DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, outrecordcount);
this.AspNetPager1.RecordCount = recordcount;
this.Rep_ReadingPager.DataSource = ds;
this.Rep_ReadingPager.DataBind();
AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
SqlDataAdapter ada = new SqlDataAdapter(sql, GetConnection());
DataSet ds = new DataSet();
int startRow = (currentPage - 1) * pagesize;
ada.Fill(ds, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
return ds;
}
public int GetPageRecord(string sql)
{
sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
private SqlConnection GetConnection()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
}
//原来的普通Reperter数据绑定
//public void Rep_getArticles()
//{
// string connection = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
// string sql = "SELECT Title,subTitle,Editor,Author,[Content],Creattime,Modifytime FROM Articles ORDER BY Modifytime DESC";
// SqlConnection conn = new SqlConnection(connection);
// SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
// DataSet ds = new DataSet();
// sda.Fill(ds);
// Rep_ReadingPager.DataSource = ds;
// Rep_ReadingPager.DataBind();
//}
}
}