1这是一个分页控件:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace page
{
/**//// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
public class Pager : System.Web.UI.WebControls.WebControl,INamingContainer
{
[Description("选择页事件")]
public event EventHandler PageChanged;
控件属性列表#region 控件属性列表
[Browsable(false),
Description("数据集所有页的记录总数"),
Category("分页"),
DefaultValue(0)]
public int RecordCount
{
get
{
object obj=ViewState["RecordCount"];
return (obj==null)?0:(int)obj;
}
set
{
ViewState["RecordCount"]=value;
}
}
[Browsable(true),
Description("每页显示的记录数"),
Category("分页"),
DefaultValue(10)]
public int PageSize
{
get
{
object obj=ViewState["PageSize"];
return (obj==null)?10:(int)obj;
}
set
{
ViewState["PageSize"]=value;
}
}
[Browsable(false),
Description("当前显示页"),
Category("分页"),
DefaultValue(1)]
public int CurrentPage
{
get
{
object obj=ViewState["CurrentPage"];
return (obj==null)?1:(int)obj;
}
set
{
ViewState["CurrentPage"]=value;
}
}
[Browsable(false),
Description("数据集页总数"),
Category("分页"),
DefaultValue(0)]
public int PageCount
{
get
{
object obj=ViewState["PageCount"];
return (obj==null)?0:(int)obj;
}
set
{
ViewState["PageCount"]=value;
}
}
[Browsable(true),
Description("首页按钮文本,例如可用:第一页"),
Category("分页")]
public string FirstPageText
{
get
{
object obj=ViewState["FirstPageText"];
return (obj==null)?null:(string)obj;
}
set
{
ViewState["FirstPageText"]=value;
}
}
[Browsable(true),
Description("前页按钮文本,例如可用:上一页"),
Category("分页")]
public string PrevPageText
{
get
{
object obj=ViewState["PrevPageText"];
return (obj==null)?null:(string)obj;
}
set
{
ViewState["PrevPageText"]=value;
}
}
[Browsable(true),
Description("下页按钮文本,例如可用:下一页"),
Category("分页")]
public string NextPageText
{
get
{
object obj=ViewState["NextPageText"];
return (obj==null)?null:(string)obj;
}
set
{
ViewState["NextPageText"]=value;
}
}
[Browsable(true),
Description("尾页按钮文本,例如可用:最后页"),
Category("分页")]
public string LastPageText
{
get
{
object obj=ViewState["LastPageText"];
return (obj==null)?null:(string)obj;
}
set
{
ViewState["LastPageText"]=value;
}
}
#endregion
protected override void CreateChildControls()
{
Label lbl_PageInfo = new Label(); //0
lbl_PageInfo.Font.Size=10;
lbl_PageInfo.Text = "尚未绑定";
Controls.Add(lbl_PageInfo);
LinkButton btn_First = new LinkButton(); //1
btn_First.Text = FirstPageText.ToString();
btn_First.Font.Size=10;
Controls.Add(btn_First);
btn_First.Visible = false;
btn_First.Click += new System.EventHandler(btnFirst_Click);
Controls.Add(new LiteralControl(" ")); //2
LinkButton btn_Prev = new LinkButton(); //3
btn_Prev.Text =PrevPageText.ToString();
btn_Prev.Font.Size=10;
Controls.Add(btn_Prev);
btn_Prev.Visible = false;
btn_Prev.Click += new System.EventHandler(btnPrev_Click);
Controls.Add(new LiteralControl(" ")); //4
LinkButton btn_Next = new LinkButton(); //5
btn_Next.Text = ViewState["NextPageText"].ToString();
btn_Next.Font.Size=10;
Controls.Add(btn_Next);
btn_Next.Visible = false;
btn_Next.Click += new System.EventHandler(btnNext_Click);
Controls.Add(new LiteralControl(" ")); //6
LinkButton btn_Last = new LinkButton(); //7
btn_Last.Text = ViewState["LastPageText"].ToString();
btn_Last.Font.Size=10;
Controls.Add(btn_Last);
btn_Last.Visible = false;
btn_Last.Click += new System.EventHandler(btnLast_Click);
Controls.Add(new LiteralControl(" ")); //8
Label lbl_1 = new Label(); //9
lbl_1.Text = "转到";
lbl_1.Font.Size=10;
lbl_1.Visible = false;
Controls.Add(lbl_1);
DropDownList drop_CurPage = new DropDownList(); //10
drop_CurPage.Items.Add("0");
drop_CurPage.Font.Size=10;
Controls.Add(drop_CurPage);
drop_CurPage.AutoPostBack = true;
drop_CurPage.Visible = false;
drop_CurPage.SelectedIndexChanged += new System.EventHandler(ddlCurrentPage_SelectedIndexChanged);
Label lbl_2 = new Label(); //11
lbl_2.Text = "页";
lbl_2.Font.Size=10;
lbl_2.Visible = false;
Controls.Add(lbl_2);
}
protected override void OnPreRender(EventArgs e)
{
if (!Page.IsPostBack)
{
BindPager();
}
}
protected virtual void PageClick(object sender, EventArgs e)
{
if (PageChanged != null)
{
PageChanged(this,e);
}
}
页面按钮和选择框事件#region 页面按钮和选择框事件
private void ddlCurrentPage_SelectedIndexChanged(object sender, System.EventArgs e)
{
CurrentPage = ((DropDownList)Controls[10]).SelectedIndex+1;
BindPager();
SendPageChanged();
}
private void btnFirst_Click(object sender, System.EventArgs e)
{
CurrentPage = 1;
BindPager();
SendPageChanged();
}
private void btnPrev_Click(object sender, System.EventArgs e)
{
CurrentPage = CurrentPage-1;
BindPager();
SendPageChanged();
}
private void btnNext_Click(object sender, System.EventArgs e)
{
CurrentPage = CurrentPage+1;
BindPager();
SendPageChanged();
}
private void btnLast_Click(object sender, System.EventArgs e)
{
CurrentPage = PageCount;
BindPager();
SendPageChanged();
}
private void SendPageChanged()
{
if (PageChanged != null)
PageClick(this, EventArgs.Empty);
}
#endregion
绑定页面#region 绑定页面
public void BindPager()
{
PageCount=(RecordCount%PageSize>0) ? RecordCount/PageSize+1:RecordCount/PageSize; //总页数
((Label)Controls[0]).Text="共<font color=red>"+RecordCount.ToString()+"</font>条信息<font color=red>"+PageSize.ToString()+"</font>条/页 当前<font color=red>"+CurrentPage.ToString()+"</font>/<font color=red>"+PageCount.ToString()+"</font>页 ";
((DropDownList)Controls[10]).Items.Clear();
for(int i=1;i<=PageCount;i++)
{
((DropDownList)Controls[10]).Items.Add(Convert.ToString(i));
}
((DropDownList)Controls[10]).SelectedIndex = ((DropDownList)Controls[10]).Items.IndexOf(((DropDownList)Controls[10]).Items.FindByValue(Convert.ToString(CurrentPage)));
((LinkButton)Controls[1]).Visible = true;
((LinkButton)Controls[3]).Visible = true;
((LinkButton)Controls[5]).Visible = true;
((LinkButton)Controls[7]).Visible = true;
((Label)Controls[9]).Visible = true;
((Label)Controls[11]).Visible = true;
((DropDownList)Controls[10]).Visible = true;
((LinkButton)Controls[1]).Enabled = true;
((LinkButton)Controls[3]).Enabled = true;
((LinkButton)Controls[5]).Enabled = true;
((LinkButton)Controls[7]).Enabled = true;
if (CurrentPage==1)
{
((LinkButton)Controls[1]).Enabled = false;
((LinkButton)Controls[3]).Enabled = false;
((LinkButton)Controls[5]).Enabled = true;
((LinkButton)Controls[7]).Enabled = true;
}
if (CurrentPage==PageCount)
{
((LinkButton)Controls[1]).Enabled = true; //如果是自定义图片按钮可以 1ibtnFirst.Attributes["disabled"] = "disabled" 不能点; 2ibtnFirst.Attributes.Remove("disabled") ;能点
((LinkButton)Controls[3]).Enabled = true;
((LinkButton)Controls[5]).Enabled = false;
((LinkButton)Controls[7]).Enabled = false;
}
if ((PageCount-1) == 0 )
{
((LinkButton)Controls[1]).Enabled = false;
((LinkButton)Controls[3]).Enabled = false;
((LinkButton)Controls[5]).Enabled = false;
((LinkButton)Controls[7]).Enabled = false;
}
}
#endregion
2这是一个分页存储过程:
CREATE PROCEDURE sp_page
@tb varchar(50), --表名
@col varchar(50), --按该列来进行分页
@coltype int, --@col列的类型,0-数字类型,1-字符类型,2-日期时间类型
@orderby bit, --排序,0-顺序,1-倒序
@collist varchar(800),--要查询出的字段列表,*表示全部字段
@pagesize int, --每页记录数
@page int, --指定页
@condition varchar(800),--查询条件
@recordcount int OUTPUT --总记录数
AS
DECLARE @sql nvarchar(4000),@where1 varchar(800),@where2 varchar(800)
IF @condition is null or rtrim(@condition)=''
BEGIN--没有查询条件
SET @where1=' WHERE '
SET @where2=' '
END
ELSE
BEGIN--有查询条件
SET @where1=' WHERE ('+@condition+') AND '--本来有条件再加上此条件
SET @where2=' WHERE ('+@condition+') '--原本没有条件而加上此条件
END
SET @sql='SELECT @recordcount=CEILING(COUNT(*)+0.0) FROM '+@tb+@where2
EXEC sp_executesql @sql,N'@recordcount int OUTPUT',@recordcount OUTPUT--计算总记录数
IF @orderby=0
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+
' FROM '+@tb+@where1+@col+'>(SELECT MAX('+@col+') '+
' FROM (SELECT TOP '+CAST(@pagesize*(@page-1) AS varchar)+' '+
@col+' FROM '+@tb+@where2+'ORDER BY '+@col+') t) ORDER BY '+@col
ELSE
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+
' FROM '+@tb+@where1+@col+'<(SELECT MIN('+@col+') '+
' FROM (SELECT TOP '+CAST(@pagesize*(@page-1) AS varchar)+' '+
@col+' FROM '+@tb+@where2+'ORDER BY '+@col+' DESC) t) ORDER BY '+
@col+' DESC'
IF @page=1--第一页
SET @sql='SELECT TOP '+CAST(@pagesize AS varchar)+' '+@collist+' FROM '+@tb+
@where2+'ORDER BY '+@col+CASE @orderby WHEN 0 THEN '' ELSE ' DESC' END
EXEC(@sql)
GO
3c#调用函数便于调用存储过程
//生成调用存储过程的参数
/// <summary>
/// 生成SqlParameter对象
/// </summary>
/// <param name="parameterName">存储过程参数名</param>
/// <param name="dbType">SqlParameter DbType</param>
/// <param name="size">SqlParameter 大小</param>
/// <param name="parameterValue">SqlParameter 值</param>
/// /// <param name="parameterValue">判断参数是输出还是输入参数</param>
/// <returns>返回SqlParameter对象</returns>
public SqlParameter CreateStoredProcedureParam(string parameterName, SqlDbType dbType, Int32 size, object parameterValue,bool isInput)
{
SqlParameter parameter ;
parameter = new SqlParameter(parameterName,dbType,size);
if(isInput==true)
{
parameter.Value = parameterValue ;
parameter.Direction=ParameterDirection.Input;
}
else
{
parameter.Direction=ParameterDirection.Output;
}
return parameter ;
}
//生成调用存储过程的SqlCommand
public SqlCommand CreateStoredProcedureCommand(SqlConnection sqlConnection, string sp_name, SqlParameter[] parameter)
{
if ( sqlConnection.State == System.Data.ConnectionState.Closed )
sqlConnection.Open();
SqlCommand command = new SqlCommand(sp_name,sqlConnection);
command.CommandType=CommandType.StoredProcedure;
if ( parameter != null )
{
foreach( SqlParameter param in parameter)
{
command.Parameters.Add(param);
}
}
return command ;
}
//调用生成DataSet
/// <summary>
/// 运行 存储过程 返回分页DataSet
/// </summary>
/// <param name="sp_name">存储过程名称</param>
/// <param name="parameter">存储过程参数</param>
/// <returns>返回分页DataSet</returns>
public DataSet RunStoredProcedureDataSet(string sp_name, SqlParameter[] parameter)
{
using ( SqlConnection sqlConnection = GetSqlConnection() )
{
SqlCommand cmd = CreateStoredProcedureCommand(sqlConnection,sp_name,parameter);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
da.SelectCommand=cmd;
da.Fill(dataSet);
Pages.Text = cmd.Parameters["@recordcount"].Value.ToString();
sqlConnection.Close();
return dataSet;
}
}
4test.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Common;
namespace MDC
{
public class WebForm2 : System.Web.UI.Page
{
ExecSQL EXEDB=new ExecSQL();
DataSet ds;
protected page.Pager Pager1;
protected System.Web.UI.WebControls.DataGrid dg;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
initial_bind();
Pager1.DataBind();
}
}
private void initial_bind()
{
SqlParameter[] sp_temp=new SqlParameter[9];
sp_temp[0]=EXEDB.CreateStoredProcedureParam("@tb",SqlDbType.VarChar,50,"TClerk",true);
sp_temp[1]=EXEDB.CreateStoredProcedureParam("@col",SqlDbType.VarChar,800,"id",true);
sp_temp[2]=EXEDB.CreateStoredProcedureParam("@collist",SqlDbType.VarChar,800,"*",true);
sp_temp[3]=EXEDB.CreateStoredProcedureParam("@condition",SqlDbType.VarChar,800,"",true);
sp_temp[4]=EXEDB.CreateStoredProcedureParam("@pagesize",SqlDbType.Int,32,this.Pager1.PageSize,true);//取自定义控件Pager1的PageSize大小传入存储过程
sp_temp[5]=EXEDB.CreateStoredProcedureParam("@page",SqlDbType.Int,32,this.Pager1.CurrentPage,true);//取自定义控件Pager1的CurrentPage属性传入存储过程
sp_temp[6]=EXEDB.CreateStoredProcedureParam("@orderby",SqlDbType.Int,32,0,true);
sp_temp[7]=EXEDB.CreateStoredProcedureParam("@coltype",SqlDbType.Int,32,0,true);
sp_temp[8]=EXEDB.CreateStoredProcedureParam("@recordcount",SqlDbType.Int,32,"*",false);
ds=EXEDB.RunStoredProcedureDataSet("sp_page",sp_temp);
this.dg.DataSource=ds.Tables[0].DefaultView;
this.dg.DataBind();
Pager1.RecordCount =int.Parse(EXEDB.Pages.Text.ToString());
Pager1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Pager1.PageChanged+=new EventHandler(Pager1_PageChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Pager1_PageChanged(object sender,EventArgs e)
{
initial_bind();
}
#endregion
}
}