分页有好多种,在以前写了存储过程,也用过pagedatasource,也自己写过代码,听好友说了aspnetpage,于是就试了试:
后台.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class MMM : System.Web.UI.Page
{
DataBase db = new DataBase();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
string strsql = "SELECT count([A_id]) FROM [Agent]";
SqlCommand myCommand = new SqlCommand(strsql, myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda = new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds = new DataSet();
myda.Fill(ds, "news");
this.AspNetPager1.RecordCount = System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);
BindData();
}
}
void BindData()
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
string strsql = "SELECT * FROM [Agent]";
SqlCommand myCommand = new SqlCommand(strsql, myConnection);
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myda = new SqlDataAdapter();
myda.SelectCommand = myCommand;
DataSet ds = new DataSet();
myda.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "news");
this.GridView1.DataSource = ds.Tables["news"];
this.GridView1.DataBind();
//动态设置用户自定义文本内容
AspNetPager1.CustomInfoHTML = "记录总数:<font color=/"blue/"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>";
AspNetPager1.CustomInfoHTML += " 总页数:<font color=/"blue/"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=/"red/"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindData();
}
}
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MMM.aspx.cs" Inherits="MMM" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Height="118px" Width="289px">
</asp:GridView>
<Webdiyer:AspNetPager id="AspNetPager1" AlwaysShow="true"
runat="server"
PageSize="8"
NumericButtonCount="8"
ShowCustomInfoSection="left"
PagingButtonSpacing="0"
ShowInputBox="always"
CssClass="mypager"
HorizontalAlign="right"
SubmitButtonText="转到"
NumericButtonTextFormatString="[{0}]" OnPageChanged="AspNetPager1_PageChanged" Width="168px"/>
</div>
</form>
</body>
</html>
当然要引用AspNetPager.dll了!