using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace HySoft.DBUtility
{
///
/// 信息分页 张国杰
/// Edition 1.1
///
public class Pagination
{
private string pageBar = ""; //分页功能条
private int pageBarSize = 1; //分页功能条页码数
private string urlStr = ""; //分页链接字符串
private int pageSize = 0; //每页显示记录的数量
private int recordCount = 0; //记录总数
private int sequence = 0; //当前页次
private string searchField = ""; //查询字段
private string searchSentence = ""; //查询语句
private string sortSentence = ""; //排序语句
private string primarykey = ""; //主键
private string tableName = ""; //要查询表的名称
private string itemUnit = ""; //单位
private string imagesPath = ""; //分页功能条按钮图片存放地址 如: "../_imgs/"
#region 属性
///
/// 分页功能条
///
public string PageBar
{
get { return pageBar; }
set { pageBar = value; }
}
///
/// 分页功能条
///
public int PageBarSize
{
get { return pageBarSize; }
set { pageBarSize = value; }
}
///
/// 分页链接字符串,例 "default.aspx?id=3&cid=4"
///
public string UrlStr
{
get { return urlStr; }
set { urlStr = value; }
}
///
/// 每页显示记录的数量
///
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
///
/// 记录总数
///
public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
///
/// 当前页次/页码
///
public int Sequence
{
get
{
return (sequence != 0) ? sequence : 1;
}
set
{ sequence = value; }
}
///
/// 查询语句,例: "AND Keywords = '' AND ID = 2"
///
public string SearchSentence
{
get { return searchSentence; }
set { searchSentence = value; }
}
///
/// 排序语句,例: " ORDER BY MCardID DESC"
///
public string SortSentence
{
get { return sortSentence; }
set { sortSentence = value; }
}
///
/// 查询主体表的主键
///
public string Primarykey
{
get { return primarykey; }
set { primarykey = value; }
}
///
/// 查询主体表,例: "News"
///
public string TableName
{
get { return tableName; }
set { tableName = value; }
}
///
/// 查询字段,例: "*" 或者 "UserName,UserPassword,RealName,..."
///
public string SearchField
{
get { return searchField; }
set { searchField = value; }
}
///
/// 记录的单位名称,例:"条记录" 或 "件商品"
///
public string ItemUnit
{
get { return itemUnit; }
set { itemUnit = value; }
}
///
/// 分页功能条按钮图片存放地址 如: "../Images/"
///
public string ImagesPath
{
get { return (string.IsNullOrEmpty(imagesPath)) ? "../Images/" : imagesPath; }
set { imagesPath = value; }
}
#endregion
#region 方法
///
/// 匹配当前页次、查询条件、排序语句等参数,获取数据表
///
///
public DataTable GetDataTable()
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter("@SqlWhere", SqlDbType.VarChar,8000),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@SqlTable", SqlDbType.VarChar,8000),
new SqlParameter("@SqlField", SqlDbType.VarChar,8000),
new SqlParameter("@SqlPK", SqlDbType.VarChar,50),
new SqlParameter("@SqlOrder", SqlDbType.VarChar,200),
new SqlParameter("@Count", SqlDbType.Int),
};
parms[0].Value = searchSentence;
parms[1].Value = pageSize;
parms[2].Value = sequence;
parms[3].Value = tableName;
parms[4].Value = searchField;
parms[5].Value = primarykey;
parms[6].Value = sortSentence;
parms[7].Direction = ParameterDirection.Output;
DataTable dt = new DataTable(); ;
using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
{
dt = DbHelperSQL.ExecuteDataTable(conn, CommandType.StoredProcedure, "PaginationData", parms);
RecordCount = int.Parse((parms[7].Value.ToString()));
conn.Close();
conn.Dispose();
}
return dt;
}
///
/// 匹配当前页次、查询条件、排序语句等参数,获取数据表
///
///
public DataTable GetDataTables()
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter("@SqlWhere", SqlDbType.VarChar,8000),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@SqlTable", SqlDbType.VarChar,8000),
new SqlParameter("@SqlField", SqlDbType.VarChar,8000),
new SqlParameter("@SqlPK", SqlDbType.VarChar,50),
new SqlParameter("@SqlOrder", SqlDbType.VarChar,200),
new SqlParameter("@Count", SqlDbType.Int),
};
parms[0].Value = searchSentence;
parms[1].Value = pageSize;
parms[2].Value = sequence;
parms[3].Value = tableName;
parms[4].Value = searchField;
parms[5].Value = primarykey;
parms[6].Value = sortSentence;
parms[7].Direction = ParameterDirection.Output;
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
{
dt = DbHelperSQL.ExecuteDataTable(conn, CommandType.StoredProcedure, "PaginationData", parms);
RecordCount = int.Parse((parms[7].Value.ToString()));
conn.Close();
conn.Dispose();
}
return dt;
}
///
/// 匹配当前页次、查询条件、排序语句等参数,获取数据表
///
///
public DataTable GetDataTable3()
{
SqlParameter[] parms = new SqlParameter[]
{
new SqlParameter("@SqlWhere", SqlDbType.VarChar,8000),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int),
new SqlParameter("@SqlTable", SqlDbType.VarChar,8000),
new SqlParameter("@SqlField", SqlDbType.VarChar,8000),
new SqlParameter("@SqlPK", SqlDbType.VarChar,50),
new SqlParameter("@SqlOrder", SqlDbType.VarChar,200),
new SqlParameter("@Count", SqlDbType.Int),
};
parms[0].Value = searchSentence;
parms[1].Value = pageSize;
parms[2].Value = sequence;
parms[3].Value = tableName;
parms[4].Value = searchField;
parms[5].Value = primarykey;
parms[6].Value = sortSentence;
parms[7].Direction = ParameterDirection.Output;
DataTable dt = new DataTable(); ;
using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
{
dt = DbHelperSQL.ExecuteDataTable(conn, CommandType.StoredProcedure, "PaginationData", parms);
RecordCount = int.Parse((parms[7].Value.ToString()));
conn.Close();
conn.Dispose();
}
return dt;
}
///
/// 获取分页功能条 模式: 首页 | 上一页 | 下一页 | 尾页 第 Sequence 页 | 共 RecordCount / PageSize 页 | 每页 PageSize
///
public void PageBarByStyle1()
{
string htmlText = "
";
htmlText += "| ";
htmlText += "{0} {1} | ";
htmlText += "第{2}页 | 共{3}页 | 每页{4}{1} | ";
htmlText += "{5} | ";
htmlText += "
";
int PageCount = 0;
if ((RecordCount % PageSize) == 0)
PageCount = RecordCount / PageSize;
else
PageCount = RecordCount / PageSize + 1;
if (PageCount < Sequence)
Sequence = PageCount;
if (PageCount <= 0)
Sequence = 1;
string str = "";
if (Sequence < 2)
str += "首页 | 上一页 | ";
else
{
str += "首页 | ";
str += "上一页 | ";
}
if ((PageCount - Sequence) < 1)
str += "下一页 | 尾页 ";
else
{
str += "下一页 | ";
str += "尾页";
}
PageBar = string.Format(htmlText, RecordCount.ToString(), ItemUnit, Sequence.ToString(), PageCount.ToString(), PageSize.ToString(), str);
}
///
/// 获取分页功能条 模式: 首页 | 上一页 | 下一页 | 尾页 第 Sequence 页 | 共 RecordCount / PageSize 页 | 每页 PageSize
///
public void PageBarByStyles1()
{
string htmlText = "";
htmlText += "| 第{2}页 | 共{3}页 | 每页{4}{1} | ";
htmlText += "{5} | ";
htmlText += "
";
int PageCount = 0;
if ((RecordCount % PageSize) == 0)
PageCount = RecordCount / PageSize;
else
PageCount = RecordCount / PageSize + 1;
if (PageCount < Sequence)
Sequence = PageCount;
if (PageCount <= 0)
Sequence = 1;
string str = "";
if (Sequence < 2)
str += "首页 | 上一页 | ";
else
{
str += "首页 | ";
str += "上一页 | ";
}
if ((PageCount - Sequence) < 1)
str += "下一页 | 尾页 ";
else
{
str += "下一页 | ";
str += "尾页";
}
PageBar = string.Format(htmlText, RecordCount.ToString(), ItemUnit, Sequence.ToString(), PageCount.ToString(), PageSize.ToString(), str);
}
///
/// 数字模式分页功能条,如 << 1 2 3 4 5 6 7 8 9 >>
///
public void NumeberPageBar()
{
int PageCount = 0;
if ((RecordCount % PageSize) == 0)
PageCount = RecordCount / PageSize;
else
PageCount = RecordCount / PageSize + 1;
if (PageCount < Sequence)
Sequence = PageCount;
if (PageCount <= 0)
Sequence = 1;
string str = "Page {0} of {1} {2} {3} {4}", temp = "", previous = "", next = "";
int areaS = 1, areaE = PageCount;
if (PageCount > 10 && Sequence > 4)
{
areaS = Sequence - 4;
areaE = Sequence - 4 + 9;
}
if (Sequence > 1)
{
previous = "Previous";
next = "Next";
}
else
{
previous = "Previous";
next = "Next";
}
for (int i = areaS; i <= areaE; i++)
{
if (i == Sequence)
temp += "" + i.ToString() + " ";
else
temp += "" + i.ToString() + " ";
}
str = string.Format(str, Sequence.ToString(), PageCount.ToString(), previous, temp, next);
PageBar = str;
}
///
/// 类似百度 google的分页,如 <<{0} 上一页{1} 1 2 3 4 5 6 7 8 9{2} 下一页{3} >>{4} 跳转[8]{5}
///
public void GoNumeberPageBar1()
{
string pageHtml = "{0} {1} {2} {3} {4} {5}";
string page0 = "", page1 = "", page2 = "", page3 = "", page4 = "", page5 = "";
int tempallpage = 1;
if ((RecordCount % PageSize) == 0)
tempallpage = RecordCount / PageSize;
else
tempallpage = RecordCount / PageSize + 1;
page5 = " 共 " + tempallpage + " 页";
if (Sequence <= PageBarSize)
{
page0 = "<<";
page1 = "上一页";
}
else
{
if ((Sequence - PageBarSize) > 0)
{
page0 = "<<";
}
else
{
page0 = "<<";
}
page1 = "上一页";
}
if ((tempallpage - Sequence + 1) < PageBarSize)
{
page3 = "下一页";
page4 = ">>";
}
else
{
page3 = "下一页";
if ((Sequence + PageBarSize) > tempallpage)
{
page4 = ">>";
}
else
{
page4 = ">>";
}
}
if (tempallpage < PageSize)
{
for (int i = 1; i <= tempallpage; i++)
{
if (i == Sequence)
{
page2 += " " + i + " ";
}
else
{
page2 += " " + i + " ";
}
}
}
else
{
int tempcount = 1;
if (Sequence % PageBarSize == 0)
{
tempcount = Sequence / PageBarSize - 1;
}
else
{
tempcount = Sequence / PageBarSize;
}
tempcount = tempcount * PageBarSize;
for (int i = 1; i <= PageBarSize; i++)
{
tempcount++;
if (tempcount <= tempallpage)
{
if (tempcount == Sequence)
{
page2 += " " + tempcount + " ";
}
else
{
page2 += " " + tempcount + " ";
}
}
}
}
PageBar = string.Format(pageHtml, page0, page1, page2, page3, page4, page5);
}
///
/// 类似百度 google的分页,如 <<{0} 上一页{1} 1 2 3 4 5 6 7 8 9{2} 下一页{3} >>{4} 跳转[8]{5}
///
public void GoNumeberPageBar()
{
string pageHtml = "{0} {1} {2}";
string page0 = "", page2 = "", page4 = "";
int tempallpage = 1;
if ((RecordCount % PageSize) == 0)
tempallpage = RecordCount / PageSize;
else
tempallpage = RecordCount / PageSize + 1;
if (Sequence <= PageBarSize)
{
page0 = "";
}
else
{
if ((Sequence - PageBarSize) > 0)
{
page0 = "<<";
}
else
{
page0 = "<<";
}
}
if ((tempallpage - Sequence + 1) < PageBarSize)
{
page4 = "";
if (tempallpage != Sequence)
{
page4 = ">>";
}
}
else
{
if ((Sequence + PageBarSize) > tempallpage)
{
page4 = ">>";
}
else
{
page4 = ">>";
}
}
if (tempallpage < PageSize)
{
for (int i = 1; i <= tempallpage; i++)
{
if (i == Sequence)
{
page2 += " " + i + " ";
}
else
{
page2 += " " + i + " ";
}
}
}
else
{
int tempcount = 1;
if (Sequence % PageBarSize == 0)
{
tempcount = Sequence / PageBarSize - 1;
}
else
{
tempcount = Sequence / PageBarSize;
}
tempcount = tempcount * PageBarSize;
for (int i = 1; i <= PageBarSize; i++)
{
tempcount++;
if (tempcount <= tempallpage)
{
if (tempcount == Sequence)
{
page2 += " " + tempcount + " ";
}
else
{
page2 += " " + tempcount + " ";
}
}
}
}
if (PageBarSize >= tempallpage)
{
page0 = "";
page4 = "";
}
PageBar = string.Format(pageHtml, page0, page2, page4);
}
///
/// 类似百度 google的分页,如 <<{0} 上一页{1} 1 2 3 4 5 6 7 8 9{2} 下一页{3} >>{4} 跳转[8]{5}
///
public void GoNumeberPageBar2()
{
string pageHtml = "{6} {0} {1} {2} {3} {4} {5}";
string page0 = "", page1 = "", page2 = "", page3 = "", page4 = "", page5 = "", page6 = "";
int tempallpage = 1;
if ((RecordCount % PageSize) == 0)
tempallpage = RecordCount / PageSize;
else
tempallpage = RecordCount / PageSize + 1;
page5 = " All " + tempallpage + " Pages";// " 共 " + tempallpage + " 页";
if (tempallpage != 1)
{
int start = 1;
int end = tempallpage;
//tempallpage % PageBarSize
if (PageBarSize < tempallpage)
{
if (Sequence <= PageBarSize)
{
end = PageBarSize;
}
else
{
if ((Sequence % PageBarSize) == 0)
{
start = ((Sequence / PageBarSize) - 1) * PageBarSize + 1;
end = Sequence;
}
else
{
start = ((Sequence / PageBarSize)) * PageBarSize + 1;
end = (Sequence / PageBarSize + 1) * PageBarSize;
if (end > tempallpage)
{
end = tempallpage;
}
}
}
}
page6 = "" + start.ToString() + " - " + end.ToString() + " Pages";
}
if (Sequence <= PageBarSize)
{
page0 = "";
page1 = "";
}
else
{
if ((Sequence - PageBarSize) > 0)
{
page0 = "<<";
}
else
{
page0 = "<<";
}
page1 = "Previous";
}
if ((tempallpage - Sequence + 1) < PageBarSize)
{
page3 = "";
page4 = "";
if (tempallpage != Sequence)
{
page3 = "Next";
page4 = ">>";
}
}
else
{
page3 = "Next";
if ((Sequence + PageBarSize) > tempallpage)
{
page4 = ">>";
}
else
{
page4 = ">>";
}
}
if (tempallpage < PageSize)
{
for (int i = 1; i <= tempallpage; i++)
{
if (i == Sequence)
{
page2 += " " + i + " ";
}
else
{
page2 += " " + i + " ";
}
}
}
else
{
int tempcount = 1;
if (Sequence % PageBarSize == 0)
{
tempcount = Sequence / PageBarSize - 1;
}
else
{
tempcount = Sequence / PageBarSize;
}
tempcount = tempcount * PageBarSize;
for (int i = 1; i <= PageBarSize; i++)
{
tempcount++;
if (tempcount <= tempallpage)
{
if (tempcount == Sequence)
{
page2 += " " + tempcount + " ";
}
else
{
page2 += " " + tempcount + " ";
}
}
}
}
if (PageBarSize >= tempallpage)
{
page0 = "";
page1 = "";
page3 = "";
page4 = "";
}
PageBar = string.Format(pageHtml, page0, page1, page2, page3, page4, page5, page6);
}
///
/// 无刷新分页模式 当前第 {0} 页. 共 {1} 页. 每页 {2} 条. 共 {3} 条. 首页 上一页 下一页 末页 转到 页
///
public void GoPageBarStyle1()
{
string pageHtml = "当前第 {0}/{1} 页. 每页 {2} 条. 共 {3} 条
{4} {5}
";
string page0 = "", page1 = "", page2 = "", page3 = "", page4 = "", page5 = "";
page0 = Sequence.ToString();
int tempallpage = 1;
if ((RecordCount % PageSize) == 0)
tempallpage = RecordCount / PageSize;
else
tempallpage = RecordCount / PageSize + 1;
page1 = tempallpage.ToString();
page2 = PageSize.ToString();
page3 = RecordCount.ToString();
page5 = "" + tempallpage + "){pagination(" + tempallpage + ");}else{pagination(strgonum);}}else{pagination(" + Sequence + ");}\" style='width:30px;cursor:hand;'>转到 页";
if (Sequence < 2)
page4 += "首页 上一页";
else
{
page4 += "首页 ";
page4 += "上一页 ";
}
if ((tempallpage - Sequence) < 1)
page4 += " 下一页 末页";
else
{
page4 += " 下一页 ";
page4 += "末页";
}
PageBar = string.Format(pageHtml, page0, page1, page2, page3, page4, page5);
}
///
/// 无刷新分页模式 当前第 {0} 页. 共 {1} 页. 每页 {2} 条. 共 {3} 条. 首页 上一页 下一页 末页 转到 页
///
public void GoPageBarStyle1(string funname)
{
string pageHtml = "当前第 {0}/{1} 页. 每页 {2} 条. 共 {3} 条
{4} {5}
";
string page0 = "", page1 = "", page2 = "", page3 = "", page4 = "", page5 = "";
page0 = Sequence.ToString();
int tempallpage = 1;
if ((RecordCount % PageSize) == 0)
tempallpage = RecordCount / PageSize;
else
tempallpage = RecordCount / PageSize + 1;
page1 = tempallpage.ToString();
page2 = PageSize.ToString();
page3 = RecordCount.ToString();
page5 = "" + tempallpage + "){" + funname + "(" + tempallpage + ");}else{" + funname + "(strgonum);}}else{" + funname + "(" + Sequence + ");}\" style='width:30px;cursor:hand;'>转到 页";
if (Sequence < 2)
page4 += "首页 上一页";
else
{
page4 += "首页 ";
page4 += "上一页 ";
}
if ((tempallpage - Sequence) < 1)
page4 += " 下一页 末页";
else
{
page4 += " 下一页 ";
page4 += "末页";
}
PageBar = string.Format(pageHtml, page0, page1, page2, page3, page4, page5);
}
///
/// 分页功能条(无刷新模式)
///
public void PageBarUnRefresh()
{
string htmlText = "";
htmlText += "
{0}
";
htmlText += "共计: {1} {2} {3}
";
htmlText += "";
int intPageCount = 0;
if ((RecordCount % PageSize) == 0)
intPageCount = RecordCount / PageSize;
else
intPageCount = RecordCount / PageSize + 1;
string str = "";
if (Sequence < 2)
str += "
";
else
{
str += "
";
str += "
";
}
if ((intPageCount - Sequence) < 1)
str += "
";
else
{
str += "
";
str += "
";
}
string strPage = Sequence.ToString() + "/" + intPageCount.ToString();
PageBar = string.Format(htmlText, str, RecordCount, ItemUnit, strPage);
}
///
/// 分页功能条(无刷新模式)
///
public void PageBarUnRefreshbak()
{
string htmlText = "";
htmlText += "
{0}
";
htmlText += "共计: {1} {2} {3} {4}
";
htmlText += "";
int intPageCount = 0;
if ((RecordCount % PageSize) == 0)
intPageCount = RecordCount / PageSize;
else
intPageCount = RecordCount / PageSize + 1;
string str = "";
if (Sequence < 2)
str += "
";
else
{
str += "
";
str += "
";
}
if ((intPageCount - Sequence) < 1)
str += "
";
else
{
str += "
";
str += "
";
}
string page5 = "";
string strPage = Sequence.ToString() + "/" + intPageCount.ToString();
page5 = " 页 ";
PageBar = string.Format(htmlText, str, RecordCount, ItemUnit, strPage, page5);
}
///
/// 分页功能条(无刷新模式)
///
/// 子列表分页编序paginationNum,子列表上级ID
public void ChildPageBarUnRefresh(int i)
{
string htmlText = "";
htmlText += "
{0}
";
htmlText += "共计: {1} {2} {3}
";
htmlText += "";
int intPageCount = 0;
if ((RecordCount % PageSize) == 0)
intPageCount = RecordCount / PageSize;
else
intPageCount = RecordCount / PageSize + 1;
string str = "";
if (Sequence < 2)
str += "
";
else
{
str += "
";
str += "
";
}
if ((intPageCount - Sequence) < 1)
str += "
";
else
{
str += "
";
str += "
";
}
string strPage = Sequence.ToString() + "/" + intPageCount.ToString();
PageBar = string.Format(htmlText, str, RecordCount, ItemUnit, strPage);
}
#region 分页 法拉利模式分页
public void PageBarFerrari()
{
int intPageCount = 0;
if ((RecordCount % PageSize) == 0)
intPageCount = RecordCount / PageSize;
else
intPageCount = RecordCount / PageSize + 1;
string str = "";
if (Sequence < 2)
{
str += "
";
str += "
";
}
else
{
str += "
";
str += "
";
}
str += "第 " + Sequence + " 页";
if ((intPageCount - Sequence) < 1)
{
str += "
";
str += "
";
}
else
{
str += "
";
str += "
";
}
//string strPage = Sequence.ToString() + "/" + intPageCount.ToString();
PageBar = str + " ";//string.Format(htmlText, str, RecordCount, ItemUnit, strPage);
}
#endregion
#endregion
#region 构造
public Pagination()
{
}
#endregion
}
}