AspNetPager分頁控件使用方法


一、下載AspNetPager.dll
二、AspNetPager.dll復制於應用程序下的bin目錄,打開解決方案,引用dll文件
三、 在工具欄中添加控件,這樣可以支持拖拽使用
四、 要使用AspNetPager 要為其設置最基本的屬性
示例:

1、前台顯示界面代碼Default.aspx

<%@ 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,"osid")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"year1")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"month1")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"output1")%></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>
</div>
</form>
</body>
</html>
View Code

 

紅框為分頁相關代碼


2、Default.aspx.cs頁面的代碼

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 = 20;
Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
Repeater1.DataBind();
}
三、DBAccess.cs頁面的代碼
using System.Data.SqlClient;
public class DBAccess
{

private SqlConnection con;
private string DBName = "tongjinet";

//創建連接對象並打開
public void Open()
{
if (con == null)
con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);
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 outputsell order by osid desc";
SqlCommand cmd = CreateCommand(sqlStr);
DataSet dataset = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
dataAdapter.Fill(dataset, firstPage, pageSize, "outputsell");
cmd.Dispose();
Close();
dataAdapter.Dispose();
return dataset;
}
//獲得查詢數據的總條數
public object GetAllCount()
{
string sqlStr = "select count(*) from outputsell";
object obj = GetScalar(sqlStr);
return obj;
}

//關閉數據庫
public void Close()
{
if (con != null)
{
con.Close();
}
}
//釋放資源
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
}
View Code

代碼參考:http://www.cnblogs.com/taizhouxiaoba/archive/2009/03/23/1419822.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM