分享一个我的分页代码

BATTLERxANGE 2010-09-20 11:02:00

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();
}
}
...全文
1523 66 打赏 收藏 转发到动态 举报
写回复
用AI写文章
66 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2011-09-19
  • 打赏
  • 举报
回复
一年前的帖子,又被挖出来了....
沃尔沃 2011-09-19
  • 打赏
  • 举报
回复
太好了。。。。
a78782295 2011-09-19
  • 打赏
  • 举报
回复
直接封装成DLL吧, 2条内裤表示压力很大
薛小坏 2011-09-19
  • 打赏
  • 举报
回复
呵呵 顶
erturetet 2011-09-19
  • 打赏
  • 举报
回复
好帖,学习,顶!
梦纷飞舞 2011-09-19
  • 打赏
  • 举报
回复
学习了!!!
highpr 2011-09-19
  • 打赏
  • 举报
回复
确实很久的帖子,收藏了
rart2008 2010-09-21
  • 打赏
  • 举报
回复
好人一生平安
baysos 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 wubudang 的回复:]
看来用自己写分页代码的人物还不少啊
[/Quote]
基本上都写得出~只是效率和扩展等问题~
灵雨飘零 2010-09-21
  • 打赏
  • 举报
回复
谢谢分享!
gcyabc123 2010-09-21
  • 打赏
  • 举报
回复
谢谢分享!
yoyo_ 2010-09-21
  • 打赏
  • 举报
回复
好东西,谢谢分享哦~~
forcall 2010-09-21
  • 打赏
  • 举报
回复
标记一下,日后慢慢研究
yangquanlaohou 2010-09-21
  • 打赏
  • 举报
回复
学习了。。学习了。。学习了。。
gul_gui 2010-09-21
  • 打赏
  • 举报
回复
代码太长了 封装成用户控件吧
chen_ya_ping 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
太多了,直接提供DLL下载使用
[/Quote]
然后写个使用文档,供大家参考
yuhuiwqvb 2010-09-21
  • 打赏
  • 举报
回复
都是些高手!不过代码真的好长.....
BATTLERxANGE 2010-09-21
  • 打赏
  • 举报
回复
另外也可以设置成:

VeryPage.ConnectionType = MyPager.EConnectionType.SqlSourceID;

这样就不需要注册事件也不需要改其他的任何东西了,只需要托上去,属性和上面的一样设置,但是将
ConnStr改成SqlSourceID="数据源ID例如SqlDataSource1"
BATTLERxANGE 2010-09-21
  • 打赏
  • 举报
回复
特点就是使用简单!效率一般。
拖上去直接用,一般设置一些属性就可以了,例如:
<uc1:VeryPage ID="VeryPage" runat="server" TableName="表名" PageName="当前需要分页的页面名如123.aspx"
PageSize="一页显示多少条数据" ConnStr="数据源连接字符串,该字符串在web.config的<connectionStrings>节点定义,设置其NAME属性就可以了" FieldName="需要显示的字段,可以不设置,默认为*" OrderField="排序字段"
Order="asc或desc" Where="条件" />

如果使用ConnStr的方式,则需要在代码中设置:
VeryPage.ConnectionType = MyPager.EConnectionType.ConnStr;

还需要注册ForConnStrEvent事件,该事件传递一个查询后的DATASET,例如:

VeryPage.ForConnStrEvent += new MyPager.VeryPage.ForConnStr(VeryPage_ForConnStrEvent);

void VeryPage_ForConnStrEvent(DataSet ds)
{
_Repeater.DataSource = ds;
_Repeater.DataBind();
}

shichao102471077 2010-09-21
  • 打赏
  • 举报
回复
学习了。。
加载更多回复(40)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧