本文共 3209 字,大约阅读时间需要 10 分钟。
using System;
using System.Collections;using System.ComponentModel;using System.Data;using System.IO;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.Configuration;using System.Data.SqlClient; using System.Text;using System.Text.RegularExpressions;using System.Web.Caching;namespace blackflash{
/// <summary> /// Pager 的摘要说明。 /// </summary> [DefaultProperty("Text"), ToolboxData("<{0}:Pager runat=server></{0}:Pager>")] public class Pager : Control { private int maxpage; private int page; private int per; private int total; private string url; [Bindable(true), Category("Appearance"), DefaultValue("")]public Pager()
{ this.url = "?"; this.page = 1; this.maxpage = 1; this.per = 20; this.total = 0; } public int MaxPage { get { return this.maxpage; } set { this.maxpage = value; } } public int PageId { get { return this.page; } set { this.page = value; } } public int Per { get { return this.per; } set { this.per = value; } } public int Total { get { return this.total; } set { this.total = value; } } public string Url { get { return this.url; } set { this.url = value; } } /// <summary> /// 将此控件呈现给指定的输出参数。 /// </summary> /// <param name="output"> 要写出到的 HTML 编写器 </param> protected override void Render(HtmlTextWriter output) { output.Write(this.GetContent()); } /// <summary> ///分页数据集返回DataTable数据,分页程序 /// </summary> /// <param name="Table">表名 必须有</param> /// <param name="Condition">条件比如:issenderdel=0 可以为空</param> /// <param name="Compositor">排序 id desc,可以为空</param> /// <param name="Mainkey">主键,必须有</param> /// <param name="list">列名,可以为空,空为*,所以列</param> /// <returns></returns> public DataTable GetData(string Table,string Condition,string Compositor,string Mainkey,string list) { string SqlStr; if (list == "") { list="*"; } SqlStr="select count(*) from "+Table; if (Condition!="") { SqlStr+=" where "+Condition; } this.total=Convert.ToInt32(DataAccess.ExecuteScalar(SqlStr));if (total%per==0)
{ this.maxpage= total/per; } else { this.maxpage= total/per+1; } if (this.page<1) { this.page = 1 ; } else if (page>maxpage) { this.page = maxpage; } if (this.page<=1) { SqlStr="select top " + this.per + " "+list+" from "+Table; if (Condition !="") { SqlStr+=" where "+Condition; } if (Compositor !="") { SqlStr+=" order by "+Compositor; } } else { SqlStr="select top " + this.per + " "+list+" from "+Table+" where "+Mainkey+" not in (select top " + ((page-1)*per) + " "+Mainkey+" from "+Table; if (Condition!="") { SqlStr+=" where "+Condition; } if (Compositor !="") { SqlStr+=" order by "+Compositor; } if (Condition!="") { SqlStr+=") and "+Condition; if (Compositor !="") { SqlStr+=" order by "+Compositor; } } else { SqlStr+=")"; if (Compositor !="") { SqlStr+=" order by "+Compositor; } } } return DataAccess.dataTable(SqlStr); } public string GetContent() { StringBuilder sb = new StringBuilder(); sb.Append("<script language='javascript'>"); sb.Append("var s = pager('"+this.url+"total="+this.total+"&',"+ this.page +", "+this.maxpage+"," +this.total+ ","+ this.per+", true);"); sb.Append("document.write(s);"); sb.Append("</script>");return sb.ToString();
}}
}转载地址:http://cbptd.baihongyu.com/