您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何理解asp.net的分頁控件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一、說明
AspNetPager.dll這個分頁控件主要用于asp.net webform網站,現將整理代碼如下
二、代碼
1、首先在測試頁面Default.aspx頁面添加引用
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
2、寫一個Repeater列表控件用于顯示數據
<asp:Repeater ID="rptNews" runat="server"> <ItemTemplate> <li> <span><%# Eval("time") %></span> <a href="NewsShow.aspx?id=<%# Eval("id") %>"><%# Access.GetStringNum( Eval("name").ToString(),15) %></a> </li> </ItemTemplate> </asp:Repeater>
3、添加
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CustomInfoStyle="FONT-SIZE: 12px" FirstPageText="首頁" HorizontalAlign="Center" inputbox LastPageText="尾頁" meta:resourcekey="AspNetPager1" NextPageText="下一頁" PageSize="10" PrevPageText="上一頁" Style="font-size: 14px" Width="95%" CssClass="anpager" CurrentPageButtonClass="cpb" OnPageChanging="AspNetPager1_PageChanging" ShowBoxThreshold="10"> </webdiyer:AspNetPager>
PageSize屬性是用于設置每頁顯示的數量
4、后臺代碼綁定
//測試數據源<br>private void ShowNews() { String strSql = String.Format("select * from News order by time asc"); DataTable dtbl = Access.ExecuteDataTable(strSql, null); this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl); this.rptNews.DataBind(); }
Access是測試數據庫訪問類,在最后的Demo中提供給大家
5、分頁控件點擊頁碼事件
//分頁 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { this.AspNetPager1.CurrentPageIndex = e.NewPageIndex; ShowNews(); }
最后奉上整頁代碼:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>asp.net分頁控件</title> <link href="css/css.css" rel="stylesheet" type="text/css" /> <style type="text/css"> .aboutcontentnr{width:100%; height:auto; } </style> </head> <body> <form id="form1" runat="server"> <div> <ul class="news"> <asp:Repeater ID="rptNews" runat="server"> <ItemTemplate> <li><span> <%# Eval("time") %></span><a href="NewsShow.aspx?id=<%# Eval("id") %>"><%# Access.GetStringNum( Eval("name").ToString(),15) %></a></li> </ItemTemplate> </asp:Repeater> </ul> <div class="paginator"> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CustomInfoStyle="FONT-SIZE: 12px" FirstPageText="首頁" HorizontalAlign="Center" inputbox LastPageText="尾頁" meta:resourcekey="AspNetPager1" NextPageText="下一頁" PageSize="10" PrevPageText="上一頁" Style="font-size: 14px" Width="95%" CssClass="anpager" CurrentPageButtonClass="cpb" OnPageChanging="AspNetPager1_PageChanging" ShowBoxThreshold="10"> </webdiyer:AspNetPager> </div> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowNews(); } } //測試數據源 private void ShowNews() { String strSql = String.Format("select * from News order by time asc"); DataTable dtbl = Access.ExecuteDataTable(strSql, null); this.rptNews.DataSource = Access.GetPageDataSource(AspNetPager1, AspNetPager1.CurrentPageIndex - 1, dtbl); this.rptNews.DataBind(); } //分頁點擊頁碼事件 protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { this.AspNetPager1.CurrentPageIndex = e.NewPageIndex; ShowNews(); } }
三、Demo
AspNetPager(dll)
AspNetPage(Demo)
關于如何理解asp.net的分頁控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。