电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

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


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

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

(图一)

使用的数据源同上一篇文章(中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 * / @PageSize)

DECLARE @SQLSTR NVARCHAR()

IF @PageIndex = OR @PageCount <=

SET @SQLSTR =NSELECT TOP +STR( @PageSize )+

CustomerID CompanyNameAddressPhone FROM Customers ORDER BY CustomerID DESC

ELSE IF @PageIndex = @PageCount

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

CustomerID CompanyNameAddressPhone 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 CompanyNameAddressPhone FROM Customers ORDER BY CustomerID ASC ) TempTable ORDER BY CustomerID DESC

EXEC (@SQLSTR)

GO

下面就就把代码贴了一下

Aspx文件代码如下

<%@ Page language=c# Codebehind=DataGridCustomPagingaspxcs AutoEventWireup=false Inherits=ZZAspnetPagingDataGridCustomPaging %>

<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN >

<HTML>

<HEAD>

<title>DataGridPaging</title>

<meta content=Microsoft Visual Studio NET name=GENERATOR>

<meta content=C# name=CODE_LANGUAGE>

<meta content=JavaScript name=vs_defaultClientScript>

<meta content= name=vs_targetSchema>

</HEAD>

<body>

<form id=Form method=post runat=server>

<TABLE id=Table NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> cellSpacing= cellPadding= width= align=center

border=>

<TR>

<TD><asp:datagrid id=DataGrid runat=server AllowPaging=True AllowCustomPaging=True Width=%>

<FooterStyle FontSize=:chmetcnv TCSC= NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv>></FooterStyle>

<HeaderStyle FontSize=:chmetcnv TCSC= NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv>></HeaderStyle>

<PagerStyle Visible=False FontSize=:chmetcnv TCSC= NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> Mode=NumericPages></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD>

<TABLE id=Table NumberType= Negative=False HasSpace=False SourceValue= UnitName=pt w:st=on>pt:chmetcnv> cellSpacing= cellPadding= width=%

align=center border=>

<TR>

<TD ><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>

Aspxcs文件代码如下

using System;

using SystemCollections;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemWeb;

using SystemWebSessionState;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIHtmlControls;

using SystemDataSqlClient;

using SystemConfiguration;

namespace ZZAspnetPaging

{

public class DataGridCustomPaging : SystemWebUIPage

{

private int pageCount;

private int recordCount;

protected SystemWebUIWebControlsLinkButton LBtnFirst;

protected SystemWebUIWebControlsLinkButton LBtnPrev;

protected SystemWebUIWebControlsLinkButton LBtnNext;

protected SystemWebUIWebControlsLinkButton LBtnLast;

protected SystemWebUIWebControlsLiteral LtlPageIndex;

protected SystemWebUIWebControlsLiteral LtlPageCount;

protected SystemWebUIWebControlsLiteral LtlPageSize;

protected SystemWebUIWebControlsLiteral LtlRecordCount;

protected SystemWebUIWebControlsDataGrid DataGrid;

private void Page_Load(object sender SystemEventArgs e)

{

if(!PageIsPostBack)

{

DataGridDataBind();

}

}

//绑定数据

private void DataGridDataBind()

{

DataSet ds = GetCustomersData(PageIndexPageSizeref recordCountref pageCount);

thisDataGridVirtualItemCount = RecordCount;

thisDataGridDataSource = ds;

thisDataGridDataBind();

SetPagingState();

}

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

override protected void OnInit(EventArgs e)

{

InitializeComponent();

baseOnInit(e);

}

private void InitializeComponent()

{

thisLBtnFirstClick += new SystemEventHandler(thisLBtnNavigation_Click);

thisLBtnPrevClick += new SystemEventHandler(thisLBtnNavigation_Click);

thisLBtnNextClick += new SystemEventHandler(thisLBtnNavigation_Click);

thisLBtnLastClick += new SystemEventHandler(thisLBtnNavigation_Click);

thisLoad += new SystemEventHandler(thisPage_Load);

}

#endregion

private static DataSet GetCustomersData(int pageIndexint pageSizeref int recordCountref int pageCount)

{

string connString = ConfigurationSettingsAppSettings[ConnString];

SqlConnection conn = new SqlConnection(connString);

SqlCommand comm = new SqlCommand(GetCustomersDataPageconn);

commParametersAdd(new SqlParameter(@PageIndexSqlDbTypeInt));

commParameters[]Value = pageIndex;

commParametersAdd(new SqlParameter(@PageSizeSqlDbTypeInt));

commParameters[]Value = pageSize;

commParametersAdd(new SqlParameter(@RecordCountSqlDbTypeInt));

commParameters[]Direction = ParameterDirectionOutput;

commParametersAdd(new SqlParameter(@PageCountSqlDbTypeInt));

commParameters[]Direction = ParameterDirectionOutput;

commCommandType = CommandTypeStoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

DataSet ds = new DataSet();

dataAdapterFill(ds);

recordCount = (int)commParameters[]Value;

pageCount = (int)commParameters[]Value;

return ds;

}

private void LBtnNavigation_Click(object sender SystemEventArgs e)

{

LinkButton btn = (LinkButton)sender;

switch(btnCommandName)

{

case First:

PageIndex = ;

break;

case Prev://if( PageIndex > )

PageIndex = PageIndex ;

break;

case Next://if( PageIndex < PageCount )

PageIndex = PageIndex + ;

break;

case Last:

PageIndex = PageCount ;

break;

}

DataGridDataBind();

}

/// <summary>

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

/// </summary>

public void SetPagingState()

{

if( PageCount <= )//( RecordCount <= PageSize )//小于等于一页

{

thisLBtnFirstEnabled = false;

thisLBtnPrevEnabled = false;

thisLBtnNextEnabled = false;

thisLBtnLastEnabled = false;

}

else //有多页

{

if( PageIndex == )//当前为第一页

{

thisLBtnFirstEnabled = false;

thisLBtnPrevEnabled = false;

thisLBtnNextEnabled = true;

thisLBtnLastEnabled = true;

}

else if( PageIndex == PageCount )//当前为最后页

{

thisLBtnFirstEnabled = true;

thisLBtnPrevEnabled = true;

thisLBtnNextEnabled = false;

thisLBtnLastEnabled = false;

}

else //中间页

{

thisLBtnFirstEnabled = true;

thisLBtnPrevEnabled = true;

thisLBtnNextEnabled = true;

thisLBtnLastEnabled = true;

}

}

thisLtlPageSizeText = PageSizeToString();

thisLtlRecordCountText = RecordCountToString();

if(RecordCount == )

{

thisLtlPageCountText = ;

thisLtlPageIndexText = ;

}

else

{

thisLtlPageCountText = PageCountToString();

thisLtlPageIndexText = (PageIndex + )ToString();

}

}

public int PageCount

{

get

{

return thisDataGridPageCount;

}

}

public int PageSize

{

get

{

return thisDataGridPageSize;

}

}

public int PageIndex

{

get

{

return thisDataGridCurrentPageIndex;

}

set

{

thisDataGridCurrentPageIndex = value;

}

}

public int RecordCount

{

get

{

return recordCount;

}

}

}

}

上一篇:HashTable类

下一篇:微软推Windows在线版 接受互联时代洗礼