求asp.net 手写分页代码,不要使用.net控件!
可以的话,小弟会加分的!
可以的话,小弟会加分的!
解决方案 »
- 东查西看,最后只好求助,奉100分以谢高手,表达敬意
- 关于解析XML的问题!
- 请教各位高手批量更新静态页的问题...
- RegularExpressionValidator控制输入日期的格式怎么写
- 为什么和MasterPage 联系的页面中控件的触发事件,没有效果 (进来有分)
- 请大家给一个在asp.net中使用多线程的例子
- 异常详细信息: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。
- 请问一下这样的正则表达式如何写?
- 企业QQ邮箱有没有开发接口?
- 如何对选项保值
- Ajax动态生成控件问题(继续求助)
- 求一正则表达式,在线=,急
<td height="30" align="center" class="black">
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">[首页]</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">[上一页]</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">[下一页]</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" OnClick="LinkButton4_Click">[尾页]</asp:LinkButton>
<br />
共 <span class="redb">
<%= TotalRows %>
</span>条信息,当前显示<span class="redb">
<%= CurrentRows %>
</span>条,共<span class="redb">
<%= TotalPage %>
</span>页 到第
<asp:DropDownList ID="ddpPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddpPage_SelectedIndexChanged">
</asp:DropDownList>
页
</td>
</tr>
后台类中这样写:#region 分页属性
public int PageIndex
{
get { return Convert.ToInt32(ViewState["pi"]); }
set { ViewState["pi"] = value; }
}
public int PageSize
{
get { return Convert.ToInt32(ViewState["ps"]); }
set { ViewState["ps"] = value; }
}
public int TotalPage
{
get { return Convert.ToInt32(ViewState["tp"]); }
set { ViewState["tp"] = value; }
}
public int TotalRows
{
get { return Convert.ToInt32(ViewState["tr"]); }
set { ViewState["tr"] = value; }
}
public int CurrentRows
{
get { return Convert.ToInt32(ViewState["cr"]); }
set { ViewState["cr"] = value; }
}
#endregion #region 初始化
protected void Page_Load(object sender, EventArgs e)
{
PageIndex = 1;
PageSize = 1000;
TotalPage = 0;
TotalRows = 0;
CurrentRows = 0;
}
#endregion#region 控制按钮显示
public void ShowButton()
{
if (TotalPage == 0 || TotalPage == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
else if (PageIndex == 1)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
}
else if (PageIndex == TotalPage)
{
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
else
{
LinkButton1.Enabled = true;
LinkButton2.Enabled = true;
LinkButton3.Enabled = true;
LinkButton4.Enabled = true;
}
}
#endregion #region 分页
protected void LinkButton1_Click(object sender, EventArgs e)
{
PageIndex = 1;
BandData();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
PageIndex -= 1;
BandData();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
PageIndex += 1;
BandData();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
PageIndex = TotalPage;
BandData();
}
#endregion protected void ddpPage_SelectedIndexChanged(object sender, EventArgs e)
{
PageIndex = Convert.ToInt32(ddpPage.SelectedValue);
BandData();
}在页面加载前要先绑定数据,在绑定数据里面给分页属性赋值,绑定好数据后,调用ShowButton();
create proc pro_page
@PageIndex int,
@PageSize int
as
select top @pageSize * from tbName where id not in (
select top @pageSize*(@pageIndex-1) id from tbName
)
这个是简单的 top方法 还有很多种方法 网上可以搜搜 有更快效率的分页存储过程
stringbuilder
<a href="a.aspx?pageno=1"/>
MSSQL2000
ALTER procedure P_GetPagedOrders2000
(@startIndex int,
@endIndex int
)
as
set nocount on(--这行可有可无)
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex(--这行可有可无)
insert into @indextable(nid) select orderid from orders order by orderid desc
select O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID
inner join @indextable t on
O.orderid=t.nid
where t.id between @startIndex and @endIndex order by t.id
set nocount off
RETURN
MSSQL2005
ALTER PROCEDURE P_GetPagedOrders2005
(@startIndex INT,
@endindex INT
)
AS
begin
WITH orderList AS (
SELECT ROW_NUMBER() OVER (ORDER BY O.orderid DESC)AS Row, O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID)SELECT orderid,orderdate,customerid,companyName,employeeName
FROM orderlist
WHERE Row between @startIndex and @endIndexend
之前有一次面试的时候说我分页就用 aspnetpager 但是那个面试官告诉我。。aspnetpager 用起来不方便。。有时候会乱分页
http://topic.csdn.net/u/20100327/17/8c6bb11c-889c-4d0d-a829-3b2756501e08.html
{
List<Hashtable> list = B_AdminMember.B_getUserInfo();
ps = new PagedDataSource();
ps.DataSource = list;
ps.AllowPaging = true;
ps.PageSize = 2;
Cache.Insert("Reps", ps); repUserInfo.DataSource = (PagedDataSource)Cache["Reps"];
repUserInfo.DataBind();
}
protected void lbFirstPage_Click(object sender, EventArgs e)
{
if (Cache["Reps"] != null)
{
ps = (PagedDataSource)Cache["Reps"];
}
else
{
List<Hashtable> cl = B_AdminMember.B_getUserInfo();
ps = new PagedDataSource();
ps.DataSource = cl;
Cache.Insert("Reps", ps);
ps = (PagedDataSource)Cache["Reps"];
}
ps.CurrentPageIndex = 0;
repUserInfo.DataSource = ps;
repUserInfo.DataBind();
} protected void lbPreviousPage_Click(object sender, EventArgs e)
{
if (Cache["Reps"] != null)
{
ps = (PagedDataSource)Cache["Reps"];
}
else
{
List<Hashtable> cl = B_AdminMember.B_getUserInfo();//从数据库获得list信息
ps = new PagedDataSource();
ps.DataSource = cl;
Cache.Insert("Reps", ps);
ps = (PagedDataSource)Cache["Reps"];
}
if (!ps.IsFirstPage)
{
ps.CurrentPageIndex--;
repUserInfo.DataSource = ps;
repUserInfo.DataBind();
}
} protected void lbNextPage_Click(object sender, EventArgs e)
{
if (Cache["Reps"] != null)
{
ps = (PagedDataSource)Cache["Reps"];
}
else
{
List<Hashtable> cl = B_AdminMember.B_getUserInfo();
ps = new PagedDataSource();
ps.DataSource = cl;
Cache.Insert("Reps", ps);
ps = (PagedDataSource)Cache["Reps"];
}
if (!ps.IsLastPage)
{
ps.CurrentPageIndex++;
repUserInfo.DataSource = ps;
repUserInfo.DataBind();
}
Response.Write("window.location.href='#tab2';");
} protected void lbLastPage_Click(object sender, EventArgs e)
{
if (Cache["Reps"] != null)
{
ps = (PagedDataSource)Cache["Reps"];
}
else
{
List<Hashtable> cl = B_AdminMember.B_getUserInfo();
ps = new PagedDataSource();
ps.DataSource = cl;
Cache.Insert("Reps", ps);
ps = (PagedDataSource)Cache["Reps"];
}
ps.CurrentPageIndex = ps.PageCount - 1;
repUserInfo.DataSource = ps;
repUserInfo.DataBind();
}
``````````````````
BindUserInfoData();放在pageload里,认真看,你可以看懂的。
using System.Collections.Generic;
using System.Web;
using dressmodel;
using System.Text;
using System.Text.RegularExpressions; public class PaginatedList<T> : List<T>
{
private int pagezise; public int Pagezise
{
get { return pagezise; }
set { pagezise = value; }
}
private int pageindex; public int Pageindex
{
get { return pageindex; }
set { pageindex = value; }
}
private int TotalCount; public int TotalCount1
{
get { return TotalCount; }
set { TotalCount = value; }
}
private int TotalPage; public int TotalPage1
{
get { return TotalPage; }
set { TotalPage = value; }
}
private string url; public string Url
{
get { return url; }
set { url = value; }
}
public PaginatedList(IList<T> source, int pageindex, int pagezise,int totalcount, string url)
{
this.pageindex = pageindex;
this.pagezise = pagezise;
TotalCount = totalcount;
TotalPage = (int)Math.Ceiling(TotalCount / (double)pagezise);
this.AddRange(source);
this.url = url; } #region 分页属性 返回分页数据
public string GetPatePar
{
get
{
if (TotalCount1 >= 1)
{
StringBuilder str = new StringBuilder();
int startpage = (pageindex - 1) / 10 * 10 + 1;
int endpage = (pageindex - 1) / 10 * 10 + 10;
int x = endpage > TotalPage ? TotalPage : endpage;
str.Append("<div class='anpager'>"); if (HasPreviousPage)
{
str.Append("<a href=" + url + "?page=1" + GetURLParms + ">首页</a>");
str.Append("<a href=" + url + "?page=" + (pageindex - 1) + GetURLParms + ">上一页</a>");
}
else
{
str.Append("<a href=#>首页</a>");
str.Append("<a href=#>上一页</a>");
}
for (int i = startpage; i <= x; i++)
{
if (i == pageindex)
{
str.Append(string.Format("<a style='margin-left:3px; margin-right:3px;padding:2px 4px 2px 4px;color:red;border:1px solid #cccccc;background-color:#ffffff; '>" + i + "</a>"));
}
else
{
str.Append(string.Format("<a href=" + url + "?page=" + i + GetURLParms + ">" + i + "</a>"));
}
}
if (HasNextPage)
{ str.Append("<a href=" + url + "?page=" + (pageindex + 1) + GetURLParms + ">下一页</a>");
str.Append("<a href=" + url + "?page=" + TotalPage + GetURLParms + ">尾页</a>");
}
else
{
str.Append("<a href=#>下一页</a>");
str.Append("<a href=#>尾页</a>");
}
str.Append("</div>");
return str.ToString();
}
else
{
return "<font color=red>暂无数据!</font>";
}
}
}
#endregion
/// <summary>
/// url重写
/// </summary>
private string GetURLParms
{
get
{
string query = HttpContext.Current.Request.Url.Query;
query = Regex.Replace(query, @"\?page=\d*", "", RegexOptions.Compiled);
return query;
}
}
/// <summary>
///判断是否有上一页
/// </summary>
public bool HasPreviousPage
{
get
{
return (pageindex > 1);
}
}
/// <summary>
///判断是否有下一页
/// </summary>
public bool HasNextPage
{
get
{
return (pageindex < TotalPage);
}
}
}
PagedDataSource pds = new PagedDataSource();
public void DataBind()
{
pds.DataSource = noticeMessage.getAllTrends();
pds.AllowPaging = true;
pds.PageSize = 10; pds.CurrentPageIndex = Pager;
this.Label4.Text = "第 " + (pds.CurrentPageIndex + 1) + " 页,共 " + (pds.PageCount) + " 页"; //设置PageDown / PageUp 按钮的状态
this.Button3.Enabled = true;
this.Button4.Enabled = true; if (pds.IsFirstPage)
this.Button3.Enabled = false;
if (pds.IsLastPage)
this.Button4.Enabled = false; //控件绑定信息
this.DataList1.DataSource = pds;
this.DataList1.DataBind(); } //声明一个变量保存当前页码
public int Pager
{
get { return (int)ViewState["Pager"]; }
set { ViewState["Pager"] = value; }
} //pageUp
protected void Button3_Click(object sender, EventArgs e)
{
Pager--;
DataBind();
}
//pageDown
protected void Button4_Click(object sender, EventArgs e)
{
Pager++;
DataBind();
}