AspNetPager分頁控制項使用方法(不使用DBAccess.cs檔案)一、前台顯示介面代碼Default.aspx(注意,代碼運行環境是VS.2005)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default4" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title>用AspNetPager.dll控制項的分頁方法操作方法</title></head><body> <form id="form1" runat="server"> <div> <table border=1> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr> <td><%#DataBinder.Eval(Container.DataItem, "city_id")%></td> <td><%#DataBinder.Eval(Container.DataItem, "city_name")%></td> </tr> </ItemTemplate> </asp:Repeater> </table> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" NumericButtonCount="6" UrlPaging="true" NumericButtonTextFormatString="[{0}]" CustomInfoHTML="第 <font color='red'><b>%CurrentPageIndex%</b></font> 頁 共 %PageCount% 頁 顯示 %StartRecordIndex%-%EndRecordIndex% 條" ShowCustomInfoSection="left"FirstPageText="首頁" LastPageText="末頁" NextPageText="下頁" PrevPageText="上頁" Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true" ShowInputBox="Always" SubmitButtonText="跳轉" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged" > </webdiyer:AspNetPager>// NumericButtonCount="6" 顯示頁碼的個數 // NumericButtonTextFormatString="[{0}]" 頁碼顯示的格式// ShowCustomInfoSection="left" 顯示左邊的(第幾頁、共幾頁、顯示幾條)// TextBeforeInputBox="轉到第" InputBox前面顯示的字// TextAfterInputBox="頁" InputBox後面顯示的字// PagingButtonSpacing="10px" 字與字間的距離 </div> </form></body></html>二、Default.aspx.cs頁面的代碼using System.Data.SqlClient;public partial class Default : System.Web.UI.Page{ //SqlConnection con = connsql.ZYJCreateConnection(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGrid(); } } private SqlConnection con; //private string DBName = "tongjinet"; //建立連線物件並開啟 public void Open() { if (con == null) //con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName); con = connsql.ZYJCreateConnection(); if (con.State == ConnectionState.Closed) con.Open(); } //建立一個命令對象並返回該對象 public SqlCommand CreateCommand(string sqlStr) { Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = sqlStr; cmd.Connection = con; return cmd; } //產生一個對象並返回該結果集第一行第一列 public object GetScalar(string sqlStr) { SqlCommand cmd = CreateCommand(sqlStr); object obj = cmd.ExecuteScalar(); //CommadnBehavior.CloseConnection是將於DataReader的資料庫連結關聯起來 //當關閉DataReader對象時候也自動關閉連結 return obj; } //執行資料庫查詢並返回一個資料集 [當前頁碼,每頁記錄條數] public DataSet GetCurrentPage(int pageIndex, int pageSize) { //設定匯入的起始地址 int firstPage = pageIndex * pageSize; string sqlStr = "select * from city order by city_id desc"; SqlCommand cmd = CreateCommand(sqlStr); DataSet dataset = new DataSet(); SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd); dataAdapter.Fill(dataset, firstPage, pageSize, "city"); cmd.Dispose(); Close(); dataAdapter.Dispose(); return dataset; } //獲得查詢資料的總條數 public object GetAllCount() { string sqlStr = "select count(*) from city"; object obj = GetScalar(sqlStr); return obj; } //關閉資料庫 public void Close() { if (con != null) { con.Close(); } } //釋放資源 public void Dispose() { if (con != null) { con.Dispose(); con = null; } } protected void AspNetPager1_PageChanged(object sender, EventArgs e) { BindGrid(); } public void BindGrid() { this.AspNetPager1.RecordCount = Int32.Parse(GetAllCount().ToString()); int pageIndex = this.AspNetPager1.CurrentPageIndex - 1; int pageSize = this.AspNetPager1.PageSize = 5; Repeater1.DataSource = GetCurrentPage(pageIndex, pageSize); Repeater1.DataBind(); }}