日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

為DataGrid的自帶分頁添加首頁、尾頁及狀態(tài)功能

系統(tǒng) 2039 0
DataGrid 提供了分頁功能,不過看上去功能有限,但是我們可以通過 DataGrid 的一些屬性來獲取狀態(tài)以及增加首頁、尾頁功能按鈕。這里沒有使用 DataGrid 的自定義分頁功能,如果在速度效率不是很講究的情況下,由 DataGrid 自己管理分頁還是不錯的,付出的代價就是要把整個相關(guān)數(shù)據(jù)取出來后再刪選指定頁的數(shù)據(jù)。好處就是開發(fā)速度快,不需要寫分頁的存儲過程。本文事例使用的是 Sql Server 中的 Northwind 數(shù)據(jù)庫。運行界面如下:
為DataGrid的自帶分頁添加首頁、尾頁及狀態(tài)功能
對于前臺的顯示界面,我放了一個
DataGrid ;四個 LinkButton 導(dǎo)向按鈕;四個 Literal 來顯示紀錄狀態(tài)。
剩下的就是用表格定位。
這里需要設(shè)置DataGrid的AllowPaging屬性為True,同時設(shè)置AllowCustomPaging屬性位false(默認為false),設(shè)置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繼承,在數(shù)據(jù)綁定時需要注意沒有數(shù)據(jù)的情況(0頁時),以及當頁數(shù)減少時避免前臺正在反頁導(dǎo)致缺頁。
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();
}
}
// 綁定數(shù)據(jù)
private void DataGridDataBind()
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
// 獲取當前的頁數(shù)
pageCount = ( int )Math.Ceiling( recordCount * 1.0 / PageSize);
// 避免紀錄從有到無時,并且已經(jīng)進行過反頁的情況下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 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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();
}
// 數(shù)據(jù)綁定
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>
/// 控制導(dǎo)航按鈕或數(shù)字的狀態(tài)
/// </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(); // 在有頁數(shù)的情況下前臺顯示頁數(shù)加1
this .LtlPageSize.Text = PageSize.ToString();
this .LtlRecordCount.Text = RecordCount.ToString();
}
// 總頁數(shù)
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 ;}
}
// 紀錄總數(shù)
public int RecordCount
{
get { return recordCount;}
set {recordCount = value ;}
}
}
}

為DataGrid的自帶分頁添加首頁、尾頁及狀態(tài)功能


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 顺昌县| 台东市| 新巴尔虎右旗| 沐川县| 马公市| 克拉玛依市| 咸阳市| 象州县| 沙洋县| 高平市| 错那县| 金阳县| 兴城市| 兴仁县| 东台市| 昔阳县| 梧州市| 湟中县| 阿城市| 舞钢市| 堆龙德庆县| 海阳市| 正安县| 托里县| 阿克苏市| 敦煌市| 鄂托克旗| 西峡县| 自治县| 塘沽区| 临猗县| 凤城市| 大同县| 镶黄旗| 五寨县| 清镇市| 上虞市| 和顺县| 兴义市| 常州市| 综艺|