利用AspNetPager控件实现数据分页(存储过程)
一、准备工作:
1、首先把AspNetPager.dll,引用到项目中来
2、添加到工具箱中,在需要用到得时候拖拽进来
二、进入代码工作:
1、建立Pro_page.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pro_page.aspx.cs" Inherits="Pro_page" %>
<%@ 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>无标题页</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
</div>
<div style="width:60%;">
<div style="width:40%;float:left;"><asp:Label ID="CustomInfoHTML" runat="server"></asp:Label></div>
<div style="width:60%;float:left;"><webdiyer:AspNetPager ID="AspNetPager1" runat="server" OnPageChanged="AspNetPager1_PageChanged" CssClass="pages" CurrentPageButtonClass="cpb" FirstPageText="首页" LastPageText="尾页" NextPageText="下页" PrevPageText="上页">
</webdiyer:AspNetPager></div>
</div>
</form>
</body>
</html>
2、页面Pro_page.aspx.cs的代码:
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;
public partial class Pro_page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
AspNetPager1.PageSize = 2;
Binds();
}
}
public void Binds()
{
SqlHelper sqlhelp = new SqlHelper(); //此处实例化数据库连接类,换成自己的就可以了
using(SqlConnection conn=new SqlConnection(sqlhelp.ConnString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "P_GetPagedReCord"; //存储过程名
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
int pageindex = 0;
string strWhere = "";
if (AspNetPager1.CurrentPageIndex < 1)
{
pageindex = 1;
}
else
{
pageindex = AspNetPager1.CurrentPageIndex;
}
SqlParameter[] parameters ={
new SqlParameter("@tblname",SqlDbType.VarChar,255),
new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
new SqlParameter("@fldName",SqlDbType.VarChar,255),
new SqlParameter("@pagesize",SqlDbType.Int,4),
new SqlParameter("@pageindex",SqlDbType.Int,4),
new SqlParameter("@doCount",SqlDbType.Bit),
new SqlParameter("@ordertype",SqlDbType.Bit),
new SqlParameter("@strwhere",SqlDbType.VarChar,2000)
};
parameters[0].Value = "VoteTitle";//数据表名称.
parameters[1].Value = "VoteID,VoteTitle";//要显示的字段名
parameters[2].Value = "VoteID"; //主键
parameters[3].Value = AspNetPager1.PageSize;//每页显示记录数
parameters[4].Value = pageindex;//索引页
parameters[5].Value = 0;//这里写死0就行了
parameters[6].Value = 0;//这里的1是降序,0为升序
parameters[7].Value = strWhere;//查询条件组合
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
//动态设置用户自定义文本内容
AspNetPager1.RecordCount = int.Parse(ds.Tables[1].Rows[0][0].ToString());
AspNetPager1.CustomInfoHTML = "记录总数:" + AspNetPager1.RecordCount.ToString() + "条,";
AspNetPager1.CustomInfoHTML += "共有" + AspNetPager1.PageCount.ToString() + "页,";
AspNetPager1.CustomInfoHTML += "当前页:" + AspNetPager1.CurrentPageIndex.ToString();
CustomInfoHTML.Text = AspNetPager1.CustomInfoHTML;
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
Binds();
}
}
3、存储过程代码P_GetPagedReCord:
CREATE PROCEDURE P_GetPagedReCord
@tblname VARCHAR(255), -- 表名
@strGetFields nvarchar(1000) = "*", -- 需要返回的列
@fldName varchar(255)='', -- 排序的字段名
@PageSize int = 2, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回, 非0 值则返回记录总数
@OrderType bit = 0, -- 设置排序类型, 非0 值则降序
@strWhere varchar(1500) = '' -- 查询条件(注意: 不要加where)
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end --以上代码的意思是如果@doCount传递过来的不是,就执行总数统计。以下的所有代码都是@doCount为的情况:
else
begin
if @OrderType != 0--降序
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'--如果@OrderType不是0,就执行降序,这句很重要!
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' ' + @strGetFields + ' from [' + @tblName + '] ' + @strOrder--如果是第一页就执行以上代码,这样会加快执行速度
end
else
begin--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)' + @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where [' + @fldName + ']' + @strTmp + '([' + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where 1=1 ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and 1=1 ' + @strWhere + ' ' + @strOrder
end
if @strWhere !='' --得到记录的总行数
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + '] where 1=1 '+ @strWhere
else
set @strSQL =@strSQL+ '; select count(*) as Total from [' + @tblName + ']'
end
exec (@strSQL)
RETURN