注: 如果只想让第一页、上一页等四个按钮为图片按钮而页索引和更多页链接为文本,则还应将NumericButtonType和MoreButtonType设为text。
在class.cs调用时,首先引入命名空间, 再把对象实例化.
如: AspNetPager anp = new AspNetPager();
ada.Fill(ds, anp.PageSize * (anp.CurrentPageIndex - 1), anp.PageSize,"name"); //name为表名
===============================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<%@ Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<Webdiyer:AspNetPager id="AspNetPager1"
runat="server"
PageSize="8"
NumericButtonCount="8"
ShowCustomInfoSection="left"
PagingButtonSpacing="0"
ShowInputBox="always"
CssClass="mypager"
HorizontalAlign="right"
SubmitButtonText="转到"
NumericButtonTextFormatString="[{0}]" OnPageChanged="AspNetPager1_PageChanged"/>
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label><br />
news_title:
<asp:Label ID="news_titleLabel" runat="server" Text='<%# Eval("news_title") %>'>
</asp:Label><br />
news_class:
<asp:Label ID="news_classLabel" runat="server" Text='<%# Eval("news_class") %>'>
</asp:Label><br />
news_pubdate:
<asp:Label ID="news_pubdateLabel" runat="server" Text='<%# Eval("news_pubdate") %>'>
</asp:Label><br />
news_content:
<asp:Label ID="news_contentLabel" runat="server" Text='<%# Eval("news_content") %>'>
</asp:Label><br />
news_author:
<asp:Label ID="news_authorLabel" runat="server" Text='<%# Eval("news_author") %>'>
</asp:Label><br />
news_input:
<asp:Label ID="news_inputLabel" runat="server" Text='<%# Eval("news_input") %>'>
</asp:Label><br />
news_senddate:
<asp:Label ID="news_senddateLabel" runat="server" Text='<%# Eval("news_senddate") %>'>
</asp:Label><br />
news_from:
<asp:Label ID="news_fromLabel" runat="server" Text='<%# Eval("news_from") %>'></asp:Label><br />
news_file:
<asp:Label ID="news_fileLabel" runat="server" Text='<%# Eval("news_file") %>'></asp:Label><br />
news_filetype:
<asp:Label ID="news_filetypeLabel" runat="server" Text='<%# Eval("news_filetype") %>'>
</asp:Label><br />
news_type:
<asp:Label ID="news_typeLabel" runat="server" Text='<%# Eval("news_type") %>'></asp:Label><br />
news_hits:
<asp:Label ID="news_hitsLabel" runat="server" Text='<%# Eval("news_hits") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>
==============================================================================
==============================================================================
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strConn = "Data Source=localhost;Initial Catalog=zcls;Persist Security Info=True;User ID=sa;Password=windows";
SqlConnection myConnection = new SqlConnection(strConn);
string strsql = "SELECT count([id]) FROM [news] where id>6000";
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()
{
string strConn = "Data Source=localhost;Initial Catalog=zcls;Persist Security Info=True;User ID=sa;Password=windows";
SqlConnection myConnection = new SqlConnection(strConn);
string strsql = "SELECT * FROM [news] where id>6000";
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.DataList2.DataSource = ds.Tables["news"];
this.DataList2.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)
{
//
//AspNetPager1.CurrentPageIndex = ((Wuqi.Webdiyer.PageChangedEventArgs)e).NewPageIndex;
BindData();
}
}