ASP.NET分页存储过程自定义用户控件(转)
网上有很多分页存储过程,但是基本上都是提供一个单纯的存储过程,没有具体的怎样去实现。最近做一个项目用户的数数据相当大(一百万以上的数据),如果用.NET自带的分页基本上是跑不动了,不是提示超时就是死在那里。于是就想到用存储过程分页来实现,去网上逛了一大圈终于找了几个比较好的存储过程。接下去就开始做测试等等,最后就干脆把它做成用户控件算了,以后用直接拖到页面上,传几个属性进去就可以实现分页,免得每次都重复同样的code。
经本人测试,对于Sqlserver的效率相当快,而对于Oracle的效率(按某个字段倒序排)不是很理想,如果不排序效率很理想,这点没有深入研究(Oracle为什么按倒序排速度很慢,在PL/SQL里也一样)。
先发布SqlServer版的存储过程(网上下载):
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@RetColumns varchar(1000) = '*', -- 需要返回的列,默认为全部
@Orderfld varchar(255), -- 排序字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType varchar(50) = 'asc', -- 设置排序类型, 非 asc 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主语句
declare @strTmp varchar(300) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @IsCount != 0 --执行总数统计
begin
if @strWhere != ''
set @strSQL = "select count(*) as Total from [" + @tblName + "] where " + @strWhere
else
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
end
else --执行查询操作
begin
if @OrderType != 'asc'
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @Orderfld +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @Orderfld +"] asc"
end
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder
if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
end
exec (@strSQL)
下面为用户控件前台html代码:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="GetPagerForSql.ascx.cs" Inherits="doHope.GetPagerForSql" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:label id="Label2" runat="server" Font-Size="9pt">共</asp:label><FONT face="宋体"> </FONT></FONT><asp:label id="lbl_RecordCnt" runat="server" Font-Size="9pt"></asp:label><FONT face="宋体"> </FONT><asp:label id="Label3" runat="server" Font-Size="9pt">项</asp:label><FONT face="宋体"> </FONT><asp:label id="Label4" runat="server" Font-Size="9pt" ForeColor="Black">|</asp:label><FONT face="宋体"> </FONT><asp:linkbutton id="lkbFirst" runat="server" Font-Size="9pt" Enabled="False" ForeColor="Black" CommandArgument="First">首页</asp:linkbutton><FONT face="宋体"> </FONT><asp:linkbutton id="lkbPre" runat="server" Font-Size="9pt" Enabled="False" ForeColor="Black" CommandArgument="Pre">上一页</asp:linkbutton><FONT face="宋体"> </FONT><asp:linkbutton id="lkbNext" runat="server" Font-Size="9pt" Enabled="False" ForeColor="Black" CommandArgument="Next">下一页</asp:linkbutton><FONT face="宋体"> </FONT><asp:linkbutton id="lkbLast" runat="server" Font-Size="9pt" Enabled="False" ForeColor="Black" CommandArgument="Last">末页</asp:linkbutton><FONT face="宋体"> </FONT><asp:label id="Label5" runat="server" Font-Size="9pt" ForeColor="Black">|</asp:label><FONT face="宋体"> </FONT><asp:label id="Label6" runat="server" Font-Size="9pt">转</asp:label><asp:textbox id="txt_CurrentPage" runat="server" Enabled="False" Width="35px" Height="18px" AutoPostBack="True"></asp:textbox><FONT face="宋体"></FONT>
<asp:label id="Label8" runat="server" Font-Size="9pt" ForeColor="Black">/</asp:label><FONT face="宋体"> </FONT><asp:label id="lbl_PageCnt" runat="server" Font-Size="9pt"></asp:label><FONT face="宋体"> </FONT><asp:label id="Label9" runat="server" Font-Size="9pt">页</asp:label>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
下面为后台代码:
namespace doHope
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 配合存储过程分页自定义控件(Sql Server)
/// By Cherish58
/// </summary>
public class GetPagerForSql : System.Web.UI.UserControl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Label lbl_PageCnt;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.LinkButton lkbLast;
protected System.Web.UI.WebControls.LinkButton lkbNext;
protected System.Web.UI.WebControls.LinkButton lkbPre;
protected System.Web.UI.WebControls.LinkButton lkbFirst;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label lbl_RecordCnt;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox txt_CurrentPage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
全局变量#region 全局变量
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得数据库连接字符
/// </summary>
protected string strconn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 初始登陆时是否绑定数据(是为true,否为false),默认为false
/// </summary>
public bool InitBindData = false ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 表名,必须赋初值
/// </summary>
public string TableName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["TableName"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["TableName"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 返回的列名,默认为全部
/// </summary>
public string RetColumns
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["RetColumns"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["RetColumns"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 查询条件字符串,默认为空
/// </summary>
public string SqlWhere
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["SqlWhere"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["SqlWhere"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 排序字段,必须赋初值
/// </summary>
public string OrderField
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["OrderField"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["OrderField"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 排序类型(升序为asc,降序为desc),默认为升序
/// </summary>
public string OrderType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["OrderType"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["OrderType"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 每页显示记录数,默认为10条
/// </summary>
public int PageSize
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return int.Parse(ViewState["PageSize"].ToString());}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["PageSize"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 初始显示为第几页,默认为第1页
/// </summary>
public int CurrentPage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return int.Parse(ViewState["CurrentPage"].ToString());}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["CurrentPage"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 数据列表控件名称,必须赋初值
/// </summary>
public string DataControlName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get...{return ViewState["DataControlName"].ToString();}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set...{ViewState["DataControlName"] = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Page_Load#region Page_Load
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Page_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(this.InitBindData)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//默认显示为第几页
ViewState["CurrentPage"] = ViewState["CurrentPage"] == null || ViewState["CurrentPage"].ToString() == "" ? "1" : ViewState["CurrentPage"].ToString() ;
//每页显示记录总数
ViewState["PageSize"] = ViewState["PageSize"] == null || ViewState["PageSize"].ToString() == "" ? 10 : int.Parse(ViewState["PageSize"].ToString()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.BindGridData() ;
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.lkbFirst.Command += new System.Web.UI.WebControls.CommandEventHandler(this.ChangePage);
this.lkbPre.Command += new System.Web.UI.WebControls.CommandEventHandler(this.ChangePage);
this.lkbNext.Command += new System.Web.UI.WebControls.CommandEventHandler(this.ChangePage);
this.lkbLast.Command += new System.Web.UI.WebControls.CommandEventHandler(this.ChangePage);
this.txt_CurrentPage.TextChanged += new System.EventHandler(this.txt_CurrentPage_TextChanged);
this.Load += new System.EventHandler(this.Page_Load);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
分页 ChangePage#region 分页 ChangePage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void ChangePage(object sender, System.Web.UI.WebControls.CommandEventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int PageCount = this.GetPageCount() ;
int CurrentPage = int.Parse(ViewState["CurrentPage"].ToString()) ;
string Change = e.CommandArgument.ToString() ;
if(Change == "Pre") //上一页
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(CurrentPage <= 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = CurrentPage - 1 ;
}
}
else if(Change == "Next") //下一页
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(CurrentPage >= PageCount)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = PageCount ;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = CurrentPage + 1 ;
}
}
else if(Change == "First") //首页
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = 1 ;
}
else //末页
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = PageCount ;
}
//显示当前页
this.txt_CurrentPage.Text = ViewState["CurrentPage"].ToString() ;
this.ProData() ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
绑定数据#region 绑定数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置分页相关的参数
/// </summary>
private void BindGridData()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//记录总数
this.lbl_RecordCnt.Text = this.GetRecordCount().ToString() ;
//总页数
this.lbl_PageCnt.Text = this.GetPageCount().ToString() ;
if(this.lbl_PageCnt.Text != "0")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//当前页
this.txt_CurrentPage.Text = ViewState["CurrentPage"].ToString() ;
}
else
this.txt_CurrentPage.Text = "0" ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//避免翻页后再查询出现列表没记录的情况
if(int.Parse(this.lbl_RecordCnt.Text) <= int.Parse(ViewState["PageSize"].ToString()))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
ViewState["CurrentPage"] = 1 ;
this.txt_CurrentPage.Text = "1" ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//绑定数据
this.ProData() ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
处理数据集#region 处理数据集
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 处理数据集
/// </summary>
/// <returns></returns>
private void ProData()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
SqlConnection conn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand("GetRecordFromPage",conn);
conn.Open() ;
cmd.CommandType = CommandType.StoredProcedure ;
cmd.Parameters.Add("@tblName",""+ViewState["TableName"].ToString()+"") ;
string retcolumns = ViewState["RetColumns"] == null || ViewState["RetColumns"].ToString() == "" ? "*" : ViewState["RetColumns"].ToString() ;
cmd.Parameters.Add("@RetColumns",retcolumns) ;
string sqlwhere = ViewState["SqlWhere"] == null || ViewState["SqlWhere"].ToString() == "" ? "" : ViewState["SqlWhere"].ToString() ;
cmd.Parameters.Add("@strWhere",sqlwhere) ;
cmd.Parameters.Add("@Orderfld",""+ViewState["OrderField"].ToString()+"") ;
cmd.Parameters.Add("@PageIndex",int.Parse(ViewState["CurrentPage"].ToString())) ;
cmd.Parameters.Add("@PageSize",""+int.Parse(ViewState["PageSize"].ToString())+"") ;
string ordertype = ViewState["OrderType"] == null || ViewState["OrderType"].ToString() == "" ? "asc" : ViewState["OrderType"].ToString() ;
cmd.Parameters.Add("@OrderType",ordertype) ;
SqlDataAdapter da = new SqlDataAdapter() ;
da.SelectCommand = cmd ;
DataSet ds = new DataSet() ;
da.Fill(ds) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//找到父页面控件并绑定(这里只对DataGrid控件绑定)
DataGrid dg = (DataGrid)this.Page.FindControl(""+ViewState["DataControlName"].ToString()+"") ;
dg.DataSource = ds ;
dg.DataBind() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
da.Dispose() ;
cmd.Dispose() ;
conn.Close() ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//控制分页按扭状态
this.StatsLinkButton() ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
控制分页按扭状态#region 控制分页按扭状态
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void StatsLinkButton()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int CurrentPage = int.Parse(ViewState["CurrentPage"].ToString()) ;
int PageCount = this.GetPageCount() ;
if(PageCount > 0)
this.txt_CurrentPage.Enabled = true ;
else
this.txt_CurrentPage.Enabled = false ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//若当前页为第一页
if(CurrentPage <=1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.lkbFirst.Enabled = false ;
this.lkbPre.Enabled = false ;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.lkbFirst.Enabled = true ;
this.lkbPre.Enabled = true ;
}
//若当前页为最后页
if(CurrentPage >= PageCount)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.lkbLast.Enabled = false ;
this.lkbNext.Enabled = false ;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.lkbLast.Enabled = true ;
this.lkbNext.Enabled = true ;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
得到记录总数、总页数#region 得到记录总数、总页数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//记录总数
private int GetRecordCount()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int RecordCount = 0 ;
string sql = "select count(*) from "+ViewState["TableName"].ToString()+" where 1=1" ;
if(ViewState["SqlWhere"] != null && ViewState["SqlWhere"].ToString() != "")
sql = sql + " and "+ViewState["SqlWhere"].ToString()+"" ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(strconn) ;
SqlCommand cmd = new SqlCommand(sql,conn) ;
conn.Open() ;
RecordCount = int.Parse(cmd.ExecuteScalar().ToString()) ;
cmd.Dispose() ;
conn.Close() ;
return RecordCount ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//总页数
private int GetPageCount()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int RecordCount = 0 ;
int YeShu = 0 ;
int psize = int.Parse(ViewState["PageSize"].ToString()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sql = "select count(*) from "+ViewState["TableName"].ToString()+" where 1=1" ;
if(ViewState["SqlWhere"] != null && ViewState["SqlWhere"].ToString() != "")
sql = sql + " and "+ViewState["SqlWhere"].ToString()+"" ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(strconn) ;
SqlCommand cmd = new SqlCommand(sql,conn) ;
conn.Open() ;
RecordCount = int.Parse(cmd.ExecuteScalar().ToString()) ;
cmd.Dispose() ;
conn.Close() ;
YeShu = RecordCount % psize ;
if(YeShu == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return RecordCount/psize ;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return RecordCount/psize + 1 ;
}
}
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
跳转#region 跳转
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void txt_CurrentPage_TextChanged(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int num = Convert.ToInt32(this.txt_CurrentPage.Text) ;
if(num > this.GetPageCount())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Page.RegisterStartupScript("","<script>alert('输入的页数已超出总页数,请重新输入!')</script>") ;
return ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ViewState["CurrentPage"] = num ;
this.ProData() ;
}
catch(Exception ee)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
Page.RegisterStartupScript("","<script>alert('请输入正确的页数!')</script>") ;
return ;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
传值后再绑定,用于有条件查询(前台调用)#region 传值后再绑定,用于有条件查询(前台调用)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 传值后再绑定,用于有条件查询
/// </summary>
public void GetDataByCond()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//默认显示为第1页
ViewState["CurrentPage"] = ViewState["CurrentPage"] == null || ViewState["CurrentPage"].ToString() == "" ? "1" : ViewState["CurrentPage"].ToString() ;
//每页显示记录总数
ViewState["PageSize"] = ViewState["PageSize"] == null || ViewState["PageSize"].ToString() == "" ? 10 : int.Parse(ViewState["PageSize"].ToString()) ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.BindGridData() ;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
使用时,只需传几个必须赋初值的属性即可:TableName为表或视图名,OrderField为排序字段,DataControlName为数据列表控件名称(这里默认是DataGrid控件,根据需要自己修改)。
里面有个InitBindData属性:初始登陆时是否绑定数据(是为true,否为false),默认为false。