<%@ 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./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml" >
<head 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,"job_id")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"job_desc")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"min_lvl")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"max_lvl")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<!-- <asp:DataGrid ID="DataGrid1" runat="server">
</asp:DataGrid>-->
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="733px" FirstPageText="第一頁" LastPageText="最后一頁" NextPageText="下一頁" PrevPageText="上一頁" Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true" ShowInputBox="Always" SubmitButtonText="跳轉(zhuǎn)" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged" >
</webdiyer:AspNetPager>
</div>
</form>
</body>
</html>
//Default.aspx頁面的代碼
DBAccess db = new DBAccess();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
this.AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount().ToString());
int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
int pageSize = this.AspNetPager1.PageSize =5;
Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
Repeater1.DataBind();
}
//前臺已經(jīng)OK了,就差后臺數(shù)據(jù)庫連接及分頁需要的方法,本項(xiàng)目數(shù)據(jù)庫示例為PUBS數(shù)據(jù)庫[安裝SQL SERVER就有的],分頁的表為jobs表
using System.Data.SqlClient;
public class DBAccess
{
private SqlConnection con;
private string DBName = "pubs";
//創(chuàng)建連接對象并打開
public void Open()
{
if (con == null)
con = new SqlConnection("server=(local);uid=sa;pwd=sa;database="+DBName);
if (con.State == ConnectionState.Closed)
con.Open();
}
//創(chuàng)建一個(gè)命令對象并返回該對象
public SqlCommand CreateCommand(string sqlStr)
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
cmd.Connection = con;
return cmd;
}
//生成一個(gè)對象并返回該結(jié)果集第一行第一列
public object GetScalar(string sqlStr)
{
SqlCommand cmd = CreateCommand(sqlStr);
object obj = cmd.ExecuteScalar();
//CommadnBehavior.CloseConnection是將于DataReader的數(shù)據(jù)庫鏈接關(guān)聯(lián)起來
//當(dāng)關(guān)閉DataReader對象時(shí)候也自動關(guān)閉鏈接
return obj;
}
//執(zhí)行數(shù)據(jù)庫查詢并返回一個(gè)數(shù)據(jù)集 [當(dāng)前頁碼,每頁記錄條數(shù)]
public DataSet GetCurrentPage(int pageIndex, int pageSize)
{
//設(shè)置導(dǎo)入的起始地址
int firstPage = pageIndex * pageSize;
string sqlStr = "select * from jobs order by job_id desc";
SqlCommand cmd = CreateCommand(sqlStr);
DataSet dataset = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dataset,firstPage,pageSize,"jobs");
cmd.Dispose();
Close();
dataAdapter.Dispose();
return dataset;
}
//獲得查詢數(shù)據(jù)的總條數(shù)
public object GetAllCount()
{
string sqlStr = "select count(*) from jobs";
object obj = GetScalar(sqlStr);
return obj;
}
//關(guān)閉數(shù)據(jù)庫
public void Close()
{
if (con != null)
{
con.Close();
}
}
//釋放資源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}