62,041
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections.Specialized;
namespace MyPager
{
public enum EConnectionType
{
SqlSourceID,
ConnStr
}
public partial class VeryPage : System.Web.UI.UserControl
{
#region 必须设置的属性
/// <summary>
/// 连接方式。
/// </summary>
public EConnectionType ConnectionType { get; set; }
/// <summary>
/// 连接字符串,该字符串指的是web.config中的connectionStrings节点。当不使用SqlSourceID的时候则必须指定,同时将引发事件传递DataSet。
/// </summary>
public string ConnStr { get; set; }
/// <summary>
/// 指定ConnectionType为ConnStr时,引发该事件,传递DataSet。
/// </summary>
public event ForConnStr ForConnStrEvent;
/// <summary>
/// 数据源绑定控件。
/// </summary>
public string SqlSourceID { get; set; }
/// <summary>
/// 要查询的表名。
/// </summary>
public string TableName { get; set; }
/// <summary>
/// 翻页时的跳转页。
/// </summary>
public string PageName { get; set; }
/// <summary>
/// 排序字段。
/// </summary>
public string OrderField { get; set; }
#endregion
#region 一些属性
/// <summary>
/// 需要移除的参数,默认将移除page
/// </summary>
public string[] RemoveKeys { get; set; }
/// <summary>
/// 每页显示数据量,默认为10。
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 总数。
/// </summary>
int Count { get; set; }
/// <summary>
/// 一共需要分多少页。
/// </summary>
int PageCount { get; set; }
/// <summary>
/// 当前页。
/// </summary>
int NowPage { get; set; }
/// <summary>
/// 需要查询的字段名,默认为“*”。
/// </summary>
public string FieldName { get; set; }
/// <summary>
/// 条件语句。
/// </summary>
public string Where { get; set; }
/// <summary>
/// 设置在只有一页的情况下是否显示分页。
/// </summary>
public bool OnePageVisible { get; set; }
/// <summary>
/// 设置排序。
/// </summary>
public string Order { get; set; }
/// <summary>
/// 指定ConnectionType为ConnStr时,将引发事件传递。
/// </summary>
DataSet ds;
public delegate void ForConnStr(DataSet ds);
#endregion
public VeryPage()
{
this.PreRender += new EventHandler(VeryPage_PreRender);
}
protected void Page_Load(object sender, EventArgs e)
{
}
void VeryPage_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
PageSize = PageSize == 0 ? 10 : PageSize;
FieldName = FieldName != null ? FieldName : "*";
Order = Order != null ? Order : "desc";
Where = Where != null ? Where : "";
if (RemoveKeys == null)
RemoveKeys = new string[] { "page" };
//OnePageVisible = OnePageVisible != null ? OnePageVisible : false;
SqlConnection con;
SqlDataSource SqlSource = null;
if (ConnectionType == EConnectionType.SqlSourceID)
{
if (SqlSourceID == null)
{
pager.InnerHtml = "Error:使用的是SqlSourceID连接方式,但并没有指定SqlSourceID。";
return;
}
else
{
SqlSource = (SqlDataSource)Page.FindControl(SqlSourceID);
if (SqlSource != null)
{
con = new SqlConnection(SqlSource.ConnectionString);
}
else
{
pager.InnerHtml = "Error:使用的是SqlSourceID连接方式,但并没有找到SqlSourceID。";
return;
}
}
}
else
{
if (ConnStr == null)
{
pager.InnerHtml = "Error:使用的是ConnStr连接方式,但并没有指定ConnStr。";
return;
}
else
{
ConnStr = ConfigurationManager.ConnectionStrings[ConnStr].ConnectionString;
con = new SqlConnection(ConnStr);
}
}
try
{
con.Open();
}
catch
{
pager.InnerHtml = "Error:连接打开失败。";
return;
}
Count = GetCount(con, TableName, Where);
PageCount = GetPageCount(Count, PageSize);
NowPage = GetNowPage();
CreatePageLink(NowPage, PageCount, OnePageVisible);
//if (ConnectionType == EConnectionType.SqlSourceID)
//{
// SetSelectCommand(SqlSource, TableName, FieldName, Where, Order);
//}
//else
//{
// SetSelectCommand(con, TableName, FieldName, Where, Order,OrderField);
// ForConnStrEvent(ds);
//}
SetSelectCommand(con, TableName, FieldName, Where, Order, OrderField);
ForConnStrEvent(ds);
}
}
/// <summary>
/// 返回一共有多少条数据。
/// </summary>
int GetCount(SqlConnection con, string TableName, string Where)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = string.Format("select count(*) from {0} {1}", TableName, Where);
int tempcount = (int)cmd.ExecuteScalar();
con.Close();
return tempcount;
}
/// <summary>
/// 返回一共需要分多少页。
/// </summary>
int GetPageCount(int Count, int PageSize)
{
if (Count != 0)
PageSize = (int)Math.Ceiling((double)Count / PageSize);
else
PageSize = 1;
return PageSize;
}
/// <summary>
/// 返回当前页。
/// </summary>
int GetNowPage()
{
string a = Request.QueryString["page"];
int tempNowPage;
if (Request.QueryString["page"] != null &&
Request.QueryString["page"] != "" &&
Regex.IsMatch(Request.QueryString["page"], @"^[+-]?\d*$") &&
int.Parse(Request.QueryString["page"]) <= PageCount)
tempNowPage = int.Parse(Request.QueryString["page"]);
else
tempNowPage = 1;
return tempNowPage;
}
/// <summary>
/// 创建分页连接。
/// </summary>
void CreatePageLink(int NowPage, int PageCount, bool OnePageVisible)
{
if (PageCount != 1 || (PageCount == 1 && OnePageVisible == true))
{
//重组参数
NameValueCollection NewQueryString = new NameValueCollection(Request.QueryString);//将参数导出到一个新的容器
//移除不需要的参数
foreach (string key in RemoveKeys)
{
NewQueryString.Remove(key);
}
StringBuilder QS = new StringBuilder("");
foreach (string key in NewQueryString.AllKeys)
{
QS.Append("&" + key + "=" + NewQueryString[key]);
}
//
StringBuilder sb = new StringBuilder("");
sb.Append("<a href=\"" + PageName + "?page=1" + QS.ToString() + "\">最前页</a>\r\n");
int minpage = 1;
if (PageCount >= 10 && NowPage >= PageCount - 5)
{
minpage = NowPage - (4 + (NowPage - (PageCount - 5)));
}
else if (PageCount >= 10 && NowPage >= 5)
{
minpage = NowPage - 4;
}
if (PageCount >= 10)
PageCount = 10;
for (int i = 0; i < PageCount; i++)
{
if (minpage != NowPage)
{
sb.Append("<a href=\"" + PageName + "?page=" + minpage.ToString() + QS.ToString() + "\">" + minpage.ToString() + "</a>\r\n");
}
else
{
sb.Append("<a>" + NowPage.ToString() + "</a>\r\n");
}
minpage++;
}
sb.Append("<a href=\"" + PageName + "?page=" + PageCount + QS.ToString() + "\">最后页</a> 共有:" + Count + "条\r\n");
pager.InnerHtml = sb.ToString();
}
}
VeryPage.ConnectionType = MyPager.EConnectionType.SqlSourceID;
VeryPage.ConnectionType = MyPager.EConnectionType.ConnStr;
VeryPage.ForConnStrEvent += new MyPager.VeryPage.ForConnStr(VeryPage_ForConnStrEvent);
void VeryPage_ForConnStrEvent(DataSet ds)
{
_Repeater.DataSource = ds;
_Repeater.DataBind();
}