首页 上一页 下一页 尾页 << 1 2 3 4 5 >>
不太想用AspNetPager,大家有自己写的不错的吗,求教?
30 个解决方案
#1
AspNetPager很不错`~
当然也有自己的,不过是公司代码,所以~~不能外传
当然也有自己的,不过是公司代码,所以~~不能外传
#2
欢迎大家来讨论啊 顶的有分
#3
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace myControler
{
/// <summary>
/// Page 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Page runat=server></{0}:Page>")]
public class Page : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
{
public event EventHandler ChangePageClick;
private int _TotalRecord =100;
private int _ShowPage = 5;
private int _PageSize =20;
private int _CurrentPageIndex = 1;
private int _PageCount = 0;
private string FontColor="blue";
private string CurrentColor="red";
private string Link="<a href=\"javascript:{0}\"><font color='{2}'>[{1}]</font></a>";
private string Go=" <INPUT type='text' id={0} onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" size=\"1\" value={2}><INPUT type=button value='跳转'onclick=javascript:__doPostBack('{1}',document.getElementById(\"{0}\").value)>";
private string LinkDiseble="<a href=\"#\"><font color='{1}'>[{0}]</font></a>";
private string NoRecord="共0条记录";
private bool isgeneric=true;
#region Properties
[
Description("总记录数"),
Bindable(false),
Category("Behavior"),
DefaultValue(0)
]
public bool IsGeneric
{
get{return isgeneric;}
set{isgeneric=value;}
}
public int TotalRecord
{
get{return _TotalRecord;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_TotalRecord = 0;
break;
}
}
_TotalRecord = value;
}
}
[
Description("每页显示记录数"),
Bindable(true),
Category("Behavior"),
DefaultValue(0)
]
public int PageSize
{
get{return _PageSize;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_PageSize = 0;
break;
}
}
_PageSize = value;
}
}
public int ShowPage
{
get{return this._ShowPage;}
set{this._ShowPage=value;}
}
[
Description("当前页值"),
Bindable(true),
Category("Behavior"),
DefaultValue(1)
]
public int CurrentPageIndex
{
get{return _CurrentPageIndex;}
set{_CurrentPageIndex = value;}
}
#endregion
protected virtual void OnPageChangeClick(EventArgs e)
{
if (ChangePageClick != null)
{
ChangePageClick(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
int PageIndex=1;
if(eventArgument.Trim()!=string.Empty)
PageIndex = int.Parse(eventArgument);
this._CurrentPageIndex = PageIndex;
System.Web.HttpContext.Current.Session["page"]=PageIndex;
OnPageChangeClick(new EventArgs());
}
protected override void RenderContents(HtmlTextWriter output)
{
_PageCount=Convert.ToInt32(Math.Ceiling(this.TotalRecord/Convert.ToDouble(this.PageSize)));
string PageListStr=string.Empty;
if(_PageCount>0)
{
int PrePage=this._CurrentPageIndex-this._ShowPage/2;
if(PrePage<1)
PrePage=1;
int NextPage=PrePage+this._ShowPage;
if(NextPage>_PageCount)
{
NextPage=_PageCount;
PrePage=NextPage-this._ShowPage;
if(PrePage<1)
PrePage=1;
}
if(isgeneric)
{
PageListStr=string.Format(Link,Page.GetPostBackClientEvent(this,"1"),"首页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,PrePage.ToString()),"上一页",FontColor);
PageListStr+=NumPage(this._ShowPage);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,NextPage.ToString()),"下一页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,this._PageCount.ToString()),"尾页",FontColor);
PageListStr+=string.Format("<font color='{1}'> 每页{2}条 共{0}条记录</font>",this.TotalRecord.ToString(),FontColor,this.PageSize.ToString());
}
else
{
PageListStr=string.Format("第 {0} 页",NumPage(_PageCount));
}
}
else
{
if(isgeneric)
PageListStr=NoRecord;
}
PageListStr+=string.Format(this.Go,this.ClientID+"_text",this.ClientID,this.CurrentPageIndex);
output.Write(PageListStr);
base.RenderContents(output);
}
public string NumPage(int Total)
{
int StartPage=1;
int EndPage=this._PageCount<this._ShowPage?this._PageCount:this._ShowPage;
if(this._PageCount>this._ShowPage)
{
if(this._CurrentPageIndex+this._ShowPage/2>this._PageCount)
{
EndPage=this._PageCount;
StartPage=EndPage-this._ShowPage+1;
}
else
{
if(this._CurrentPageIndex-this._ShowPage/2<=0)
{
StartPage=1;
EndPage=StartPage+this._ShowPage-1;
}
else
{
StartPage=this._CurrentPageIndex-this._ShowPage/2;
EndPage=StartPage+this._ShowPage-1;
}
}
}
string PageListTemp=string.Empty;
for(int i=StartPage;i<=EndPage;i++)
{
if(this._CurrentPageIndex==i)
PageListTemp+=string.Format(LinkDiseble,i.ToString(),CurrentColor);
else
{
if(isgeneric)
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),FontColor);
else
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),"white");
}
}
return PageListTemp;
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace myControler
{
/// <summary>
/// Page 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Page runat=server></{0}:Page>")]
public class Page : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
{
public event EventHandler ChangePageClick;
private int _TotalRecord =100;
private int _ShowPage = 5;
private int _PageSize =20;
private int _CurrentPageIndex = 1;
private int _PageCount = 0;
private string FontColor="blue";
private string CurrentColor="red";
private string Link="<a href=\"javascript:{0}\"><font color='{2}'>[{1}]</font></a>";
private string Go=" <INPUT type='text' id={0} onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" size=\"1\" value={2}><INPUT type=button value='跳转'onclick=javascript:__doPostBack('{1}',document.getElementById(\"{0}\").value)>";
private string LinkDiseble="<a href=\"#\"><font color='{1}'>[{0}]</font></a>";
private string NoRecord="共0条记录";
private bool isgeneric=true;
#region Properties
[
Description("总记录数"),
Bindable(false),
Category("Behavior"),
DefaultValue(0)
]
public bool IsGeneric
{
get{return isgeneric;}
set{isgeneric=value;}
}
public int TotalRecord
{
get{return _TotalRecord;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_TotalRecord = 0;
break;
}
}
_TotalRecord = value;
}
}
[
Description("每页显示记录数"),
Bindable(true),
Category("Behavior"),
DefaultValue(0)
]
public int PageSize
{
get{return _PageSize;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_PageSize = 0;
break;
}
}
_PageSize = value;
}
}
public int ShowPage
{
get{return this._ShowPage;}
set{this._ShowPage=value;}
}
[
Description("当前页值"),
Bindable(true),
Category("Behavior"),
DefaultValue(1)
]
public int CurrentPageIndex
{
get{return _CurrentPageIndex;}
set{_CurrentPageIndex = value;}
}
#endregion
protected virtual void OnPageChangeClick(EventArgs e)
{
if (ChangePageClick != null)
{
ChangePageClick(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
int PageIndex=1;
if(eventArgument.Trim()!=string.Empty)
PageIndex = int.Parse(eventArgument);
this._CurrentPageIndex = PageIndex;
System.Web.HttpContext.Current.Session["page"]=PageIndex;
OnPageChangeClick(new EventArgs());
}
protected override void RenderContents(HtmlTextWriter output)
{
_PageCount=Convert.ToInt32(Math.Ceiling(this.TotalRecord/Convert.ToDouble(this.PageSize)));
string PageListStr=string.Empty;
if(_PageCount>0)
{
int PrePage=this._CurrentPageIndex-this._ShowPage/2;
if(PrePage<1)
PrePage=1;
int NextPage=PrePage+this._ShowPage;
if(NextPage>_PageCount)
{
NextPage=_PageCount;
PrePage=NextPage-this._ShowPage;
if(PrePage<1)
PrePage=1;
}
if(isgeneric)
{
PageListStr=string.Format(Link,Page.GetPostBackClientEvent(this,"1"),"首页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,PrePage.ToString()),"上一页",FontColor);
PageListStr+=NumPage(this._ShowPage);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,NextPage.ToString()),"下一页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,this._PageCount.ToString()),"尾页",FontColor);
PageListStr+=string.Format("<font color='{1}'> 每页{2}条 共{0}条记录</font>",this.TotalRecord.ToString(),FontColor,this.PageSize.ToString());
}
else
{
PageListStr=string.Format("第 {0} 页",NumPage(_PageCount));
}
}
else
{
if(isgeneric)
PageListStr=NoRecord;
}
PageListStr+=string.Format(this.Go,this.ClientID+"_text",this.ClientID,this.CurrentPageIndex);
output.Write(PageListStr);
base.RenderContents(output);
}
public string NumPage(int Total)
{
int StartPage=1;
int EndPage=this._PageCount<this._ShowPage?this._PageCount:this._ShowPage;
if(this._PageCount>this._ShowPage)
{
if(this._CurrentPageIndex+this._ShowPage/2>this._PageCount)
{
EndPage=this._PageCount;
StartPage=EndPage-this._ShowPage+1;
}
else
{
if(this._CurrentPageIndex-this._ShowPage/2<=0)
{
StartPage=1;
EndPage=StartPage+this._ShowPage-1;
}
else
{
StartPage=this._CurrentPageIndex-this._ShowPage/2;
EndPage=StartPage+this._ShowPage-1;
}
}
}
string PageListTemp=string.Empty;
for(int i=StartPage;i<=EndPage;i++)
{
if(this._CurrentPageIndex==i)
PageListTemp+=string.Format(LinkDiseble,i.ToString(),CurrentColor);
else
{
if(isgeneric)
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),FontColor);
else
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),"white");
}
}
return PageListTemp;
}
}
}
#4
用aspnetpager www.webdiy.com up~jf~
#5
自己摸索做下吧
#6
呵呵,自己写的没有aspnetpager 好啊,就不献丑了(因为里面还有bug!)
#7
ding
#8
首页 上一页 下一页 尾页 << 1 2 3 4 5 >>
自己通过select top 10 * from table where id not in(....)
自己通过select top 10 * from table where id not in(....)
#9
http://www.cnblogs.com/ami/archive/2006/07/20/455693.html
#10
最好自己写,别人写的不一定能满足你的要求。比如你的表的主键是一个字段还是多个字段?主键中是否有需要转换的字段?当然如果用not in方式要简单些,但如果数据量很大时效率会低一些。
#11
推荐还是用ASPNETPAGEER, 性能方面与代码安全方面都要比一般写的高点..(个人意见)..我指的是数据的读取过程..用not in 与 not exists 都不如存储过程中的临时表的直接定位..
三楼的大哥很强..学习ING~
三楼的大哥很强..学习ING~
#12
http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/PagerControls.asp
#13
幫頂
結帖時接點分...
結帖時接點分...
#14
lz也感觉到了 AspNetPager 不太方便了吗?
恩,好。我就一直用我自己的。
这里有个演示页面。sc.1380000.com
恩,好。我就一直用我自己的。
这里有个演示页面。sc.1380000.com
#15
http://www.Webdiyer.com
#16
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class StaffMan : System.Web.UI.Page
{
int onpage, TotalPage;
protected void Page_Load(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
if (onpage == 0)
{
onpage = 1;
}
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
const int PageSizeBig = 14;//每页的大小;
Conn VideoData = new Conn();
PageSize VideoSize = new PageSize();
GridView1.DataSource = VideoData.ConnEnt(VideoSize.PageSizeMain(onpage, PageSizeBig, "X_Staff", "", "", "Staff_ID", out TotalPage));
GridView1.DataBind();
VideoData.CloseAll();
Label1.Text = PageSizeBig.ToString().Trim()+"条记录/页 第" + onpage.ToString().Trim() + "页";
Label2.Text = "共" + TotalPage.ToString().Trim() + "页 跳转到第";
DropDownList1.Items.Clear();
for (int i = 1; i <= TotalPage; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = onpage - 1;
if (onpage == TotalPage && TotalPage == 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
else if (onpage == 1 && TotalPage != 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage != TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage == TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
if (TotalPage == 0)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
onpage = 1;
BindData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
onpage = onpage - 1;
BindData();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
onpage = onpage + 1;
BindData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
onpage = DropDownList1.Items.Count;
BindData();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue);
BindData();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class StaffMan : System.Web.UI.Page
{
int onpage, TotalPage;
protected void Page_Load(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
if (onpage == 0)
{
onpage = 1;
}
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
const int PageSizeBig = 14;//每页的大小;
Conn VideoData = new Conn();
PageSize VideoSize = new PageSize();
GridView1.DataSource = VideoData.ConnEnt(VideoSize.PageSizeMain(onpage, PageSizeBig, "X_Staff", "", "", "Staff_ID", out TotalPage));
GridView1.DataBind();
VideoData.CloseAll();
Label1.Text = PageSizeBig.ToString().Trim()+"条记录/页 第" + onpage.ToString().Trim() + "页";
Label2.Text = "共" + TotalPage.ToString().Trim() + "页 跳转到第";
DropDownList1.Items.Clear();
for (int i = 1; i <= TotalPage; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = onpage - 1;
if (onpage == TotalPage && TotalPage == 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
else if (onpage == 1 && TotalPage != 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage != TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage == TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
if (TotalPage == 0)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
onpage = 1;
BindData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
onpage = onpage - 1;
BindData();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
onpage = onpage + 1;
BindData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
onpage = DropDownList1.Items.Count;
BindData();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue);
BindData();
}
}
#17
PageSize类
public class PageSize
{
/// <summary>
/// ACCESS分页程序
/// </summary>
/// <param name="onpage">当前页数</param>
/// <param name="page">每页多少条记录</param>
/// <param name="datename">数据库名</param>
/// <param name="wheresql">需要过滤的条件</param>
/// <param name="orderby">需要按什么来进行按序</param>
/// <param name="datebaseID">数据库中的ID字段名</param>
/// <param name="LinkPageSize">输入输出函数</param>
/// <returns>返回SQL查询语句</returns>
public string PageSizeMain(int onpage, int page, string datename, string wheresql, string orderby, string datebaseID, out int TotalSize)
{
string TargetSql;//返回SQL字符串
int recordtotal;//总的记录数
bool isrepage;
int totalpage;//总的页数
if (wheresql.Trim() == "")//计算总的记录数
{
TargetSql = "select count(*) as countcord from " + datename;
}
else
{
TargetSql = "select count(*) as countcord from " + datename + " where " + wheresql;
}
Conn CountPage = new Conn();
OleDbDataReader rec = CountPage.ConnEnt(TargetSql);
rec.Read();
recordtotal = Convert.ToInt32(rec[0]);
rec.Close();
CountPage.CloseAll();
isrepage = true;
if (recordtotal % page == 0)//**计算总的页数
{
totalpage = recordtotal / page;
}
else
{
totalpage = recordtotal / page + 1;
isrepage = false;
}
TotalSize = totalpage;
int endpage;
if (onpage == totalpage && totalpage > 1 && isrepage == false)//判断是不是最后一页
{
page = recordtotal % ((onpage - 1) * page);
endpage = recordtotal;
}
else
{
endpage = page * onpage;
}
if (wheresql.Trim() == "")
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
else
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
return (TargetSql);
}
}
public class PageSize
{
/// <summary>
/// ACCESS分页程序
/// </summary>
/// <param name="onpage">当前页数</param>
/// <param name="page">每页多少条记录</param>
/// <param name="datename">数据库名</param>
/// <param name="wheresql">需要过滤的条件</param>
/// <param name="orderby">需要按什么来进行按序</param>
/// <param name="datebaseID">数据库中的ID字段名</param>
/// <param name="LinkPageSize">输入输出函数</param>
/// <returns>返回SQL查询语句</returns>
public string PageSizeMain(int onpage, int page, string datename, string wheresql, string orderby, string datebaseID, out int TotalSize)
{
string TargetSql;//返回SQL字符串
int recordtotal;//总的记录数
bool isrepage;
int totalpage;//总的页数
if (wheresql.Trim() == "")//计算总的记录数
{
TargetSql = "select count(*) as countcord from " + datename;
}
else
{
TargetSql = "select count(*) as countcord from " + datename + " where " + wheresql;
}
Conn CountPage = new Conn();
OleDbDataReader rec = CountPage.ConnEnt(TargetSql);
rec.Read();
recordtotal = Convert.ToInt32(rec[0]);
rec.Close();
CountPage.CloseAll();
isrepage = true;
if (recordtotal % page == 0)//**计算总的页数
{
totalpage = recordtotal / page;
}
else
{
totalpage = recordtotal / page + 1;
isrepage = false;
}
TotalSize = totalpage;
int endpage;
if (onpage == totalpage && totalpage > 1 && isrepage == false)//判断是不是最后一页
{
page = recordtotal % ((onpage - 1) * page);
endpage = recordtotal;
}
else
{
endpage = page * onpage;
}
if (wheresql.Trim() == "")
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
else
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
return (TargetSql);
}
}
#18
mark
#19
http://www.cnblogs.com/halgogo/archive/2005/05/27/163777.aspx
sql写的分页有问题
应该是
(this.LzhPages1.CurrentPage-1) * this.LzhPages1.Count
看完学习和使用
你就能掌握了
关键给了注释帮助理解
sql写的分页有问题
应该是
(this.LzhPages1.CurrentPage-1) * this.LzhPages1.Count
看完学习和使用
你就能掌握了
关键给了注释帮助理解
#20
hh
#21
作个标记
#22
作个标记
#23
作个标记
#24
作个标记
#25
mark
#26
越简单往往越是最好的。
用存储过程,先取如第 x 条记录的ID,比如是 2000,然后select top 10 …… where ……
我自己的测试:
每页 10 条记录 1W页 - 8W页
用时:78 - 625 毫秒之间,递增 50 毫秒
用存储过程,先取如第 x 条记录的ID,比如是 2000,然后select top 10 …… where ……
我自己的测试:
每页 10 条记录 1W页 - 8W页
用时:78 - 625 毫秒之间,递增 50 毫秒
#27
这里有个演示页面。http://sc.1380000.com
#28
Test!
#29
做个标记,回家试试看
#30
学习中。
#1
AspNetPager很不错`~
当然也有自己的,不过是公司代码,所以~~不能外传
当然也有自己的,不过是公司代码,所以~~不能外传
#2
欢迎大家来讨论啊 顶的有分
#3
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace myControler
{
/// <summary>
/// Page 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Page runat=server></{0}:Page>")]
public class Page : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
{
public event EventHandler ChangePageClick;
private int _TotalRecord =100;
private int _ShowPage = 5;
private int _PageSize =20;
private int _CurrentPageIndex = 1;
private int _PageCount = 0;
private string FontColor="blue";
private string CurrentColor="red";
private string Link="<a href=\"javascript:{0}\"><font color='{2}'>[{1}]</font></a>";
private string Go=" <INPUT type='text' id={0} onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" size=\"1\" value={2}><INPUT type=button value='跳转'onclick=javascript:__doPostBack('{1}',document.getElementById(\"{0}\").value)>";
private string LinkDiseble="<a href=\"#\"><font color='{1}'>[{0}]</font></a>";
private string NoRecord="共0条记录";
private bool isgeneric=true;
#region Properties
[
Description("总记录数"),
Bindable(false),
Category("Behavior"),
DefaultValue(0)
]
public bool IsGeneric
{
get{return isgeneric;}
set{isgeneric=value;}
}
public int TotalRecord
{
get{return _TotalRecord;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_TotalRecord = 0;
break;
}
}
_TotalRecord = value;
}
}
[
Description("每页显示记录数"),
Bindable(true),
Category("Behavior"),
DefaultValue(0)
]
public int PageSize
{
get{return _PageSize;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_PageSize = 0;
break;
}
}
_PageSize = value;
}
}
public int ShowPage
{
get{return this._ShowPage;}
set{this._ShowPage=value;}
}
[
Description("当前页值"),
Bindable(true),
Category("Behavior"),
DefaultValue(1)
]
public int CurrentPageIndex
{
get{return _CurrentPageIndex;}
set{_CurrentPageIndex = value;}
}
#endregion
protected virtual void OnPageChangeClick(EventArgs e)
{
if (ChangePageClick != null)
{
ChangePageClick(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
int PageIndex=1;
if(eventArgument.Trim()!=string.Empty)
PageIndex = int.Parse(eventArgument);
this._CurrentPageIndex = PageIndex;
System.Web.HttpContext.Current.Session["page"]=PageIndex;
OnPageChangeClick(new EventArgs());
}
protected override void RenderContents(HtmlTextWriter output)
{
_PageCount=Convert.ToInt32(Math.Ceiling(this.TotalRecord/Convert.ToDouble(this.PageSize)));
string PageListStr=string.Empty;
if(_PageCount>0)
{
int PrePage=this._CurrentPageIndex-this._ShowPage/2;
if(PrePage<1)
PrePage=1;
int NextPage=PrePage+this._ShowPage;
if(NextPage>_PageCount)
{
NextPage=_PageCount;
PrePage=NextPage-this._ShowPage;
if(PrePage<1)
PrePage=1;
}
if(isgeneric)
{
PageListStr=string.Format(Link,Page.GetPostBackClientEvent(this,"1"),"首页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,PrePage.ToString()),"上一页",FontColor);
PageListStr+=NumPage(this._ShowPage);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,NextPage.ToString()),"下一页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,this._PageCount.ToString()),"尾页",FontColor);
PageListStr+=string.Format("<font color='{1}'> 每页{2}条 共{0}条记录</font>",this.TotalRecord.ToString(),FontColor,this.PageSize.ToString());
}
else
{
PageListStr=string.Format("第 {0} 页",NumPage(_PageCount));
}
}
else
{
if(isgeneric)
PageListStr=NoRecord;
}
PageListStr+=string.Format(this.Go,this.ClientID+"_text",this.ClientID,this.CurrentPageIndex);
output.Write(PageListStr);
base.RenderContents(output);
}
public string NumPage(int Total)
{
int StartPage=1;
int EndPage=this._PageCount<this._ShowPage?this._PageCount:this._ShowPage;
if(this._PageCount>this._ShowPage)
{
if(this._CurrentPageIndex+this._ShowPage/2>this._PageCount)
{
EndPage=this._PageCount;
StartPage=EndPage-this._ShowPage+1;
}
else
{
if(this._CurrentPageIndex-this._ShowPage/2<=0)
{
StartPage=1;
EndPage=StartPage+this._ShowPage-1;
}
else
{
StartPage=this._CurrentPageIndex-this._ShowPage/2;
EndPage=StartPage+this._ShowPage-1;
}
}
}
string PageListTemp=string.Empty;
for(int i=StartPage;i<=EndPage;i++)
{
if(this._CurrentPageIndex==i)
PageListTemp+=string.Format(LinkDiseble,i.ToString(),CurrentColor);
else
{
if(isgeneric)
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),FontColor);
else
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),"white");
}
}
return PageListTemp;
}
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace myControler
{
/// <summary>
/// Page 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:Page runat=server></{0}:Page>")]
public class Page : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
{
public event EventHandler ChangePageClick;
private int _TotalRecord =100;
private int _ShowPage = 5;
private int _PageSize =20;
private int _CurrentPageIndex = 1;
private int _PageCount = 0;
private string FontColor="blue";
private string CurrentColor="red";
private string Link="<a href=\"javascript:{0}\"><font color='{2}'>[{1}]</font></a>";
private string Go=" <INPUT type='text' id={0} onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\" size=\"1\" value={2}><INPUT type=button value='跳转'onclick=javascript:__doPostBack('{1}',document.getElementById(\"{0}\").value)>";
private string LinkDiseble="<a href=\"#\"><font color='{1}'>[{0}]</font></a>";
private string NoRecord="共0条记录";
private bool isgeneric=true;
#region Properties
[
Description("总记录数"),
Bindable(false),
Category("Behavior"),
DefaultValue(0)
]
public bool IsGeneric
{
get{return isgeneric;}
set{isgeneric=value;}
}
public int TotalRecord
{
get{return _TotalRecord;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_TotalRecord = 0;
break;
}
}
_TotalRecord = value;
}
}
[
Description("每页显示记录数"),
Bindable(true),
Category("Behavior"),
DefaultValue(0)
]
public int PageSize
{
get{return _PageSize;}
set
{
foreach(char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_PageSize = 0;
break;
}
}
_PageSize = value;
}
}
public int ShowPage
{
get{return this._ShowPage;}
set{this._ShowPage=value;}
}
[
Description("当前页值"),
Bindable(true),
Category("Behavior"),
DefaultValue(1)
]
public int CurrentPageIndex
{
get{return _CurrentPageIndex;}
set{_CurrentPageIndex = value;}
}
#endregion
protected virtual void OnPageChangeClick(EventArgs e)
{
if (ChangePageClick != null)
{
ChangePageClick(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
int PageIndex=1;
if(eventArgument.Trim()!=string.Empty)
PageIndex = int.Parse(eventArgument);
this._CurrentPageIndex = PageIndex;
System.Web.HttpContext.Current.Session["page"]=PageIndex;
OnPageChangeClick(new EventArgs());
}
protected override void RenderContents(HtmlTextWriter output)
{
_PageCount=Convert.ToInt32(Math.Ceiling(this.TotalRecord/Convert.ToDouble(this.PageSize)));
string PageListStr=string.Empty;
if(_PageCount>0)
{
int PrePage=this._CurrentPageIndex-this._ShowPage/2;
if(PrePage<1)
PrePage=1;
int NextPage=PrePage+this._ShowPage;
if(NextPage>_PageCount)
{
NextPage=_PageCount;
PrePage=NextPage-this._ShowPage;
if(PrePage<1)
PrePage=1;
}
if(isgeneric)
{
PageListStr=string.Format(Link,Page.GetPostBackClientEvent(this,"1"),"首页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,PrePage.ToString()),"上一页",FontColor);
PageListStr+=NumPage(this._ShowPage);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,NextPage.ToString()),"下一页",FontColor);
PageListStr+=string.Format(Link,Page.GetPostBackClientEvent(this,this._PageCount.ToString()),"尾页",FontColor);
PageListStr+=string.Format("<font color='{1}'> 每页{2}条 共{0}条记录</font>",this.TotalRecord.ToString(),FontColor,this.PageSize.ToString());
}
else
{
PageListStr=string.Format("第 {0} 页",NumPage(_PageCount));
}
}
else
{
if(isgeneric)
PageListStr=NoRecord;
}
PageListStr+=string.Format(this.Go,this.ClientID+"_text",this.ClientID,this.CurrentPageIndex);
output.Write(PageListStr);
base.RenderContents(output);
}
public string NumPage(int Total)
{
int StartPage=1;
int EndPage=this._PageCount<this._ShowPage?this._PageCount:this._ShowPage;
if(this._PageCount>this._ShowPage)
{
if(this._CurrentPageIndex+this._ShowPage/2>this._PageCount)
{
EndPage=this._PageCount;
StartPage=EndPage-this._ShowPage+1;
}
else
{
if(this._CurrentPageIndex-this._ShowPage/2<=0)
{
StartPage=1;
EndPage=StartPage+this._ShowPage-1;
}
else
{
StartPage=this._CurrentPageIndex-this._ShowPage/2;
EndPage=StartPage+this._ShowPage-1;
}
}
}
string PageListTemp=string.Empty;
for(int i=StartPage;i<=EndPage;i++)
{
if(this._CurrentPageIndex==i)
PageListTemp+=string.Format(LinkDiseble,i.ToString(),CurrentColor);
else
{
if(isgeneric)
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),FontColor);
else
PageListTemp+=string.Format(Link,Page.GetPostBackClientEvent(this,i.ToString()),i.ToString(),"white");
}
}
return PageListTemp;
}
}
}
#4
用aspnetpager www.webdiy.com up~jf~
#5
自己摸索做下吧
#6
呵呵,自己写的没有aspnetpager 好啊,就不献丑了(因为里面还有bug!)
#7
ding
#8
首页 上一页 下一页 尾页 << 1 2 3 4 5 >>
自己通过select top 10 * from table where id not in(....)
自己通过select top 10 * from table where id not in(....)
#9
http://www.cnblogs.com/ami/archive/2006/07/20/455693.html
#10
最好自己写,别人写的不一定能满足你的要求。比如你的表的主键是一个字段还是多个字段?主键中是否有需要转换的字段?当然如果用not in方式要简单些,但如果数据量很大时效率会低一些。
#11
推荐还是用ASPNETPAGEER, 性能方面与代码安全方面都要比一般写的高点..(个人意见)..我指的是数据的读取过程..用not in 与 not exists 都不如存储过程中的临时表的直接定位..
三楼的大哥很强..学习ING~
三楼的大哥很强..学习ING~
#12
http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/PagerControls.asp
#13
幫頂
結帖時接點分...
結帖時接點分...
#14
lz也感觉到了 AspNetPager 不太方便了吗?
恩,好。我就一直用我自己的。
这里有个演示页面。sc.1380000.com
恩,好。我就一直用我自己的。
这里有个演示页面。sc.1380000.com
#15
http://www.Webdiyer.com
#16
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class StaffMan : System.Web.UI.Page
{
int onpage, TotalPage;
protected void Page_Load(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
if (onpage == 0)
{
onpage = 1;
}
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
const int PageSizeBig = 14;//每页的大小;
Conn VideoData = new Conn();
PageSize VideoSize = new PageSize();
GridView1.DataSource = VideoData.ConnEnt(VideoSize.PageSizeMain(onpage, PageSizeBig, "X_Staff", "", "", "Staff_ID", out TotalPage));
GridView1.DataBind();
VideoData.CloseAll();
Label1.Text = PageSizeBig.ToString().Trim()+"条记录/页 第" + onpage.ToString().Trim() + "页";
Label2.Text = "共" + TotalPage.ToString().Trim() + "页 跳转到第";
DropDownList1.Items.Clear();
for (int i = 1; i <= TotalPage; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = onpage - 1;
if (onpage == TotalPage && TotalPage == 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
else if (onpage == 1 && TotalPage != 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage != TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage == TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
if (TotalPage == 0)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
onpage = 1;
BindData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
onpage = onpage - 1;
BindData();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
onpage = onpage + 1;
BindData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
onpage = DropDownList1.Items.Count;
BindData();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue);
BindData();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class StaffMan : System.Web.UI.Page
{
int onpage, TotalPage;
protected void Page_Load(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
if (onpage == 0)
{
onpage = 1;
}
if (!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
const int PageSizeBig = 14;//每页的大小;
Conn VideoData = new Conn();
PageSize VideoSize = new PageSize();
GridView1.DataSource = VideoData.ConnEnt(VideoSize.PageSizeMain(onpage, PageSizeBig, "X_Staff", "", "", "Staff_ID", out TotalPage));
GridView1.DataBind();
VideoData.CloseAll();
Label1.Text = PageSizeBig.ToString().Trim()+"条记录/页 第" + onpage.ToString().Trim() + "页";
Label2.Text = "共" + TotalPage.ToString().Trim() + "页 跳转到第";
DropDownList1.Items.Clear();
for (int i = 1; i <= TotalPage; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
DropDownList1.SelectedIndex = onpage - 1;
if (onpage == TotalPage && TotalPage == 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
else if (onpage == 1 && TotalPage != 1)
{
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage != TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
LinkButton5.Enabled = true;
}
else if (onpage == TotalPage)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
if (TotalPage == 0)
{
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = false;
LinkButton5.Enabled = false;
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
onpage = 1;
BindData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
onpage = onpage - 1;
BindData();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
onpage = onpage + 1;
BindData();
}
protected void LinkButton5_Click(object sender, EventArgs e)
{
onpage = DropDownList1.Items.Count;
BindData();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
onpage = Convert.ToInt32(DropDownList1.SelectedValue);
BindData();
}
}
#17
PageSize类
public class PageSize
{
/// <summary>
/// ACCESS分页程序
/// </summary>
/// <param name="onpage">当前页数</param>
/// <param name="page">每页多少条记录</param>
/// <param name="datename">数据库名</param>
/// <param name="wheresql">需要过滤的条件</param>
/// <param name="orderby">需要按什么来进行按序</param>
/// <param name="datebaseID">数据库中的ID字段名</param>
/// <param name="LinkPageSize">输入输出函数</param>
/// <returns>返回SQL查询语句</returns>
public string PageSizeMain(int onpage, int page, string datename, string wheresql, string orderby, string datebaseID, out int TotalSize)
{
string TargetSql;//返回SQL字符串
int recordtotal;//总的记录数
bool isrepage;
int totalpage;//总的页数
if (wheresql.Trim() == "")//计算总的记录数
{
TargetSql = "select count(*) as countcord from " + datename;
}
else
{
TargetSql = "select count(*) as countcord from " + datename + " where " + wheresql;
}
Conn CountPage = new Conn();
OleDbDataReader rec = CountPage.ConnEnt(TargetSql);
rec.Read();
recordtotal = Convert.ToInt32(rec[0]);
rec.Close();
CountPage.CloseAll();
isrepage = true;
if (recordtotal % page == 0)//**计算总的页数
{
totalpage = recordtotal / page;
}
else
{
totalpage = recordtotal / page + 1;
isrepage = false;
}
TotalSize = totalpage;
int endpage;
if (onpage == totalpage && totalpage > 1 && isrepage == false)//判断是不是最后一页
{
page = recordtotal % ((onpage - 1) * page);
endpage = recordtotal;
}
else
{
endpage = page * onpage;
}
if (wheresql.Trim() == "")
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
else
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
return (TargetSql);
}
}
public class PageSize
{
/// <summary>
/// ACCESS分页程序
/// </summary>
/// <param name="onpage">当前页数</param>
/// <param name="page">每页多少条记录</param>
/// <param name="datename">数据库名</param>
/// <param name="wheresql">需要过滤的条件</param>
/// <param name="orderby">需要按什么来进行按序</param>
/// <param name="datebaseID">数据库中的ID字段名</param>
/// <param name="LinkPageSize">输入输出函数</param>
/// <returns>返回SQL查询语句</returns>
public string PageSizeMain(int onpage, int page, string datename, string wheresql, string orderby, string datebaseID, out int TotalSize)
{
string TargetSql;//返回SQL字符串
int recordtotal;//总的记录数
bool isrepage;
int totalpage;//总的页数
if (wheresql.Trim() == "")//计算总的记录数
{
TargetSql = "select count(*) as countcord from " + datename;
}
else
{
TargetSql = "select count(*) as countcord from " + datename + " where " + wheresql;
}
Conn CountPage = new Conn();
OleDbDataReader rec = CountPage.ConnEnt(TargetSql);
rec.Read();
recordtotal = Convert.ToInt32(rec[0]);
rec.Close();
CountPage.CloseAll();
isrepage = true;
if (recordtotal % page == 0)//**计算总的页数
{
totalpage = recordtotal / page;
}
else
{
totalpage = recordtotal / page + 1;
isrepage = false;
}
TotalSize = totalpage;
int endpage;
if (onpage == totalpage && totalpage > 1 && isrepage == false)//判断是不是最后一页
{
page = recordtotal % ((onpage - 1) * page);
endpage = recordtotal;
}
else
{
endpage = page * onpage;
}
if (wheresql.Trim() == "")
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
else
{
if (orderby.Trim() == "")
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + datebaseID + " desc";
}
else
{
TargetSql = "select * from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(page) + " " + datebaseID + " from " + datename + " where " + datebaseID + " in (select top " + Convert.ToString(endpage) + " " + datebaseID + " from " + datename + " where " + wheresql + " order by " + datebaseID + " desc) order by " + datebaseID + ") order by " + orderby + "," + datebaseID + " desc";
}
}
return (TargetSql);
}
}
#18
mark
#19
http://www.cnblogs.com/halgogo/archive/2005/05/27/163777.aspx
sql写的分页有问题
应该是
(this.LzhPages1.CurrentPage-1) * this.LzhPages1.Count
看完学习和使用
你就能掌握了
关键给了注释帮助理解
sql写的分页有问题
应该是
(this.LzhPages1.CurrentPage-1) * this.LzhPages1.Count
看完学习和使用
你就能掌握了
关键给了注释帮助理解
#20
hh
#21
作个标记
#22
作个标记
#23
作个标记
#24
作个标记
#25
mark
#26
越简单往往越是最好的。
用存储过程,先取如第 x 条记录的ID,比如是 2000,然后select top 10 …… where ……
我自己的测试:
每页 10 条记录 1W页 - 8W页
用时:78 - 625 毫秒之间,递增 50 毫秒
用存储过程,先取如第 x 条记录的ID,比如是 2000,然后select top 10 …… where ……
我自己的测试:
每页 10 条记录 1W页 - 8W页
用时:78 - 625 毫秒之间,递增 50 毫秒
#27
这里有个演示页面。http://sc.1380000.com
#28
Test!
#29
做个标记,回家试试看
#30
学习中。