以前在利用GridView、ListView做分页显示的时候,要么使用控件自身的分页功能,要么手动写代码实现分页功能,最近发现有这么一个AspNetpager的分页控件, 还挺不错。
(1)、先下载aspnetpager.dll这个文件,在VS2005新建Web项目中添加该引用,并将其添加到工具栏中,这样可以轻易拖放。
(2)、在cs文件中添加引用:using Wuqi.Webdiyer;
以下是代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SpiltPage.aspx.cs" Inherits="SpiltPage" %>
<%@ 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>
<style type="text/css">
/*拍拍网风格*/.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;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
</div>
<webdiyer:aspnetpager CssClass="paginator" id="AspNetPager1" runat="server" OnPageChanged="ChangePage" AlwaysShow="True" FirstPageText="首页" Font-Size="Smaller" LastPageText="末页" NextPageText="下一页" NumericButtonCount="5" PrevPageText="上一页" ShowInputBox="Always"></webdiyer:aspnetpager>
</form>
</body>
</html>
----------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
public partial class SpiltPage : System.Web.UI.Page
{
string conString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
string strSQL = "select count(*) from BMSK_GB";
using (SqlCommand cmd = new SqlCommand(strSQL, con))
{
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
}
}
BindData();
}
}
//数据绑定
public void BindData()
{
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
string strSQL = "select SKBM as 水库编码,SKMC as 水库名称,SZS as 所在市,SZX as 所在县,SFWX as 是否危险 from BMSK_GB";
SqlDataAdapter da = new SqlDataAdapter(strSQL,con);
DataSet ds = new DataSet();
da.Fill(ds,AspNetPager1.PageSize*(AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"tab");
this.GridView1.DataSource = ds.Tables["tab"];
this.GridView1.DataBind();
}
}
//翻页事件
protected void ChangePage(object src, PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();
}
}
后续补充说明:
对于用存储过程,可以写一个方法返回一个可用的DataSet对象提供使用,如下:
public static DataSet GetAllUserPage(string table, string fields, string orderField, string sqlWhere,int pageSize, int pageIndex, out int totalPage, out int totalRecord)
{
DataSet ds = null;
string spName = "[dbo].[sp_super_page]";
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@TableName",SqlDbType.VarChar, 5000),
new SqlParameter("@Fields",SqlDbType.VarChar, 5000),
new SqlParameter("@OrderField",SqlDbType.VarChar, 5000),
new SqlParameter("@sqlWhere",SqlDbType.VarChar, 5000),
new SqlParameter("@pageSize",SqlDbType.Int),
new SqlParameter("@pageIndex",SqlDbType.Int),
new SqlParameter("@TotalPage",SqlDbType.Int),
new SqlParameter("@totalRecord",SqlDbType.Int)
};
parameters[0].Value = table;
parameters[1].Value = fields;
parameters[2].Value = orderField;
parameters[3].Value = sqlWhere;
parameters[4].Value = pageSize;
parameters[5].Value = pageIndex;
parameters[6].Direction = ParameterDirection.Output;
parameters[7].Direction = ParameterDirection.ReturnValue;
ds = SQLHelper.ExecuteDataset(SQLHelper.connectionStr, CommandType.StoredProcedure, spName, parameters);
totalPage = Convert.ToInt32(parameters[6].Value);
totalRecord = Convert.ToInt32(parameters[7].Value);
return ds;
}