想找一个分页程序的代码谁能给我一个啊?谢谢了
希望给我一个带 代码的例子我是新手
分页效果最好就象这个论坛一样的 共500篇 共4页 1 2 3 4 5 6
类似这样的 也可发我邮箱 melack@163.com
谢谢了!!!
[解决办法]
自己做的分页控件 :
public partial class WebUserControl : System.Web.UI.UserControl
{
private string imagePreUrl;
/// <summary>
/// 上一页的图片地址
/// </summary>
public string ImagePrevUrl
{
get
{
return imagePreUrl;
}
set
{
this.imagePreUrl = value;
}
}
private string imageNextUrl;
/// <summary>
/// 下一页的图片地址
/// </summary>
public string ImageNextUrl
{
get
{
return imageNextUrl;
}
set
{
this.imageNextUrl = value;
}
}
private string pageName;
/// <summary>
/// 使用分页的页面名称,注意?后带参数的需要自行修改
/// </summary>
public string PageName
{
get
{
return pageName;
}
set
{
this.pageName = value;
}
}
/// <summary>
/// 分页大小
/// </summary>
private int pageSize;
public int PageSize
{
get
{
return pageSize;
}
set
{
this.pageSize = value;
}
}
/// <summary>
/// 可见连接的个数
/// </summary>
private int visibilityPageCount;
public int VisibilityPageCount
{
get
{
return visibilityPageCount;
}
set
{
this.visibilityPageCount = value;
}
}
string currentPageUrl = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public PagedDataSource BindGrid(DataView dataView)
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = dataView;
objPds.AllowPaging = true;
objPds.PageSize = pageSize;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
if (CurPage > objPds.PageCount)
{
CurPage = 1;
}
}
else
{
CurPage = 1;
}
this.lblPageCount.Text = objPds.PageCount.ToString();
currentPageUrl = Request.Url.PathAndQuery;
if (currentPageUrl.Contains("Page="))
{
currentPageUrl = currentPageUrl.Remove(currentPageUrl.LastIndexOf("Page=") - 1);
}
if (currentPageUrl.Contains("?"))
{
currentPageUrl += "&Page=";
}
else
{
currentPageUrl += "?Page=";
}
this.PageAStr(CurPage, objPds.PageCount);
objPds.CurrentPageIndex = CurPage - 1;
lblCurrentPage.Text = CurPage.ToString();
// string RootPath = (Request.ApplicationPath != "/") ? Request.ApplicationPath + "/" : Request.ApplicationPath;
if (!objPds.IsFirstPage)
{
lnkPrev.NavigateUrl = currentPageUrl + Convert.ToString(CurPage - 1);
}
if (!objPds.IsLastPage)
{
lnkNext.NavigateUrl = currentPageUrl + Convert.ToString(CurPage + 1);
}
return objPds;
}
private void PageAStr(int pageNow, int pageCount)
{
int pageBegin = pageNow - visibilityPageCount / 2;
int pageEnd = pageNow + visibilityPageCount / 2;
if (pageNow == 1)
{
pageBegin = 1;
pageEnd = pageNow + visibilityPageCount - 1;
}
else if (pageCount < this.visibilityPageCount)
{
pageBegin = 1;
pageEnd = pageCount;
}
else
{
if ((pageNow + visibilityPageCount / 2) > pageCount)
{
pageBegin = pageCount - visibilityPageCount;
pageEnd = pageCount;
}
else
{
pageBegin = pageNow - visibilityPageCount / 2;
pageEnd = pageNow + visibilityPageCount / 2;
}
}
if (pageBegin <= 0)
{
pageBegin = 1;
}
if (pageEnd > pageCount)
{
pageEnd = pageCount;
}
this.ltl.Text = "";
if (pageBegin > 1)
{
this.ltl.Text += "...";
}
// string RootPath = (Request.ApplicationPath != "/") ? Request.ApplicationPath + "/" : Request.ApplicationPath;
for (int i = pageBegin; i <= pageEnd; i++)
{
this.ltl.Text += " <a href='" + currentPageUrl + i.ToString() + "'>" + i.ToString() + "</a>";
}
if (pageEnd < pageCount)
{
this.ltl.Text += "...";
}
}
public string StrPreImage()
{
return "<img src='" + this.imagePreUrl + "' alt='前一页' align='absmiddle' style='cursor:pointer;border:0px;' />";
}
public string StrNextImage()
{
return "<img src='" + this.imageNextUrl + "' alt='后一页' align='absmiddle' style='cursor:pointer;border:0px;' />";
}
}
[解决办法]
一段存储过程的分页代码:供参考
CREATE proc sp_DataPage
(
@RecordCount int output,
@QueryStr nvarchar(100)='table1',--表名、视图名、查询语句
@PageSize int=20, --每页的大小(行数)
@PageCurrent int=1, --要显示的页
@FdShow nvarchar (1000)='*', --要显示的字段列表
@IdentityStr nvarchar (100)='id', --主键
@WhereStr nvarchar (200)='1=1 and id % 11111 = 0',
@FdOrder nvarchar(100)='id', --排序
@isReturn bit=0
)
as
declare @sql nvarchar(2000)
declare @rs varchar(50)
declare @tsql nvarchar(200),@WStr nvarchar (200)
set @sql = ''
set @rs = ''
if(@isReturn=1)
begin
set @tsql=N'select @RecordCount = count(*) from ' + @QueryStr +' '+ @WhereStr
exec sp_executesql @tsql,N'@RecordCount int output',@RecordCount output
end
else
begin
set @RecordCount = @PageSize * @PageCurrent + 1
end
if @PageCurrent = 1
begin
set @sql = 'select top ' + cast(@PageSize as varchar(3)) + ' " ' +rtrim(cast ( @RecordCount as varchar(4)) )+' " as dzhrowcount,'+ @FdShow + ' from ' + @QueryStr + ' ' + @WhereStr + ' order by ' +@FdOrder
end
else
begin
set @sql = ' SELECT '+' "' +rtrim(cast ( @RecordCount as varchar(4)) )+' " as dzhrowcount , * from ( select top '+ cast(@PageSize*(@PageCurrent) as nvarchar(10)) +' '+@FdShow +' from '+ @QueryStr + ' ' + @WhereStr + ' order by ' +@FdOrder +' ) a where a.'+@IdentityStr ++' not in (select top '+cast(@PageSize*(@PageCurrent-1) as nvarchar(10)) + ' id from ' + @QueryStr + ' ' + @WhereStr + ' order by ' +@FdOrder +')'
-- set @sql = 'select top ' + cast(@PageSize as nvarchar(3)) + ' " ' +rtrim(cast ( @RecordCount as char(4)) )+' " as dzhrowcoun,'+ @FdShow + ' from ' + @QueryStr + ' ' + @wstr + ' ' + @IdentityStr + '> ( select max(' + @IdentityStr + ') from (select top ' + cast(@PageSize*(@PageCurrent-1) as nvarchar(10)) + ' ' + @IdentityStr + ' from ' + @QueryStr + ' ' + @WhereStr + ' order by ' + @FdOrder + ') as t) order by ' + @FdOrder
end
print @sql
execute(@sql)
return @RecordCount
GO