建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 数据库 > MSSQL Server教程 >

sqlserver如何实现高性能分页(2)

时间:2011-04-20 21:16来源: 作者: 点击:
分页控件 using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; us


分页控件

using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// 扩展连接字符串
/// </summary>
public class ExStringBuilder
{
private StringBuilder InsertString;
private StringBuilder PageString;
private int PrivatePageNum = 1;
private int PrivateMaxPageSize = 25;
private int PrivateMaxPages = 10;
private int PrivateCount;
private int PrivateAllPage;
public ExStringBuilder()
{
InsertString = new StringBuilder("");
}
/// <summary>
/// 得到生成的HTML
/// </summary>
public string GetHtml
{
get
{
return InsertString.ToString();
}
}
/// <summary>
/// 得到生成的分页HTML
/// </summary>
public string GetPageHtml
{
get
{
return PageString.ToString();
}
}
/// <summary>
/// 设置或获取目前页数
/// </summary>
public int PageNum
{
get
{
return PrivatePageNum;
}
set
{
if (value >= 1)
{
PrivatePageNum = value;
}
}
}
/// <summary>
/// 设置或获取最大分页数
/// </summary>
public int MaxPageSize
{
get
{
return PrivateMaxPageSize;
}
set
{
if (value >= 1)
{
PrivateMaxPageSize = value;
}
}
}
/// <summary>
/// 设置或获取每次显示最大页数
/// </summary>
public int MaxPages
{
get
{
return PrivateMaxPages;
}
set
{
PrivateMaxPages = value;
}
}
/// <summary>
/// 设置或获取数据总数
/// </summary>
public int DateCount
{
get
{
return PrivateCount;
}
set
{
PrivateCount = value;
}
}
/// <summary>
/// 获取数据总页数
/// </summary>
public int AllPage
{
get
{
return PrivateAllPage;
}
}
/// <summary>
/// 初始化分页
/// </summary>
public void Pagination()
{
PageString = new StringBuilder("");
//得到总页数
PrivateAllPage = (int)Math.Ceiling((decimal)PrivateCount / (decimal)PrivateMaxPageSize);
//防止上标或下标越界
if (PrivatePageNum > PrivateAllPage)
{
PrivatePageNum = PrivateAllPage;
}
//滚动游标分页方式
int LeftRange, RightRange, LeftStart, RightEnd;
LeftRange = (PrivateMaxPages + 1) / 2-1;
RightRange = (PrivateMaxPages + 1) / 2;
if (PrivateMaxPages >= PrivateAllPage)
{
LeftStart = 1;
RightEnd = PrivateAllPage;
}
else
{
if (PrivatePageNum <= LeftRange)
{
LeftStart = 1;
RightEnd = LeftStart + PrivateMaxPages - 1;
}
else if (PrivateAllPage - PrivatePageNum < RightRange)
{
RightEnd = PrivateAllPage;
LeftStart = RightEnd - PrivateMaxPages + 1;
}
else
{
LeftStart = PrivatePageNum - LeftRange;
RightEnd = PrivatePageNum + RightRange;
}
}

//生成页码列表统计
PageString.Append(...);

StringBuilder PreviousString = new StringBuilder("");
//如果在第一页
if (PrivatePageNum > 1)
{
...
}
else
{
...
}
//如果在第一组分页
if (PrivatePageNum > PrivateMaxPages)
{
...
}
else
{
...
}
PageString.Append(PreviousString);
//生成中间页
for (int i = LeftStart; i <= RightEnd; i++)
{
//为当前页时
if (i == PrivatePageNum)
{
...
}
else
{
...
}
}
StringBuilder LastString = new StringBuilder("");
//如果在最后一页
if (PrivatePageNum < PrivateAllPage)
{
...
}
else
{
...
}
//如果在最后一组
if ((PrivatePageNum + PrivateMaxPages) < PrivateAllPage)
{
...
}
else
{
...
}
PageString.Append(LastString);
}
/// <summary>
/// 生成Tag分类表格
/// </summary>
public void TagTable(ExDataRow myExDataRow)
{
InsertString.Append(...);
}

调用方法:
//得到分页设置并放入Session
ExRequest myExRequest = new ExRequest();
myExRequest.PageSession("Tag_", new string[] { "page", "size" });
//生成Tag分页
ExStringBuilder Tag = new ExStringBuilder();
//设置每次显示多少条纪录
Tag.MaxPageSize = Convert.ToInt32(Session["Tag_size"]);
//设置最多显示多少页码
Tag.MaxPages = 9;
//设置当前为第几页
Tag.PageNum = Convert.ToInt32(Session["Tag_page"]);
string[][] myNamenValue = new string[2][]{
new string[]{"MaxPageSize","PageNum","Count"},
new string[]{Tag.MaxPageSize.ToString(),Tag.PageNum.ToString()}
};
//调用存储过程
DataTable myDataTable = mySQL.BatchGetDB("Tag_Page_Name_Select", myNamenValue, "Count");
Tag.DateCount = (int)mySQL.OutputCommand.Parameters["@Count"].Value;
Tag.Pagination();

HeadPage.InnerHtml = FootPage.InnerHtml = Tag.GetPageHtml;

for (int i = 0, j = myDataTable.Rows.Count; i < j; i++)
{
Tag.TagTable(new ExDataRow(myDataTable.Rows[i]));
}
TagBox.InnerHtml = Tag.GetHtml;
(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片