具体的思路就先用AspNetPager读取数据库里内容有多少,也就是有几条
然后再用datalist去利用AspNetPager的属性去取数据库里的东西这样就
实现了分页效果,现在做网站挺使用的,主流,您一定要主要红字的部分
前台页面代码:Page.aspx
<%@ Page Language="C#" CodeFile="page.aspx.cs" Inherits="page" %>
<%@ 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>Datalist分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="list" runat="server">
<ItemTemplate>
<%# DataBinder.eval_r(Container.DataItem,"About_Title") %>
</ItemTemplate>
</asp:DataList>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" firstpagetext="首页" lastpagetext="尾页" nextpagetext="下一页" numericbuttoncount="4" pageindexboxtype="DropDownList" prevpagetext="上一页" showcustominfosection="Right" showpageindexbox="Always" submitbuttontext="Go" textafterpageindexbox="页" textbeforepageindexbox="转到" AlwaysShow="True" CustomInfoHTML="第%CurrentPageIndex%页,共%PageCount%页,每页%PageSize%条" LayoutType="Table" CssClass="paginator" CustomInfoClass="paginator" Height="20px" CustomInfoSectionWidth="" Wrap="False" PageSize="3" OnPageChanging="AspNetPager1_PageChanging">
</webdiyer:AspNetPager>
</div>
</form>
</body>
</html>
注:不要忘记红色部分
后台页面:
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;
using Wuqi.Webdiyer;//别忘记命名空间
public partial class page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("server=.; uid=sa; pwd=1; database=liuyanban;");
con.Open();
SqlCommand cmd = new SqlCommand("select count(*)from info", con);
AspNetPager1.RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
//读取数据库里有几条内容
con.Close();
DataBindlist();
}
}
protected void DataBindlist()
{
SqlConnection con = new SqlConnection("server=.; uid=sa; pwd=1; database=liuyanban;");
SqlDataAdapter ad = new SqlDataAdapter("select * from info", con);
DataSet ds = new DataSet();
ad.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "About");
liuyanban.DataSource = ds.Tables["About"];
liuyanban.DataBind();
}
protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
DataBindlist();
}}