分享
 
 
 

为DataGrid自定义分页添加自定义导航和分页信息

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

在上一篇文章中我讲到了对DataGrid实行自定义分页,这可以避免为了显示一页数据而获取整个数据记录集,从而提高分页效率,不过使用的导航还是DataGrid自带的数字连接或简单的上一页,下一页,而且看不到总页数、总记录数之类的信息。下面就为他增加我们所需要的部分。

先来看看修改后的分页显示,截图如下:

(图一)

使用的数据源同上一篇文章(Asp.net中DataGrid控件的自定义分页)相同,都是访问Northwind库,为了独立开来这里还是把存储过程列了一下,

CREATE PROCEDURE [GetCustomersDataPage]

@PageIndex INT,

@PageSize INT,

@RecordCount INT OUT,

@PageCount INT OUT

AS

SELECT @RecordCount = COUNT(*) FROM Customers

SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)

DECLARE @SQLSTR NVARCHAR(1000)

IF @PageIndex = 0 OR @PageCount <= 1

SET @SQLSTR =N'SELECT TOP '+STR( @PageSize )+

' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID DESC

ELSE IF @PageIndex = @PageCount - 1

SET @SQLSTR =N' SELECT * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'

ELSE

SET @SQLSTR =N' SELECT TOP '+STR( @PageSize )+' * FROM ( SELECT TOP '+STR( @RecordCount - @PageSize * @PageIndex )+

' CustomerID, CompanyName,Address,Phone FROM Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC'

EXEC (@SQLSTR)

GO

下面就就把代码贴了一下,

Aspx文件代码如下:

<%@ Page language="c#" Codebehind="DataGridCustomPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridCustomPaging" %>

<!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" AllowPaging="True" AllowCustomPaging="True" Width="100%">

<FooterStyle Font-Size="9pt"></FooterStyle>

<HeaderStyle Font-Size="9pt"></HeaderStyle>

<PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD>

<TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="100%"

align="center" border="1">

<TR>

<TD style="WIDTH: 150px"><asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">首页</asp:linkbutton>&nbsp;

<asp:linkbutton id="LBtnPrev" runat="server" CommandName="Prev">上一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">下一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">尾页</asp:linkbutton></TD>

<TD>第<asp:literal id="LtlPageIndex" runat="server"></asp:literal>页&nbsp; 共<asp:literal id="LtlPageCount" runat="server"></asp:literal>页&nbsp;

每页<asp:Literal id="LtlPageSize" runat="server"></asp:Literal>条&nbsp; 共<asp:Literal id="LtlRecordCount" runat="server"></asp:Literal>条&nbsp;

</TD>

</TR>

</TABLE>

</TD>

</TR>

</TABLE>

</form>

</body>

</HTML>

Aspx.cs文件代码如下:

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 DataGridCustomPaging : System.Web.UI.Page

{

private int pageCount;

private int recordCount;

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(PageIndex,PageSize,ref recordCount,ref pageCount);

this.DataGrid1.VirtualItemCount = RecordCount;

this.DataGrid1.DataSource = ds;

this.DataGrid1.DataBind();

SetPagingState();

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

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 static DataSet GetCustomersData(int pageIndex,int pageSize,ref int recordCount,ref int pageCount)

{

string connString = ConfigurationSettings.AppSettings["ConnString"];

SqlConnection conn = new SqlConnection(connString);

SqlCommand comm = new SqlCommand("GetCustomersDataPage",conn);

comm.Parameters.Add(new SqlParameter("@PageIndex",SqlDbType.Int));

comm.Parameters[0].Value = pageIndex;

comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));

comm.Parameters[1].Value = pageSize;

comm.Parameters.Add(new SqlParameter("@RecordCount",SqlDbType.Int));

comm.Parameters[2].Direction = ParameterDirection.Output;

comm.Parameters.Add(new SqlParameter("@PageCount",SqlDbType.Int));

comm.Parameters[3].Direction = ParameterDirection.Output;

comm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

DataSet ds = new DataSet();

dataAdapter.Fill(ds);

recordCount = (int)comm.Parameters[2].Value;

pageCount = (int)comm.Parameters[3].Value;

return ds;

}

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();

}

/// <summary>

/// 控制导航按钮或数字的状态

/// </summary>

public void SetPagingState()

{

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;

}

}

this.LtlPageSize.Text = PageSize.ToString();

this.LtlRecordCount.Text = RecordCount.ToString();

if(RecordCount == 0)

{

this.LtlPageCount.Text = "0";

this.LtlPageIndex.Text = "0";

}

else

{

this.LtlPageCount.Text = PageCount.ToString();

this.LtlPageIndex.Text = (PageIndex + 1).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;

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有