首页 1 2 3 4 5 下一页 这样也可以
用存储过程吗还是分页控件?具体怎么来。平时搞的都是 首页 上一页 下一页 未页 这种!
请各位给我一个比较可以用的列子,我为这个苦啊,帮帮,谢谢。
12 个解决方案
#1
回帖是一种美德
我的QQ:272828600
群:81439697
public void Bind()
{
PagedDataSource objPagedData = new PagedDataSource();
objPagedData.AllowPaging = true;
objPagedData.DataSource = ds.Table[0].DefaultView;
objPagedData.PageSize = 10;
lblCount.Text = objPagedData.PageCount.ToString();
int cur = Convert.ToInt32(lblCur.Text);
if (cur == 1)
{
lbUp.Enabled = false;
}
else
{
lbUp.Enabled = true;
}
if (cur == objPagedData.PageCount)
{
lbDwon.Enabled = false;
lbLast.Enabled = false;
}
else
{
lbDwon.Enabled = true;
lbLast.Enabled = true;
}
objPagedData.CurrentPageIndex = cur - 1;
GridView1.DataSource = objPagedData;
GridView1.DataBind();
}
这段代码是在绑定方法里面写
/// <summary>
/// 首页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbFirst_Click1(object sender, EventArgs e)
{
this.lblCur.Text = "1";
Bind();
}
/// <summary>
/// 上一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbUp_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count--;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 下一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbDwon_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count++;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 末页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbLast_Click1(object sender, EventArgs e)
{
this.lblCur.Text = lblCount.Text;
Bind();
}
这是前面按钮的点击事件
<TABLE style="WIDTH: 779px; HEIGHT: 5px; TEXT-ALIGN: center">
<TBODY><TR><TD style="HEIGHT: 17px" align=right>
<asp:LinkButton id="lbFirst" onclick="lbFirst_Click1" runat="server">首页</asp:LinkButton>
<asp:LinkButton id="lbUp" onclick="lbUp_Click1" runat="server">上一页</asp:LinkButton>
<asp:LinkButton id="lbDwon" onclick="lbDwon_Click1" runat="server">下一页</asp:LinkButton>
<asp:LinkButton id="lbLast" onclick="lbLast_Click1" runat="server">末页</asp:LinkButton>
共<asp:Label id="lblCount" runat="server" Text="1"></asp:Label>页 当前页数为第
<asp:Label id="lblCur" runat="server" Text="1"></asp:Label>页</TD></TR></TBODY></TABLE>
这是源页面的代码
我的QQ:272828600
群:81439697
public void Bind()
{
PagedDataSource objPagedData = new PagedDataSource();
objPagedData.AllowPaging = true;
objPagedData.DataSource = ds.Table[0].DefaultView;
objPagedData.PageSize = 10;
lblCount.Text = objPagedData.PageCount.ToString();
int cur = Convert.ToInt32(lblCur.Text);
if (cur == 1)
{
lbUp.Enabled = false;
}
else
{
lbUp.Enabled = true;
}
if (cur == objPagedData.PageCount)
{
lbDwon.Enabled = false;
lbLast.Enabled = false;
}
else
{
lbDwon.Enabled = true;
lbLast.Enabled = true;
}
objPagedData.CurrentPageIndex = cur - 1;
GridView1.DataSource = objPagedData;
GridView1.DataBind();
}
这段代码是在绑定方法里面写
/// <summary>
/// 首页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbFirst_Click1(object sender, EventArgs e)
{
this.lblCur.Text = "1";
Bind();
}
/// <summary>
/// 上一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbUp_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count--;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 下一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbDwon_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count++;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 末页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbLast_Click1(object sender, EventArgs e)
{
this.lblCur.Text = lblCount.Text;
Bind();
}
这是前面按钮的点击事件
<TABLE style="WIDTH: 779px; HEIGHT: 5px; TEXT-ALIGN: center">
<TBODY><TR><TD style="HEIGHT: 17px" align=right>
<asp:LinkButton id="lbFirst" onclick="lbFirst_Click1" runat="server">首页</asp:LinkButton>
<asp:LinkButton id="lbUp" onclick="lbUp_Click1" runat="server">上一页</asp:LinkButton>
<asp:LinkButton id="lbDwon" onclick="lbDwon_Click1" runat="server">下一页</asp:LinkButton>
<asp:LinkButton id="lbLast" onclick="lbLast_Click1" runat="server">末页</asp:LinkButton>
共<asp:Label id="lblCount" runat="server" Text="1"></asp:Label>页 当前页数为第
<asp:Label id="lblCur" runat="server" Text="1"></asp:Label>页</TD></TR></TBODY></TABLE>
这是源页面的代码
#2
要做到1234分页 ,datalist和repeater估计有点麻烦,我没试过,我是用gridview,girdview可以实现 首页 1 2 3 4 5 下一页 的分页效果,而且很好用,还有就是借助第三方控件也可以,比如aspnetpager就很好用,这个我也用过。
#3
1楼的大哥 你那么辛苦贴了 首页 上一页 下一页 末页 这种代码,哎,这个懂啊 就是不要这个 特意说明了哦,
要数字分页的效果呀。谢谢你。
要数字分页的效果呀。谢谢你。
#4
可以去下载aspnetpager.dll 挺好用的 然后放到网站或项目的bin目录下 就在你的工具箱里有aspnetpager这个控件了
后台:
protected void databind()
{
DataSet ds = DBHelper.GetAll(); //方法返回dataset或是dataTable
DataView dv = ds.Tables[0].DefaultView;
PagedDataSource pds = new PagedDataSource();
this.AspNetPager1.RecordCount = dv.Count;
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.DataList或Repeater.DataSource = pds;
this.DataList或Repeater.DataBind();
}
前台:
//这是分页控件
<webdiyer:aspnetpager style="FONT-SIZE: 12px" id="AspNetPager1" runat="server" CustomInfoClass="formbutton" SubmitButtonText="GO"
SubmitButtonClass="formfield" CssClass="formfield" PrevPageText="前一页" PageSize="3" NextPageText="下一页" LastPageText="尾页"
FirstPageText="首页" ShowInputBox="Always" CustomInfoTextAlign="Center" ShowNavigationToolTip="True"
CustomInfoHTML="第<font color='red'><b>%CurrentPageIndex%</b></font>页 共%PageCount% 页 %StartRecordIndex%-%EndRecordIndex%"
InputBoxStyle="width:19px" meta:resourceKey="AspNetPager1" width="506px" showcustominfosection="Left"
onpagechanged="AspNetPager1_PageChanged" horizontalalign="Center">
</webdiyer:aspnetpager>
后台:
protected void databind()
{
DataSet ds = DBHelper.GetAll(); //方法返回dataset或是dataTable
DataView dv = ds.Tables[0].DefaultView;
PagedDataSource pds = new PagedDataSource();
this.AspNetPager1.RecordCount = dv.Count;
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.DataList或Repeater.DataSource = pds;
this.DataList或Repeater.DataBind();
}
前台:
//这是分页控件
<webdiyer:aspnetpager style="FONT-SIZE: 12px" id="AspNetPager1" runat="server" CustomInfoClass="formbutton" SubmitButtonText="GO"
SubmitButtonClass="formfield" CssClass="formfield" PrevPageText="前一页" PageSize="3" NextPageText="下一页" LastPageText="尾页"
FirstPageText="首页" ShowInputBox="Always" CustomInfoTextAlign="Center" ShowNavigationToolTip="True"
CustomInfoHTML="第<font color='red'><b>%CurrentPageIndex%</b></font>页 共%PageCount% 页 %StartRecordIndex%-%EndRecordIndex%"
InputBoxStyle="width:19px" meta:resourceKey="AspNetPager1" width="506px" showcustominfosection="Left"
onpagechanged="AspNetPager1_PageChanged" horizontalalign="Center">
</webdiyer:aspnetpager>
#5
ASP.NET2.0中datalist仿百度分页
注意这个分页在1.0中无效
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;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.IO;
public partial class mysql : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]);
int ToatalCountRecord;//总记录数
int PageItem = 4;//每页显示的条数
int CurrentPage = 1;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (Request.QueryString["page"] != null)
{
if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage))
{
Response.Write("请输入分页参数!");
Response.End();
return;
}
}
this.BuidGrid();
}
}
private void BuidGrid()
{
string s2 = "select * from ts1";
SqlDataAdapter da = new SqlDataAdapter(s2, conn);
DataSet ds = new DataSet();
int startRecord = (CurrentPage - 1) * PageItem;
da.Fill(ds, startRecord, PageItem, "a");
this.DataList1.DataSource = ds.Tables["a"].DefaultView;
this.DataList1.DataBind();
SqlCommand comm = new SqlCommand("select count(*) from ts1", conn);
conn.Open();
ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
BuildPages();
}
private void BuildPages()
{
int Step = 5;//偏移量
int LeftNum = 0;//做界限
int RightNum = 0;//右界限
string PageUrl = Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (CurrentPage > 1)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + " </a>";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += " <font color=red>" + " " +"["+i.ToString() +"]"+ "" + " </font>";
}
else
{
OutPut += " <a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + " </a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + " </a>";
}
this.PageInfo.InnerHtml = OutPut;
}
}
请注意颜色标注部分
需要在前台添加一个 <div id="PageInfo" runat="server" >
#6
ASPX页代码
<asp:DataList ID="Data_p" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal" runat="server" DataKeyField="id" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
姓名: <%# Eval("name") %> <%# Eval("id") %> <br />
电话: <%# Eval("tel") %> <br />
</ItemTemplate>
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server" Visible="true" > </asp:PlaceHolder>
</FooterTemplate>
<EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" />
</asp:DataList>
C# code
ASPX.cs代码
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
db pdb = new db();
for (int i = 0; i < pdb.pgs().PageCount; i++)
{
HyperLink h=new HyperLink();
Literal l = new Literal();
int n = i + 1;
h.Text = n.ToString();
h.ID=n.ToString();
h.NavigateUrl = "datelist2.aspx?page=" + i.ToString();
l.Text =" ";
ph.Controls.Add(h);
ph.Controls.Add(l);
}
}
}
<asp:DataList ID="Data_p" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal" runat="server" DataKeyField="id" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
姓名: <%# Eval("name") %> <%# Eval("id") %> <br />
电话: <%# Eval("tel") %> <br />
</ItemTemplate>
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server" Visible="true" > </asp:PlaceHolder>
</FooterTemplate>
<EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" />
</asp:DataList>
C# code
ASPX.cs代码
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
db pdb = new db();
for (int i = 0; i < pdb.pgs().PageCount; i++)
{
HyperLink h=new HyperLink();
Literal l = new Literal();
int n = i + 1;
h.Text = n.ToString();
h.ID=n.ToString();
h.NavigateUrl = "datelist2.aspx?page=" + i.ToString();
l.Text =" ";
ph.Controls.Add(h);
ph.Controls.Add(l);
}
}
}
#8
这个嘛简单啊,哈哈,给你个类
using System;
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;
/// <summary>
/// PageNum 的摘要说明
/// </summary>
public class PageNum
{
public PageNum()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
///
/// </summary>
/// <param name="pageurl">传递你需要分页的地址,类似于"ContributeManage.aspx?state=" + state + "&isdel=" + isdel + "&isCommend=" + isCommend + "",如果没有参数则请加上1=1参数</param>
/// <param name="page">当前页码</param>
/// <param name="total">总的页数</param>
/// <param name="PageSize">每页显示的条数</param>
/// <returns>分页代码,类似于共4页 首页 上一页 1 2 3 4 下一页 末页</returns>
public static string CreatePage(string pageurl, int page, int total, int PageSize)
{
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (PageSize != 0)
{
allpage = (total / PageSize);
allpage = ((total % PageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + pageurl + "&page=1\">首页</a> <a href=\"" + pageurl + "&page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + pageurl + "&page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? " <a href=\"" + pageurl + "&page=" + next + "\">下一页</a> <a href=\"" + pageurl + "&page=" + allpage + "\">尾页</a>" : " 下一页 尾页";
pagestr += " 跳转到:<input id='txtPageNum'style=\"width:30px;\" type='text' size=3 value='" + page.ToString() + "' onkeyup=\"javascript:value=value.replace(/[^\\d]/g,'');if(this.value=='0'||this.value==''||parseInt(this.value)<0){this.value=1;}if(parseInt(this.value)>parseInt(" + allpage.ToString() + ")){this.value=" + allpage.ToString() + "}\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"><input type='button' value='转到' name='goToPage' onclick=\"javascript:window.location.href='" + pageurl + "&page='+document.getElementById('txtPageNum').value;\">";
return pagestr;
}
用这个类传几个参数就可以用了
对了,记住要用存储过程分页
using System;
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;
/// <summary>
/// PageNum 的摘要说明
/// </summary>
public class PageNum
{
public PageNum()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
///
/// </summary>
/// <param name="pageurl">传递你需要分页的地址,类似于"ContributeManage.aspx?state=" + state + "&isdel=" + isdel + "&isCommend=" + isCommend + "",如果没有参数则请加上1=1参数</param>
/// <param name="page">当前页码</param>
/// <param name="total">总的页数</param>
/// <param name="PageSize">每页显示的条数</param>
/// <returns>分页代码,类似于共4页 首页 上一页 1 2 3 4 下一页 末页</returns>
public static string CreatePage(string pageurl, int page, int total, int PageSize)
{
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (PageSize != 0)
{
allpage = (total / PageSize);
allpage = ((total % PageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + pageurl + "&page=1\">首页</a> <a href=\"" + pageurl + "&page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + pageurl + "&page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? " <a href=\"" + pageurl + "&page=" + next + "\">下一页</a> <a href=\"" + pageurl + "&page=" + allpage + "\">尾页</a>" : " 下一页 尾页";
pagestr += " 跳转到:<input id='txtPageNum'style=\"width:30px;\" type='text' size=3 value='" + page.ToString() + "' onkeyup=\"javascript:value=value.replace(/[^\\d]/g,'');if(this.value=='0'||this.value==''||parseInt(this.value)<0){this.value=1;}if(parseInt(this.value)>parseInt(" + allpage.ToString() + ")){this.value=" + allpage.ToString() + "}\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"><input type='button' value='转到' name='goToPage' onclick=\"javascript:window.location.href='" + pageurl + "&page='+document.getElementById('txtPageNum').value;\">";
return pagestr;
}
用这个类传几个参数就可以用了
对了,记住要用存储过程分页
#9
/**//*--实现分页的通用存储过程
显示指定表、视图、查询结果的第X页
对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法
如果视图或查询结果中有主键,不推荐此方法
如果使用查询语句,而且查询语句使用了order by,则查询语句必须包含top 语句
最后更新时间: 2008.01.20
--邹建 2003.09(引用请保留此信息)--*/
/**//*--调用示例
EXEC dbo.p_show
@QueryStr = N'tb',
@PageSize = 5,
@PageCurrent = 3,
@FdShow = 'id, colid, name',
@FdOrder = 'colid, name'
select id, colid from tb
order by colid, name
EXEC dbo.p_show
@QueryStr = N'
SELECT TOP 100 PERCENT
*
FROM dbo.sysobjects
ORDER BY xtype',
@PageSize = 5,
@PageCurrent = 2,
@FdShow = 'name, xtype',
@FdOrder = 'xtype, name'
--*/
CREATE PROC dbo.p_show
@QueryStr nvarchar(4000), -- 表名、视图名、查询语句
@PageSize int=10, -- 每页的大小(行数)
@PageCurrent int=1, -- 要显示的页
@FdShow nvarchar (4000) = N'', -- 要显示的字段列表,如果查询结果不需要标识字段,需要指定此值,且不包含标识字段
@FdOrder nvarchar (1000) = N'' -- 排序字段列表
AS
SET NOCOUNT ON
DECLARE
@FdName sysname, --表中的主键或表、临时表中的标识列名
@Id1 sysname, --开始和结束的记录号
@Id2 sysname,
@Obj_ID int --对象ID
--表中有复合主键的处理
DECLARE
@strfd nvarchar(2000), --复合主键列表
@strjoin nvarchar(4000), --连接字段
@strwhere nvarchar(2000) --查询条件
SELECT
@Obj_ID = OBJECT_ID(@QueryStr),
@FdShow = CASE
WHEN @FdShow > N'' THEN N' ' + @FdShow
ELSE N' *'
END,
@FdOrder = CASE
WHEN @FdOrder > N'' THEN N' ORDER BY ' + @FdOrder
ELSE N' '
END,
@QueryStr = CASE
WHEN @Obj_ID IS NULL THEN N' (' + @QueryStr + N')A'
ELSE N' ' + @QueryStr
END
-- 如果显示第一页,可以直接用 top 来完成
IF @PageCurrent = 1
BEGIN
SELECT
@Id1 = CAST(@PageSize as varchar(20))
EXEC(N'
SELECT TOP ' + @Id1 + N'
' + @FdShow + N'
FROM ' + @QueryStr + N'
' + @FdOrder
)
RETURN
END
-- 如果是表, 则检查表中是否有标识更或主键
IF @Obj_ID IS NULL OR OBJECTPROPERTY(@Obj_ID, 'IsTable') = 0
GOTO lb_usetemp
ELSE
BEGIN
SELECT
@Id1 = CAST(@PageSize as varchar(20)),
@Id2 = CAST((@PageCurrent - 1) * @PageSize as varchar(20))
-- 标识列
SELECT
@FdName = name
FROM dbo.syscolumns
WHERE id = @Obj_ID
AND status = 0x80
IF @@ROWCOUNT = 0 --如果表中无标识列,则检查表中是否有主键
BEGIN
DECLARE
@pk_number int
SELECT
@strfd = N'',
@strjoin = N'',
@strwhere = N''
SELECT
@strfd = @strfd
+ N',' + QUOTENAME(name),
@strjoin = @strjoin
+ N' AND A.' + QUOTENAME(name)
+ N'=B.' + QUOTENAME(name),
@strwhere = @strwhere
+ N' AND B.' + QUOTENAME(name) + N' IS NULL'
FROM(
SELECT
IX.id, IX.indid,
IXC.colid, ixc.keyno,
C.name
FROM dbo.sysobjects O,
dbo.sysindexes IX,
dbo.sysindexkeys IXC,
dbo.syscolumns C
WHERE O.parent_obj = @Obj_ID
AND O.xtype = 'PK'
AND O.name = IX.name
AND IX.id = @Obj_ID
AND IX.id = IXC.id
AND IX.indid = IXC.indid
AND IXC.id = C.id
AND IXC.colid = C.colid
)A
ORDER BY keyno
SELECT
@pk_number = @@ROWCOUNT,
@strfd = STUFF(@strfd, 1, 1, N''),
@strjoin = STUFF(@strjoin, 1, 5, N''),
@strwhere = STUFF(@strwhere, 1, 5, N'')
IF @pk_number = 0
GOTO lb_usetemp --如果表中无主键,则用临时表处理
ELSE IF @pk_number = 1
BEGIN
SELECT
@FdName = @strfd
GOTO lb_useidentity -- 使用单一主键
END
ELSE GOTO lb_usepk -- 使用复合主键
END
END
/**//*--使用标识列或主键为单一字段的处理方法--*/
lb_useidentity:
EXEC(N'
SELECT TOP ' + @Id1 + N'
' + @FdShow + N'
FROM '+@QueryStr + N'
WHERE ' + @FdName + ' NOT IN(
SELECT TOP ' + @Id2 + N'
' + @FdName + '
FROM ' + @QueryStr + N'
' + @FdOrder + N')
' + @FdOrder + N'
')
RETURN
/**//*--表中有复合主键的处理方法--*/
lb_usepk:
EXEC(N'
SELECT
' + @FdShow + N'
FROM(
SELECT TOP ' + @Id1 + N'
A.*
FROM ' + @QueryStr + N' A
LEFT JOIN(
SELECT TOP ' + @Id2 + N'
' + @strfd + N'
FROM ' + @QueryStr + N'
' + @FdOrder + N'
)B
ON ' + @strjoin + N'
WHERE ' + @strwhere + N'
' + @FdOrder + N'
)A
' + @FdOrder + N'
')
RETURN
/**//*--用临时表处理的方法--*/
lb_usetemp:
SELECT
@FdName = QUOTENAME(N'ID_' + CAST(NEWID() as varchar(40))),
@Id1 = CAST(@PageSize * (@PageCurrent-1) as varchar(20)),
@Id2 = CAST(@PageSize * @PageCurrent-1 as varchar(20))
EXEC(N'
SELECT
' + @FdName + N' = IDENTITY(int, 0, 1),
' + @FdShow + N'
INTO #tb
FROM(
SELECT TOP 100 PERCENT
*
FROM ' + @QueryStr + N'
' + @FdOrder + N'
)A
' + @FdOrder + N'
SELECT
' + @FdShow + N'
FROM #tb
WHERE ' + @FdName + ' BETWEEN ' + @Id1 + ' AND ' + @Id2 + N'
'
)
GO
还要写一个存储过程得到总条数这样就搞定,你还可以改一下然后可以控制样式
#10
安装5楼的方法做了,谢谢你哦,呵呵,成功了,不够那是刷新的,如果能像QQ评论搞成无刷新的数字分页那将更好,不过将就先用着哦,百度 google都用那样的刷新呢 呵呵
#11
如果想用不刷新的,你就得用到AJAX的了。那么就要得在服务器端写了
#12
sssssssss
#1
回帖是一种美德
我的QQ:272828600
群:81439697
public void Bind()
{
PagedDataSource objPagedData = new PagedDataSource();
objPagedData.AllowPaging = true;
objPagedData.DataSource = ds.Table[0].DefaultView;
objPagedData.PageSize = 10;
lblCount.Text = objPagedData.PageCount.ToString();
int cur = Convert.ToInt32(lblCur.Text);
if (cur == 1)
{
lbUp.Enabled = false;
}
else
{
lbUp.Enabled = true;
}
if (cur == objPagedData.PageCount)
{
lbDwon.Enabled = false;
lbLast.Enabled = false;
}
else
{
lbDwon.Enabled = true;
lbLast.Enabled = true;
}
objPagedData.CurrentPageIndex = cur - 1;
GridView1.DataSource = objPagedData;
GridView1.DataBind();
}
这段代码是在绑定方法里面写
/// <summary>
/// 首页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbFirst_Click1(object sender, EventArgs e)
{
this.lblCur.Text = "1";
Bind();
}
/// <summary>
/// 上一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbUp_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count--;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 下一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbDwon_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count++;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 末页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbLast_Click1(object sender, EventArgs e)
{
this.lblCur.Text = lblCount.Text;
Bind();
}
这是前面按钮的点击事件
<TABLE style="WIDTH: 779px; HEIGHT: 5px; TEXT-ALIGN: center">
<TBODY><TR><TD style="HEIGHT: 17px" align=right>
<asp:LinkButton id="lbFirst" onclick="lbFirst_Click1" runat="server">首页</asp:LinkButton>
<asp:LinkButton id="lbUp" onclick="lbUp_Click1" runat="server">上一页</asp:LinkButton>
<asp:LinkButton id="lbDwon" onclick="lbDwon_Click1" runat="server">下一页</asp:LinkButton>
<asp:LinkButton id="lbLast" onclick="lbLast_Click1" runat="server">末页</asp:LinkButton>
共<asp:Label id="lblCount" runat="server" Text="1"></asp:Label>页 当前页数为第
<asp:Label id="lblCur" runat="server" Text="1"></asp:Label>页</TD></TR></TBODY></TABLE>
这是源页面的代码
我的QQ:272828600
群:81439697
public void Bind()
{
PagedDataSource objPagedData = new PagedDataSource();
objPagedData.AllowPaging = true;
objPagedData.DataSource = ds.Table[0].DefaultView;
objPagedData.PageSize = 10;
lblCount.Text = objPagedData.PageCount.ToString();
int cur = Convert.ToInt32(lblCur.Text);
if (cur == 1)
{
lbUp.Enabled = false;
}
else
{
lbUp.Enabled = true;
}
if (cur == objPagedData.PageCount)
{
lbDwon.Enabled = false;
lbLast.Enabled = false;
}
else
{
lbDwon.Enabled = true;
lbLast.Enabled = true;
}
objPagedData.CurrentPageIndex = cur - 1;
GridView1.DataSource = objPagedData;
GridView1.DataBind();
}
这段代码是在绑定方法里面写
/// <summary>
/// 首页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbFirst_Click1(object sender, EventArgs e)
{
this.lblCur.Text = "1";
Bind();
}
/// <summary>
/// 上一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbUp_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count--;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 下一页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbDwon_Click1(object sender, EventArgs e)
{
int count = Convert.ToInt32(lblCur.Text.Trim());
count++;
this.lblCur.Text = count.ToString();
Bind();
}
/// <summary>
/// 末页点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbLast_Click1(object sender, EventArgs e)
{
this.lblCur.Text = lblCount.Text;
Bind();
}
这是前面按钮的点击事件
<TABLE style="WIDTH: 779px; HEIGHT: 5px; TEXT-ALIGN: center">
<TBODY><TR><TD style="HEIGHT: 17px" align=right>
<asp:LinkButton id="lbFirst" onclick="lbFirst_Click1" runat="server">首页</asp:LinkButton>
<asp:LinkButton id="lbUp" onclick="lbUp_Click1" runat="server">上一页</asp:LinkButton>
<asp:LinkButton id="lbDwon" onclick="lbDwon_Click1" runat="server">下一页</asp:LinkButton>
<asp:LinkButton id="lbLast" onclick="lbLast_Click1" runat="server">末页</asp:LinkButton>
共<asp:Label id="lblCount" runat="server" Text="1"></asp:Label>页 当前页数为第
<asp:Label id="lblCur" runat="server" Text="1"></asp:Label>页</TD></TR></TBODY></TABLE>
这是源页面的代码
#2
要做到1234分页 ,datalist和repeater估计有点麻烦,我没试过,我是用gridview,girdview可以实现 首页 1 2 3 4 5 下一页 的分页效果,而且很好用,还有就是借助第三方控件也可以,比如aspnetpager就很好用,这个我也用过。
#3
1楼的大哥 你那么辛苦贴了 首页 上一页 下一页 末页 这种代码,哎,这个懂啊 就是不要这个 特意说明了哦,
要数字分页的效果呀。谢谢你。
要数字分页的效果呀。谢谢你。
#4
可以去下载aspnetpager.dll 挺好用的 然后放到网站或项目的bin目录下 就在你的工具箱里有aspnetpager这个控件了
后台:
protected void databind()
{
DataSet ds = DBHelper.GetAll(); //方法返回dataset或是dataTable
DataView dv = ds.Tables[0].DefaultView;
PagedDataSource pds = new PagedDataSource();
this.AspNetPager1.RecordCount = dv.Count;
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.DataList或Repeater.DataSource = pds;
this.DataList或Repeater.DataBind();
}
前台:
//这是分页控件
<webdiyer:aspnetpager style="FONT-SIZE: 12px" id="AspNetPager1" runat="server" CustomInfoClass="formbutton" SubmitButtonText="GO"
SubmitButtonClass="formfield" CssClass="formfield" PrevPageText="前一页" PageSize="3" NextPageText="下一页" LastPageText="尾页"
FirstPageText="首页" ShowInputBox="Always" CustomInfoTextAlign="Center" ShowNavigationToolTip="True"
CustomInfoHTML="第<font color='red'><b>%CurrentPageIndex%</b></font>页 共%PageCount% 页 %StartRecordIndex%-%EndRecordIndex%"
InputBoxStyle="width:19px" meta:resourceKey="AspNetPager1" width="506px" showcustominfosection="Left"
onpagechanged="AspNetPager1_PageChanged" horizontalalign="Center">
</webdiyer:aspnetpager>
后台:
protected void databind()
{
DataSet ds = DBHelper.GetAll(); //方法返回dataset或是dataTable
DataView dv = ds.Tables[0].DefaultView;
PagedDataSource pds = new PagedDataSource();
this.AspNetPager1.RecordCount = dv.Count;
pds.DataSource = dv;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
this.DataList或Repeater.DataSource = pds;
this.DataList或Repeater.DataBind();
}
前台:
//这是分页控件
<webdiyer:aspnetpager style="FONT-SIZE: 12px" id="AspNetPager1" runat="server" CustomInfoClass="formbutton" SubmitButtonText="GO"
SubmitButtonClass="formfield" CssClass="formfield" PrevPageText="前一页" PageSize="3" NextPageText="下一页" LastPageText="尾页"
FirstPageText="首页" ShowInputBox="Always" CustomInfoTextAlign="Center" ShowNavigationToolTip="True"
CustomInfoHTML="第<font color='red'><b>%CurrentPageIndex%</b></font>页 共%PageCount% 页 %StartRecordIndex%-%EndRecordIndex%"
InputBoxStyle="width:19px" meta:resourceKey="AspNetPager1" width="506px" showcustominfosection="Left"
onpagechanged="AspNetPager1_PageChanged" horizontalalign="Center">
</webdiyer:aspnetpager>
#5
ASP.NET2.0中datalist仿百度分页
注意这个分页在1.0中无效
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;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.IO;
public partial class mysql : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]);
int ToatalCountRecord;//总记录数
int PageItem = 4;//每页显示的条数
int CurrentPage = 1;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (Request.QueryString["page"] != null)
{
if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage))
{
Response.Write("请输入分页参数!");
Response.End();
return;
}
}
this.BuidGrid();
}
}
private void BuidGrid()
{
string s2 = "select * from ts1";
SqlDataAdapter da = new SqlDataAdapter(s2, conn);
DataSet ds = new DataSet();
int startRecord = (CurrentPage - 1) * PageItem;
da.Fill(ds, startRecord, PageItem, "a");
this.DataList1.DataSource = ds.Tables["a"].DefaultView;
this.DataList1.DataBind();
SqlCommand comm = new SqlCommand("select count(*) from ts1", conn);
conn.Open();
ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar());
conn.Close();
BuildPages();
}
private void BuildPages()
{
int Step = 5;//偏移量
int LeftNum = 0;//做界限
int RightNum = 0;//右界限
string PageUrl = Request.FilePath;
int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
if (CurrentPage > 1)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + " </a>";
}
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += " <font color=red>" + " " +"["+i.ToString() +"]"+ "" + " </font>";
}
else
{
OutPut += " <a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + " </a>";
}
}
if (CurrentPage < PageCount)
{
OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + " </a>";
}
this.PageInfo.InnerHtml = OutPut;
}
}
请注意颜色标注部分
需要在前台添加一个 <div id="PageInfo" runat="server" >
#6
ASPX页代码
<asp:DataList ID="Data_p" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal" runat="server" DataKeyField="id" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
姓名: <%# Eval("name") %> <%# Eval("id") %> <br />
电话: <%# Eval("tel") %> <br />
</ItemTemplate>
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server" Visible="true" > </asp:PlaceHolder>
</FooterTemplate>
<EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" />
</asp:DataList>
C# code
ASPX.cs代码
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
db pdb = new db();
for (int i = 0; i < pdb.pgs().PageCount; i++)
{
HyperLink h=new HyperLink();
Literal l = new Literal();
int n = i + 1;
h.Text = n.ToString();
h.ID=n.ToString();
h.NavigateUrl = "datelist2.aspx?page=" + i.ToString();
l.Text =" ";
ph.Controls.Add(h);
ph.Controls.Add(l);
}
}
}
<asp:DataList ID="Data_p" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal" runat="server" DataKeyField="id" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
姓名: <%# Eval("name") %> <%# Eval("id") %> <br />
电话: <%# Eval("tel") %> <br />
</ItemTemplate>
<FooterTemplate>
<asp:PlaceHolder ID="ph" runat="server" Visible="true" > </asp:PlaceHolder>
</FooterTemplate>
<EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" />
</asp:DataList>
C# code
ASPX.cs代码
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
db pdb = new db();
for (int i = 0; i < pdb.pgs().PageCount; i++)
{
HyperLink h=new HyperLink();
Literal l = new Literal();
int n = i + 1;
h.Text = n.ToString();
h.ID=n.ToString();
h.NavigateUrl = "datelist2.aspx?page=" + i.ToString();
l.Text =" ";
ph.Controls.Add(h);
ph.Controls.Add(l);
}
}
}
#7
#8
这个嘛简单啊,哈哈,给你个类
using System;
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;
/// <summary>
/// PageNum 的摘要说明
/// </summary>
public class PageNum
{
public PageNum()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
///
/// </summary>
/// <param name="pageurl">传递你需要分页的地址,类似于"ContributeManage.aspx?state=" + state + "&isdel=" + isdel + "&isCommend=" + isCommend + "",如果没有参数则请加上1=1参数</param>
/// <param name="page">当前页码</param>
/// <param name="total">总的页数</param>
/// <param name="PageSize">每页显示的条数</param>
/// <returns>分页代码,类似于共4页 首页 上一页 1 2 3 4 下一页 末页</returns>
public static string CreatePage(string pageurl, int page, int total, int PageSize)
{
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (PageSize != 0)
{
allpage = (total / PageSize);
allpage = ((total % PageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + pageurl + "&page=1\">首页</a> <a href=\"" + pageurl + "&page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + pageurl + "&page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? " <a href=\"" + pageurl + "&page=" + next + "\">下一页</a> <a href=\"" + pageurl + "&page=" + allpage + "\">尾页</a>" : " 下一页 尾页";
pagestr += " 跳转到:<input id='txtPageNum'style=\"width:30px;\" type='text' size=3 value='" + page.ToString() + "' onkeyup=\"javascript:value=value.replace(/[^\\d]/g,'');if(this.value=='0'||this.value==''||parseInt(this.value)<0){this.value=1;}if(parseInt(this.value)>parseInt(" + allpage.ToString() + ")){this.value=" + allpage.ToString() + "}\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"><input type='button' value='转到' name='goToPage' onclick=\"javascript:window.location.href='" + pageurl + "&page='+document.getElementById('txtPageNum').value;\">";
return pagestr;
}
用这个类传几个参数就可以用了
对了,记住要用存储过程分页
using System;
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;
/// <summary>
/// PageNum 的摘要说明
/// </summary>
public class PageNum
{
public PageNum()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
///
/// </summary>
/// <param name="pageurl">传递你需要分页的地址,类似于"ContributeManage.aspx?state=" + state + "&isdel=" + isdel + "&isCommend=" + isCommend + "",如果没有参数则请加上1=1参数</param>
/// <param name="page">当前页码</param>
/// <param name="total">总的页数</param>
/// <param name="PageSize">每页显示的条数</param>
/// <returns>分页代码,类似于共4页 首页 上一页 1 2 3 4 下一页 末页</returns>
public static string CreatePage(string pageurl, int page, int total, int PageSize)
{
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (PageSize != 0)
{
allpage = (total / PageSize);
allpage = ((total % PageSize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + pageurl + "&page=1\">首页</a> <a href=\"" + pageurl + "&page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + pageurl + "&page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? " <a href=\"" + pageurl + "&page=" + next + "\">下一页</a> <a href=\"" + pageurl + "&page=" + allpage + "\">尾页</a>" : " 下一页 尾页";
pagestr += " 跳转到:<input id='txtPageNum'style=\"width:30px;\" type='text' size=3 value='" + page.ToString() + "' onkeyup=\"javascript:value=value.replace(/[^\\d]/g,'');if(this.value=='0'||this.value==''||parseInt(this.value)<0){this.value=1;}if(parseInt(this.value)>parseInt(" + allpage.ToString() + ")){this.value=" + allpage.ToString() + "}\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\\d]/g,''))\"><input type='button' value='转到' name='goToPage' onclick=\"javascript:window.location.href='" + pageurl + "&page='+document.getElementById('txtPageNum').value;\">";
return pagestr;
}
用这个类传几个参数就可以用了
对了,记住要用存储过程分页
#9
/**//*--实现分页的通用存储过程
显示指定表、视图、查询结果的第X页
对于表中主键或标识列的情况,直接从原表取数查询,其它情况使用临时表的方法
如果视图或查询结果中有主键,不推荐此方法
如果使用查询语句,而且查询语句使用了order by,则查询语句必须包含top 语句
最后更新时间: 2008.01.20
--邹建 2003.09(引用请保留此信息)--*/
/**//*--调用示例
EXEC dbo.p_show
@QueryStr = N'tb',
@PageSize = 5,
@PageCurrent = 3,
@FdShow = 'id, colid, name',
@FdOrder = 'colid, name'
select id, colid from tb
order by colid, name
EXEC dbo.p_show
@QueryStr = N'
SELECT TOP 100 PERCENT
*
FROM dbo.sysobjects
ORDER BY xtype',
@PageSize = 5,
@PageCurrent = 2,
@FdShow = 'name, xtype',
@FdOrder = 'xtype, name'
--*/
CREATE PROC dbo.p_show
@QueryStr nvarchar(4000), -- 表名、视图名、查询语句
@PageSize int=10, -- 每页的大小(行数)
@PageCurrent int=1, -- 要显示的页
@FdShow nvarchar (4000) = N'', -- 要显示的字段列表,如果查询结果不需要标识字段,需要指定此值,且不包含标识字段
@FdOrder nvarchar (1000) = N'' -- 排序字段列表
AS
SET NOCOUNT ON
DECLARE
@FdName sysname, --表中的主键或表、临时表中的标识列名
@Id1 sysname, --开始和结束的记录号
@Id2 sysname,
@Obj_ID int --对象ID
--表中有复合主键的处理
DECLARE
@strfd nvarchar(2000), --复合主键列表
@strjoin nvarchar(4000), --连接字段
@strwhere nvarchar(2000) --查询条件
SELECT
@Obj_ID = OBJECT_ID(@QueryStr),
@FdShow = CASE
WHEN @FdShow > N'' THEN N' ' + @FdShow
ELSE N' *'
END,
@FdOrder = CASE
WHEN @FdOrder > N'' THEN N' ORDER BY ' + @FdOrder
ELSE N' '
END,
@QueryStr = CASE
WHEN @Obj_ID IS NULL THEN N' (' + @QueryStr + N')A'
ELSE N' ' + @QueryStr
END
-- 如果显示第一页,可以直接用 top 来完成
IF @PageCurrent = 1
BEGIN
SELECT
@Id1 = CAST(@PageSize as varchar(20))
EXEC(N'
SELECT TOP ' + @Id1 + N'
' + @FdShow + N'
FROM ' + @QueryStr + N'
' + @FdOrder
)
RETURN
END
-- 如果是表, 则检查表中是否有标识更或主键
IF @Obj_ID IS NULL OR OBJECTPROPERTY(@Obj_ID, 'IsTable') = 0
GOTO lb_usetemp
ELSE
BEGIN
SELECT
@Id1 = CAST(@PageSize as varchar(20)),
@Id2 = CAST((@PageCurrent - 1) * @PageSize as varchar(20))
-- 标识列
SELECT
@FdName = name
FROM dbo.syscolumns
WHERE id = @Obj_ID
AND status = 0x80
IF @@ROWCOUNT = 0 --如果表中无标识列,则检查表中是否有主键
BEGIN
DECLARE
@pk_number int
SELECT
@strfd = N'',
@strjoin = N'',
@strwhere = N''
SELECT
@strfd = @strfd
+ N',' + QUOTENAME(name),
@strjoin = @strjoin
+ N' AND A.' + QUOTENAME(name)
+ N'=B.' + QUOTENAME(name),
@strwhere = @strwhere
+ N' AND B.' + QUOTENAME(name) + N' IS NULL'
FROM(
SELECT
IX.id, IX.indid,
IXC.colid, ixc.keyno,
C.name
FROM dbo.sysobjects O,
dbo.sysindexes IX,
dbo.sysindexkeys IXC,
dbo.syscolumns C
WHERE O.parent_obj = @Obj_ID
AND O.xtype = 'PK'
AND O.name = IX.name
AND IX.id = @Obj_ID
AND IX.id = IXC.id
AND IX.indid = IXC.indid
AND IXC.id = C.id
AND IXC.colid = C.colid
)A
ORDER BY keyno
SELECT
@pk_number = @@ROWCOUNT,
@strfd = STUFF(@strfd, 1, 1, N''),
@strjoin = STUFF(@strjoin, 1, 5, N''),
@strwhere = STUFF(@strwhere, 1, 5, N'')
IF @pk_number = 0
GOTO lb_usetemp --如果表中无主键,则用临时表处理
ELSE IF @pk_number = 1
BEGIN
SELECT
@FdName = @strfd
GOTO lb_useidentity -- 使用单一主键
END
ELSE GOTO lb_usepk -- 使用复合主键
END
END
/**//*--使用标识列或主键为单一字段的处理方法--*/
lb_useidentity:
EXEC(N'
SELECT TOP ' + @Id1 + N'
' + @FdShow + N'
FROM '+@QueryStr + N'
WHERE ' + @FdName + ' NOT IN(
SELECT TOP ' + @Id2 + N'
' + @FdName + '
FROM ' + @QueryStr + N'
' + @FdOrder + N')
' + @FdOrder + N'
')
RETURN
/**//*--表中有复合主键的处理方法--*/
lb_usepk:
EXEC(N'
SELECT
' + @FdShow + N'
FROM(
SELECT TOP ' + @Id1 + N'
A.*
FROM ' + @QueryStr + N' A
LEFT JOIN(
SELECT TOP ' + @Id2 + N'
' + @strfd + N'
FROM ' + @QueryStr + N'
' + @FdOrder + N'
)B
ON ' + @strjoin + N'
WHERE ' + @strwhere + N'
' + @FdOrder + N'
)A
' + @FdOrder + N'
')
RETURN
/**//*--用临时表处理的方法--*/
lb_usetemp:
SELECT
@FdName = QUOTENAME(N'ID_' + CAST(NEWID() as varchar(40))),
@Id1 = CAST(@PageSize * (@PageCurrent-1) as varchar(20)),
@Id2 = CAST(@PageSize * @PageCurrent-1 as varchar(20))
EXEC(N'
SELECT
' + @FdName + N' = IDENTITY(int, 0, 1),
' + @FdShow + N'
INTO #tb
FROM(
SELECT TOP 100 PERCENT
*
FROM ' + @QueryStr + N'
' + @FdOrder + N'
)A
' + @FdOrder + N'
SELECT
' + @FdShow + N'
FROM #tb
WHERE ' + @FdName + ' BETWEEN ' + @Id1 + ' AND ' + @Id2 + N'
'
)
GO
还要写一个存储过程得到总条数这样就搞定,你还可以改一下然后可以控制样式
#10
安装5楼的方法做了,谢谢你哦,呵呵,成功了,不够那是刷新的,如果能像QQ评论搞成无刷新的数字分页那将更好,不过将就先用着哦,百度 google都用那样的刷新呢 呵呵
#11
如果想用不刷新的,你就得用到AJAX的了。那么就要得在服务器端写了
#12
sssssssss