日期:2012-04-16  浏览次数:20390 次

DataGrid提供了分页功能,不过看上去功能有限,但是我们可以通过DataGrid的一些属性来获取状态以及增加首页、尾页功能按钮。这里没有使用DataGrid的自定义分页功能,如果在速度效率不是很讲究的情况下,由DataGrid自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。本文事例使用的是Sql Server中的Northwind数据库。运行界面如下:



对于前台的显示界面,我放了一个DataGrid;四个LinkButton导向按钮;四个Literal来显示纪录状态。

剩下的就是用表格定位。

这里需要设置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&