DataGrid
提供了分頁功能,不過看上去功能有限,但是我們可以通過
DataGrid
的一些屬性來獲取狀態以及增加首頁、尾頁功能按鈕。這里沒有使用
DataGrid
的自定義分頁功能,如果在速度效率不是很講究的情況下,由
DataGrid
自己管理分頁還是不錯的,付出的代價就是要把整個相關數據取出來后再刪選指定頁的數據。好處就是開發速度快,不需要寫分頁的存儲過程。本文事例使用的是
Sql Server
中的
Northwind
數據庫。運行界面如下:
剩下的就是用表格定位。
這里需要設置DataGrid的AllowPaging屬性為True,同時設置AllowCustomPaging屬性位false(默認為false),設置PagerStyle的Visible屬性為False,使前臺不顯示。
前臺的代碼如下:
<%@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridPaging" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGridPaging</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"
border="1">
<TR>
<TD><asp:datagrid id="DataGrid1" runat="server" PageSize="5" Width="100%" AllowPaging="True">
<HeaderStyle Font-Size="9pt"></HeaderStyle>
<FooterStyle Font-Size="9pt"></FooterStyle>
<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
</TABLE>
<TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"
border="1">
<TR>
<TD style="WIDTH: 207px">
<asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">
首頁</asp:linkbutton>
<asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">
上一頁</asp:linkbutton>
<asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">
下一頁</asp:linkbutton>
<asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">
尾頁</asp:linkbutton> </TD>
<TD>
第
<asp:literal id="LtlPageIndex" runat="server"></asp:literal>
頁 共
<asp:literal id="LtlPageCount" runat="server"></asp:literal>
頁 每頁
<asp:literal id="LtlPageSize" runat="server"></asp:literal>
條 共
<asp:literal id="LtlRecordCount" runat="server"></asp:literal>
條
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
后臺
cs
文件代碼,
DataGridPaging
類從System.Web.UI.Page繼承,在數據綁定時需要注意沒有數據的情況(0頁時),以及當頁數減少時避免前臺正在反頁導致缺頁。
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
using
System.Configuration;
namespace
ZZ.AspnetPaging
{
public
class
DataGridPaging : System.Web.UI.Page
{
private
static
string
connString = ConfigurationSettings.AppSettings["ConnString"];
private
int
recordCount;
private
int
pageCount;
protected
System.Web.UI.WebControls.LinkButton LBtnFirst;
protected
System.Web.UI.WebControls.LinkButton LBtnPrev;
protected
System.Web.UI.WebControls.LinkButton LBtnNext;
protected
System.Web.UI.WebControls.LinkButton LBtnLast;
protected
System.Web.UI.WebControls.Literal LtlPageIndex;
protected
System.Web.UI.WebControls.Literal LtlPageCount;
protected
System.Web.UI.WebControls.Literal LtlPageSize;
protected
System.Web.UI.WebControls.Literal LtlRecordCount;
protected
System.Web.UI.WebControls.DataGrid DataGrid1;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
if
(!Page.IsPostBack)
{
DataGridDataBind();
}
}
//
綁定數據
private
void
DataGridDataBind()
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
//
獲取當前的頁數
pageCount = (
int
)Math.Ceiling( recordCount * 1.0 / PageSize);
//
避免紀錄從有到無時,并且已經進行過反頁的情況下CurrentPageIndex > PageCount出錯
if
(recordCount ==0)
{
this
.DataGrid1.CurrentPageIndex = 0;
}
else
if
(
this
.DataGrid1.CurrentPageIndex >= pageCount)
{
this
.DataGrid1.CurrentPageIndex = pageCount - 1;
}
this
.DataGrid1.DataSource = ds;
this
.DataGrid1.DataBind();
NavigationStateChange();
}
#region
Web
窗體設計器生成的代碼
override
protected
void
OnInit(EventArgs e)
{
//
// CODEGEN:
該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
///
<summary>
///
設計器支持所需的方法 - 不要使用代碼編輯器修改
///
此方法的內容。
///
</summary>
private
void
InitializeComponent()
{
this
.LBtnFirst.Click +=
new
System.EventHandler(
this
.LBtnNavigation_Click);
this
.LBtnPrev.Click +=
new
System.EventHandler(
this
.LBtnNavigation_Click);
this
.LBtnNext.Click +=
new
System.EventHandler(
this
.LBtnNavigation_Click);
this
.LBtnLast.Click +=
new
System.EventHandler(
this
.LBtnNavigation_Click);
this
.Load +=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
LBtnNavigation_Click(
object
sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch
(btn.CommandName)
{
case
"First":
PageIndex = 0;
break
;
case
"Prev":
//if( PageIndex > 0 )
PageIndex = PageIndex - 1;
break
;
case
"Next":
//if( PageIndex < PageCount -1)
PageIndex = PageIndex + 1;
break
;
case
"Last":
PageIndex = PageCount - 1;
break
;
}
DataGridDataBind();
}
//
數據綁定
public
static
DataSet GetCustomersData()
{
SqlConnection conn =
new
SqlConnection(connString);
string
sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
SqlCommand comm =
new
SqlCommand( sqlStr ,conn);
SqlDataAdapter dataAdapter =
new
SqlDataAdapter(comm);
DataSet ds =
new
DataSet();
dataAdapter.Fill(ds);
return
ds;
}
///
<summary>
///
控制導航按鈕或數字的狀態
///
</summary>
public
void
NavigationStateChange()
{
if
( PageCount <= 1 )
//( RecordCount <= PageSize )//
小于等于一頁
{
this
.LBtnFirst.Enabled =
false
;
this
.LBtnPrev.Enabled =
false
;
this
.LBtnNext.Enabled =
false
;
this
.LBtnLast.Enabled =
false
;
}
else
//
有多頁
{
if
( PageIndex == 0 )
//
當前為第一頁
{
this
.LBtnFirst.Enabled =
false
;
this
.LBtnPrev.Enabled =
false
;
this
.LBtnNext.Enabled =
true
;
this
.LBtnLast.Enabled =
true
;
}
else
if
( PageIndex == PageCount - 1 )
//
當前為最后頁
{
this
.LBtnFirst.Enabled =
true
;
this
.LBtnPrev.Enabled =
true
;
this
.LBtnNext.Enabled =
false
;
this
.LBtnLast.Enabled =
false
;
}
else
//
中間頁
{
this
.LBtnFirst.Enabled =
true
;
this
.LBtnPrev.Enabled =
true
;
this
.LBtnNext.Enabled =
true
;
this
.LBtnLast.Enabled =
true
;
}
}
if
(RecordCount == 0)
//
當沒有紀錄時DataGrid.PageCount會顯示1頁
this
.LtlPageCount.Text = "0";
else
this
.LtlPageCount.Text = PageCount.ToString();
if
(RecordCount == 0)
this
.LtlPageIndex.Text = "0";
else
this
.LtlPageIndex.Text = (PageIndex + 1).ToString();
//
在有頁數的情況下前臺顯示頁數加1
this
.LtlPageSize.Text = PageSize.ToString();
this
.LtlRecordCount.Text = RecordCount.ToString();
}
//
總頁數
public
int
PageCount
{
get
{
return
this
.DataGrid1.PageCount;}
}
//
頁大小
public
int
PageSize
{
get
{
return
this
.DataGrid1.PageSize;}
}
//
頁索引,從零開始
public
int
PageIndex
{
get
{
return
this
.DataGrid1.CurrentPageIndex;}
set
{
this
.DataGrid1.CurrentPageIndex =
value
;}
}
//
紀錄總數
public
int
RecordCount
{
get
{
return
recordCount;}
set
{recordCount =
value
;}
}
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
