寫的不算好
僅供參考
GridPager
ascx:
<%@ Control Language=
c#
AutoEventWireup=
false
Codebehind=
GridPager
ascx
cs
Inherits=
Test
BaseClass
GridPager
TargetSchema=
%><?xml:namespace prefix = asp /><asp:linkbutton id=lbtnPre runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
Enabled=
False
Font
Size=
Small
>
</asp:linkbutton> <asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnPage
runat=
server
Text=
Visible=
False
></asp:linkbutton><asp:linkbutton id=lbtnNext runat=
server
Text=
Visible=
False
></asp:linkbutton>
///////////////////////////////////////////////////////////////////////////////////////
GridPager
ascx
cs:
using System;
using System
Data;
using System
Drawing;
using System
Web;
using System
Web
UI
WebControls;
namespace Test
BaseClass
{
public class GridPager : System
Web
UI
UserControl
{
#region 類變量
protected System
Web
UI
WebControls
LinkButton lbtnNext;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPage
;
protected System
Web
UI
WebControls
LinkButton lbtnPre;
#endregion
#region
/// <summary>
/// 編號
/// 內容摘要
翻頁的對象
DataGrid控件
/// </summary>
public DataGrid TheGrid;
/// <summary>
/// 編號
/// 內容摘要
顯示查詢結果記錄數的標簽
可以不設置
表示無需顯示
/// </summary>
public Label LabelRowsCount;
/// <summary>
/// 編號
/// 內容摘要
顯示查詢結果總頁數的標簽
可以不設置
表示無需顯示
/// </summary>
public Label LabelPageCount;
/// <summary>
/// 編號
/// 內容摘要
顯示當前頁碼的標簽
可以不設置
表示無需顯示
/// </summary>
public Label LabelPageIndex;
/// <summary>
/// 編號
/// 內容摘要
查詢數據的SQL語句
/// </summary>
private string SelectSQL
{
get
{
if(ViewState[this
ClientID+
_SelectSQL
] != null)
return ViewState[this
ClientID+
_SelectSQL
]
ToString();
else
return string
Empty;
}
set
{
ViewState[this
ClientID+
_SelectSQL
] = value;
}
}//SelectSQL結束
/// <summary>
/// 編號
/// 內容摘要
每頁顯示的行數
/// </summary>
public int PageSizes
{
get
{
if(ViewState[this
ClientID+
_PageSizes
] != null)
return (int)ViewState[this
ClientID+
_PageSizes
];
else
return
;
}
set
{
ViewState[this
ClientID+
_PageSizes
] = value;
}
}//PageSizes結束
/// <summary>
/// 編號
/// 內容摘要
總頁數
/// </summary>
private int PageCount
{
get
{
if(ViewState[this
ClientID+
_PageCount
] != null)
return (int)ViewState[this
ClientID+
_PageCount
];
else
return
;
}
set
{
ViewState[this
ClientID+
_PageCount
] = value;
}
}//PageCount結束
/// <summary>
/// 編號
/// 內容摘要
當前頁碼
/// </summary>
private int CurrentPageIndex
{
get
{
if(ViewState[this
ClientID+
_CurrentPageIndex
] != null)
return (int)ViewState[this
ClientID+
_CurrentPageIndex
];
else
return
;
}
set
{
ViewState[this
ClientID+
_CurrentPageIndex
] = value;
if(this
LabelPageIndex!= null)
this
LabelPageIndex
Text = (value+
)
ToString();
}
}//CurrentPageIndex結束
private LinkButton[] PageButtons
{
get
{
return new LinkButton[]{lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
lbtnPage
};
}
}
#endregion
#region 方法區開始
/// <summary>
/// 方法編號
/// 內容摘要
初始化頁面
/// </summary>
private void Page_Load(object sender
System
EventArgs e)
{
}
/// <summary>
/// 方法編號
/// 內容摘要
設置與本控件關聯的控件
/// </summary>
/// <param name=
mDataGrid
>DataGrid控件
必須提供</param>
/// <param name=
lblRowsCount
>顯示查詢結果記錄數的標簽
可以輸入null
表示無需顯示</param>
/// <param name=
lblPageCount
>顯示查詢結果總頁數的標簽
可以輸入null
表示無需顯示</param>
/// <param name=
lblPageIndex
>顯示當前頁碼的標簽
可以輸入null
表示無需顯示</param>
public void InitPagerControls(DataGrid mDataGrid
Label lblRowsCount
Label lblPageCount
Label lblPageIndex)
{
mDataGrid
AllowPaging = false;
this
TheGrid = mDataGrid;
this
LabelRowsCount = lblRowsCount;
this
LabelPageCount = lblPageCount;
this
LabelPageIndex = lblPageIndex;
}
/// <summary>
/// 方法編號
/// 內容摘要
改變查詢條件重新查詢數據
/// </summary>
/// <param name=
selectSQL
>查詢的SQL語句
翻頁的時候始終使用該語句獲得數據集
直到下一次調用本方法</param>
public void LoadData(string selectSQL)
{
//保存新的SQL語句
this
SelectSQL = selectSQL;
//統計符合條件的數據的條數
int mRowCount =
;
string tmpSQL =
SELECT COUNT(*) FROM (
+ selectSQL +
) t
;
Database db = new Database();
System
Data
OleDb
OleDbDataReader tmpDr = db
GetDataReaderFromSQL(tmpSQL);
if(tmpDr
Read())
mRowCount = Convert
ToInt
(tmpDr[
]);
//釋放數據連接
tmpDr
Close();
db
Dispose();
//如果需要則顯示數據總行數
if(this
LabelRowsCount!=null)
this
LabelRowsCount
Text = mRowCount
ToString();
//統計總頁數
如果需要則顯示總頁數
this
PageCount = (int)Math
Ceiling(((double)mRowCount)/((double)PageSizes));
if(this
LabelPageCount != null)
this
LabelPageCount
Text = this
PageCount
ToString();
//DataGrid的當前頁回到第一頁
按鈕也返回第一頁的狀態
this
CurrentPageIndex =
;
this
ChangePageButtons(
);
//查詢第一頁數據
this
lbtnPages_Click(this
PageButtons[
]
null);
}
/// <summary>
/// 方法編號
/// 內容摘要
查詢符合條件的數據並綁定DataGrid顯示
/// </summary>
private void BindData()
{
string selectSQL = this
SelectSQL;
if(selectSQL != string
Empty)
{
Database db = new Database();//這是我自己的一個數據操作類
int rowStart = (CurrentPageIndex * PageSizes) +
;//當前頁的起始序號
int rowEnd = (CurrentPageIndex+
) * PageSizes;//當前頁的結束序號
string querySQL =
SELECT * FROM (SELECT t
*
rownum sn FROM (
+ selectSQL +
) t
) t
+
WHERE t
sn BETWEEN
+rowStart
ToString()+
AND
+ rowEnd
ToString();
//查詢數據庫讀取符合條件的數據
DataTable tb = db
GetDataTableFromSQL(querySQL);
//綁定DataGrid顯示
TheGrid
DataSource = tb;
TheGrid
DataBind();
db
Dispose();
}
}
/// <summary>
/// 方法編號
/// 內容摘要
改變翻頁按鈕的狀態
如顯示
的頁碼
/// </summary>
private void ChangePageButtons(int mIndex)
{
int tmpRem =
;
int tmpDiv = Math
DivRem(this
PageCount
out tmpRem);
if(tmpRem ==
) tmpRem=
;
//所有按鈕恢復默認的狀態
for(int i =
;i<
;i++)
{
int pageNum = mIndex*
+ i +
;
this
PageButtons[i]
Text = pageNum
ToString();
this
PageButtons[i]
Visible = true;
this
PageButtons[i]
Enabled = true;
this
PageButtons[i]
Font
Size = System
Web
UI
WebControls
FontUnit
XSmall;
}//end of for(int i =
;i<
;i++)
this
lbtnPre
Visible = true;
this
lbtnNext
Visible = true;
//如果目前是
~
頁
那麼不顯示
前面的
if(mIndex ==
)
lbtnPre
Visible = false;
//如果目前是最後的一頁
則不顯示後面的
並且保證顯示的翻頁按鈕不超過最大頁數
if(mIndex == tmpDiv)
{
lbtnNext
Visible = false;
for(int i =
;i>tmpRem
;i=i
)
this
PageButtons[i]
Visible = false;
}
}
/// <summary>
/// 方法編號
/// 內容摘要
顯示前十個頁碼
/// </summary>
private void lbtnPre_Click(object sender
System
EventArgs e)
{
this
ChangePageButtons((int)Math
Floor(Math
Max(this
CurrentPageIndex
)/
));
this
lbtnPages_Click(this
PageButtons[
]
null);
}
/// <summary>
/// 方法編號
/// 內容摘要
顯示後十個頁碼
/// </summary>
private void lbtnNext_Click(object sender
System
EventArgs e)
{
this
ChangePageButtons((int)Math
Floor(Math
Max(this
CurrentPageIndex +
)/
));
this
lbtnPages_Click(this
PageButtons[
]
null);
}
/// <summary>
/// 方法編號
/// 內容摘要
查詢綁定新的一頁
/// </summary>
private void lbtnPages_Click(object sender
System
EventArgs e)
{
LinkButton tmpLBtn = sender as LinkButton;
int pageIndex = Convert
ToInt
(tmpLBtn
Text
Trim())
;
this
CurrentPageIndex = pageIndex;
foreach(LinkButton mLBtn in this
PageButtons)
{
mLBtn
Enabled = true;
mLBtn
Font
Size = System
Web
UI
WebControls
FontUnit
XSmall;
}
tmpLBtn
Enabled = false;
tmpLBtn
Font
Size = System
Web
UI
WebControls
FontUnit
Small;
this
BindData();
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP
NET Web 窗體設計器所必需的
//
InitializeComponent();
base
OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法
不要使用代碼編輯器
/// 修改此方法的內容
/// </summary>
private void InitializeComponent()
{
this
lbtnPre
Click += new System
EventHandler(this
lbtnPre_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnPage
Click += new System
EventHandler(this
lbtnPages_Click);
this
lbtnNext
Click += new System
EventHandler(this
lbtnNext_Click);
this
Load += new System
EventHandler(this
Page_Load);
}
#endregion
#endregion
From:http://tw.wingwit.com/Article/program/Oracle/201311/17308.html