Asp.Net数据分页技术全功略
假如有一张学生表,其包含的字段有:学号、姓名、性别、出生日期、年龄
第一种方法
HTML页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="Horizontal" onpageindexchanged="DataGrid1_PageIndexChanged"
PageSize="5" Width="600px">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#2461BF" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundColumn DataField="XH" HeaderText="学号"></asp:BoundColumn>
<asp:BoundColumn DataField="XM" HeaderText="姓名"></asp:BoundColumn>
<asp:BoundColumn DataField="XB" HeaderText="性别"></asp:BoundColumn>
<asp:BoundColumn DataField="CSSJ" HeaderText="出生日期"></asp:BoundColumn>
<asp:BoundColumn DataField="AGE" HeaderText="年龄"></asp:BoundColumn>
</Columns>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
<br />
<table border="1" class="style1" width="600">
<tr>
<td>
<asp:Button ID="top" runat="server" Text="首页" onclick="top_Click" />
</td>
<td>
<asp:Button ID="next" runat="server" Text="下一页" onclick="next_Click" />
</td>
<td>
<asp:Button ID="pre" runat="server" Text="上一页" onclick="pre_Click" />
</td>
<td>
<asp:Button ID="bottom" runat="server" Text="未记录" onclick="bottom_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
private const int PageCount = 3;
private static int TotalPageCount = 0;
private int RecordCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.DataGrid1.PageSize = PageCount;
this.DataGrid1.CurrentPageIndex = 0;
this.GetTotalPageCount();
TotalPageCount = this.RecordCount / PageCount;
if (TotalPageCount * PageCount < this.RecordCount)
{
TotalPageCount++;
}
BindDataGrid();
FirstLastPage();
}
}
private void GetTotalPageCount()
{
string sql = "select * from xs";
string sqlcon = WebConfigurationManager.ConnectionStrings["xscjConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.RecordCount++;
}
dr.Close();
}
private void BindDataGrid()
{
string sql = "select * from xs order by xh";
string sqlconnection = WebConfigurationManager.ConnectionStrings["xscjConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(sqlconnection);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
this.DataGrid1.DataSource = ds;
FirstLastPage();
this.DataGrid1.DataBind();
con.Close();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.BindDataGrid();
}
private void FirstLastPage()
{
if (TotalPageCount > 0)
{
if (this.DataGrid1.CurrentPageIndex == 0)
{
this.top.Enabled = false;
this.pre.Enabled = false;
this.next.Enabled = true;
this.bottom.Enabled = true;
}
if (this.DataGrid1.CurrentPageIndex == TotalPageCount - 1)
{
this.top.Enabled = true;
this.pre.Enabled = true;
this.next.Enabled = false;
this.bottom.Enabled = false;
}
if (this.DataGrid1.CurrentPageIndex != 0 && this.DataGrid1.CurrentPageIndex != TotalPageCount - 1)
{
this.top.Enabled = true;
this.pre.Enabled = true;
this.next.Enabled = true;
this.bottom.Enabled = true;
}
}
}
protected void top_Click(object sender, EventArgs e)
{
this.DataGrid1.CurrentPageIndex = 0;
this.BindDataGrid();
Response.Write(DataGrid1.CurrentPageIndex.ToString());
this.FirstLastPage();
}
protected void next_Click(object sender, EventArgs e)
{
this.DataGrid1.CurrentPageIndex = (int)Math.Min(TotalPageCount - 1, this.DataGrid1.CurrentPageIndex + 1); ;
this.BindDataGrid();
Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
this.FirstLastPage();
}
protected void pre_Click(object sender, EventArgs e)
{
this.DataGrid1.CurrentPageIndex = (int)Math.Max(0, this.DataGrid1.CurrentPageIndex - 1);
this.BindDataGrid();
Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
this.FirstLastPage();
}
protected void bottom_Click(object sender, EventArgs e)
{
this.DataGrid1.CurrentPageIndex = TotalPageCount - 1;
this.BindDataGrid();
Response.Write(this.DataGrid1.CurrentPageIndex.ToString());
this.FirstLastPage();
}
}