|
大家好,好久沒(méi)有更新我的博客了,特地整理了關(guān)于分頁(yè)技術(shù)的知識(shí)點(diǎn),堪稱超簡(jiǎn)單實(shí)用的分頁(yè). --------------------------------------------------------------------------------
前臺(tái)代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml" > <head runat="server"> <title>無(wú)標(biāo)題頁(yè)</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <TABLE borderColor=#e5e3e3 cellSpacing=0 cellPadding=3 width=790 align=center border=1> <TR bgColor=#fff9ec> <TH align="center" width=200 bgColor=#fff9ec>名稱</TH> <TH align="center" width=200 bgColor=#fff9ec>類別</TH> <TH align="center" width=200 bgColor=#fff9ec>最小值</TH> <TH align="center" width=200 bgColor=#fff9ec>最大值</TH> </TR> </HeaderTemplate> <ItemTemplate> <TR> <TD align="center" width=200><%#Eval("job_id")%></TD> <TD align="center" width=200><%#Eval("job_desc")%></TD> <TD align="center" width=200><%#Eval("min_lvl")%></TD> <TD align="center" width=200><%#Eval("max_lvl")%></TD> </TR> </ItemTemplate> <FooterTemplate> </TABLE> </FooterTemplate> </asp:Repeater> </div> <p></p> <div align="center"> 當(dāng)前頁(yè)<asp:Label ID="nowpage" runat="server" Font-Bold="True"></asp:Label>/<asp:Label ID="allpages" runat="server" Font-Bold="True" ForeColor="#C00000"></asp:Label> 每頁(yè) <asp:Label ID="onepage" runat="server" Font-Bold="True"></asp:Label> 條 共 <asp:Label ID="allpage" runat="server" Font-Bold="True"></asp:Label> 頁(yè)<asp:HyperLink ID="firstpage" runat="server">第一頁(yè)</asp:HyperLink> <asp:HyperLink ID="prepage" runat="server">上一頁(yè)</asp:HyperLink> <asp:HyperLink ID="nextpage" runat="server">下一頁(yè)</asp:HyperLink> <asp:HyperLink ID="endpage" runat="server">末一頁(yè)</asp:HyperLink> 共有 <asp:Label ID="allmsg" runat="server" Font-Bold="True"></asp:Label> 條留言 </div> <br /> <div align="center"><b>PowerBy ABEN</b> <a target="_blank">http://blog.sina.com.cn/whaben</a> </div> </form> </body> </html>
后臺(tái)事件代碼: 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; //------導(dǎo)包 using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int CurPage; if (Request.QueryString["Page"] != null) CurPage = Convert.ToInt32(Request.QueryString["Page"]); else CurPage = 1; //連接數(shù)據(jù)庫(kù)及將數(shù)據(jù)封裝到一個(gè)數(shù)據(jù)集中 SqlConnection con = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=sa;"); SqlDataAdapter adapter = new SqlDataAdapter("select * from jobs", con); DataSet ds = new DataSet(); adapter.Fill(ds, "jobs");
PagedDataSource ps = new PagedDataSource(); ps.DataSource = ds.Tables["jobs"].DefaultView; ps.AllowPaging = true; //每個(gè)頁(yè)面顯示的條數(shù) ps.PageSize = 5; onepage.Text = ps.PageSize.ToString(); //求數(shù)據(jù)的總數(shù) allmsg.Text = ps.DataSourceCount.ToString(); ps.CurrentPageIndex = CurPage - 1; //求總頁(yè) allpage.Text = ps.PageCount.ToString(); allpages.Text = ps.PageCount.ToString(); nowpage.Text = CurPage.ToString(); //將數(shù)據(jù)源與控件綁定 Repeater1.DataSource = ps; Repeater1.DataBind();
//上一頁(yè) if (!ps.IsFirstPage) { firstpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1"; prepage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); } //下一頁(yè) if (!ps.IsLastPage) { nextpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1); endpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(ps.PageCount); } } }
如上述代碼有什么問(wèn)題請(qǐng)留言或聯(lián)系我!
|