效果图如下:
前台代码:
<%&#64; Page Language&#61;"C#" AutoEventWireup&#61;"true" CodeFile&#61;"01.aspx.cs" Inherits&#61;"_01" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns&#61;"http://www.w3.org/1999/xhtml">
<head runat&#61;"server">
<title>title>
<link href&#61;"css/css.css" rel&#61;"stylesheet" type&#61;"text/css" />
head>
<body>
<form id&#61;"form1" runat&#61;"server">
<div>
<asp:Repeater ID&#61;"rptTest" runat&#61;"server">
<HeaderTemplate><table class&#61;"border">HeaderTemplate>
<ItemTemplate><tr><td class&#61;"td"><%#Eval("id")%>td><td class&#61;"td"><%#Eval("title")%>td><td class&#61;"td"><%#Eval("usercode")%>td>tr>ItemTemplate>
<FooterTemplate>table>FooterTemplate>
asp:Repeater>
div>
<br />
<div id&#61;"wayduPager">
第<asp:Label ID&#61;"lblPageIndex" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>页/总<asp:Label ID&#61;"lblMaxPage" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>页 | 每页<asp:Label ID&#61;"lblPageSize" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>条 总<asp:Label ID&#61;"lblTotalCount" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>条 | <asp:LinkButton ID&#61;"lnkFirst" OnClick&#61;"lnkFirst_Click" runat&#61;"server">首 页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkPrev" OnClick&#61;"lnkPrev_Click" runat&#61;"server">上一页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkNext" OnClick&#61;"lnkNext_Click" runat&#61;"server">下一页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkLast" OnClick&#61;"lnkLast_Click" runat&#61;"server">尾 页asp:LinkButton> | 转到&#xff1a;第<asp:DropDownList ID&#61;"ddlselectPage" runat&#61;"server" AutoPostBack&#61;"True" OnSelectedIndexChanged&#61;"ddlselectPage_SelectedIndexChanged">asp:DropDownList>页
div>
form>
body>
html>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns&#61;"http://www.w3.org/1999/xhtml">
<head runat&#61;"server">
<title>title>
<link href&#61;"css/css.css" rel&#61;"stylesheet" type&#61;"text/css" />
head>
<body>
<form id&#61;"form1" runat&#61;"server">
<div>
<asp:Repeater ID&#61;"rptTest" runat&#61;"server">
<HeaderTemplate><table class&#61;"border">HeaderTemplate>
<ItemTemplate><tr><td class&#61;"td"><%#Eval("id")%>td><td class&#61;"td"><%#Eval("title")%>td><td class&#61;"td"><%#Eval("usercode")%>td>tr>ItemTemplate>
<FooterTemplate>table>FooterTemplate>
asp:Repeater>
div>
<br />
<div id&#61;"wayduPager">
第<asp:Label ID&#61;"lblPageIndex" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>页/总<asp:Label ID&#61;"lblMaxPage" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>页 | 每页<asp:Label ID&#61;"lblPageSize" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>条 总<asp:Label ID&#61;"lblTotalCount" runat&#61;"server" Text&#61;"1" ForeColor&#61;"Red">asp:Label>条 | <asp:LinkButton ID&#61;"lnkFirst" OnClick&#61;"lnkFirst_Click" runat&#61;"server">首 页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkPrev" OnClick&#61;"lnkPrev_Click" runat&#61;"server">上一页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkNext" OnClick&#61;"lnkNext_Click" runat&#61;"server">下一页asp:LinkButton> | <asp:LinkButton ID&#61;"lnkLast" OnClick&#61;"lnkLast_Click" runat&#61;"server">尾 页asp:LinkButton> | 转到&#xff1a;第<asp:DropDownList ID&#61;"ddlselectPage" runat&#61;"server" AutoPostBack&#61;"True" OnSelectedIndexChanged&#61;"ddlselectPage_SelectedIndexChanged">asp:DropDownList>页
div>
form>
body>
html>
后台代码&#xff1a;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindList();
}
}
#region 绑定数据源
private void BindList()
{
string sql &#61; " select id,title,memberid,(select usercode from t_mem_memberinfo where memberid&#61;a.memberid) as usercode from t_blog a order by id desc ";
DataSet ds &#61; sqlDatabase.getDS(sql);
//-----------------------分页代码-----------------------//
PagedDataSource Pds &#61; new PagedDataSource();
Pds.DataSource &#61; ds.Tables[0].DefaultView;
Pds.AllowPaging &#61; true; //开启分页
Pds.PageSize &#61; 15; //分页
int index &#61; int.Parse(lblPageIndex.Text); //当前页数
int PageSize &#61; Pds.PageSize; //每页记录数
int TotalRecords &#61; Pds.DataSourceCount; //总记录数
int TotalPages &#61; TotalRecords / PageSize;//总页数&#xff08;总记录数除以每页的记录数&#xff09;
int ModePages &#61; TotalRecords % PageSize;//得到取模的余数(总记录数取模每页的记录数)
if (ModePages > 0)//如果取模数不等于0&#xff0c;则把总页数加1
{
TotalPages &#43;&#61; 1;
}
if (TotalPages &#61;&#61; 0)
{
TotalPages &#61; 1;
}
this.ViewState["TotalPages"] &#61; TotalPages; //保存"总页数"到页面视图中
lblMaxPage.Text &#61; TotalPages.ToString(); //显示页面上的总页数
lblTotalCount.Text &#61; TotalRecords.ToString(); //显示页面上的总记录数
lblPageSize.Text &#61; Pds.PageSize.ToString(); //每页记录数
Pds.CurrentPageIndex &#61; index - 1;
//若是"第一页"
if (index &#61;&#61; 1)
{
lnkFirst.Enabled &#61; false;
lnkPrev.Enabled &#61; false;
if (TotalPages > 1)
{
lnkNext.Enabled &#61; true;
lnkLast.Enabled &#61; true;
}
else
{
lnkNext.Enabled &#61; false;
lnkLast.Enabled &#61; false;
}
}
else
{
lnkFirst.Enabled &#61; true;
lnkPrev.Enabled &#61; true;
//若是"尾页"
if (index &#61;&#61; TotalPages)
{
lnkNext.Enabled &#61; false;
lnkLast.Enabled &#61; false;
}
else
{
lnkNext.Enabled &#61; true;
lnkLast.Enabled &#61; true;
}
}
ddlselectPage.Items.Clear(); //清除防止累加
for (int i &#61; 1; i <&#61; TotalPages; i&#43;&#43;)
{
ddlselectPage.Items.Add(i.ToString());
}
ddlselectPage.Items.FindByText(index.ToString()).Selected &#61; true; //把当前页显示在列表框的第一个
this.rptTest.DataSource &#61; Pds;
this.rptTest.DataBind();
}
#endregion
#region "首页"事件
protected void lnkFirst_Click(object sender, EventArgs e)
{
int index &#61; 1;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "上一页"事件
protected void lnkPrev_Click(object sender, EventArgs e)
{
int index &#61; int.Parse(lblPageIndex.Text);
index--;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "下一页"事件
protected void lnkNext_Click(object sender, EventArgs e)
{
int index &#61; int.Parse(lblPageIndex.Text);
index&#43;&#43;;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "尾页"事件
protected void lnkLast_Click(object sender, EventArgs e)
{
int index &#61; Convert.ToInt32(this.ViewState["TotalPages"].ToString());
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "转到"事件
protected void ddlselectPage_SelectedIndexChanged(object sender, EventArgs e)
{
lblPageIndex.Text &#61; ddlselectPage.SelectedValue;
BindList();
}
#endregion
}
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindList();
}
}
#region 绑定数据源
private void BindList()
{
string sql &#61; " select id,title,memberid,(select usercode from t_mem_memberinfo where memberid&#61;a.memberid) as usercode from t_blog a order by id desc ";
DataSet ds &#61; sqlDatabase.getDS(sql);
//-----------------------分页代码-----------------------//
PagedDataSource Pds &#61; new PagedDataSource();
Pds.DataSource &#61; ds.Tables[0].DefaultView;
Pds.AllowPaging &#61; true; //开启分页
Pds.PageSize &#61; 15; //分页
int index &#61; int.Parse(lblPageIndex.Text); //当前页数
int PageSize &#61; Pds.PageSize; //每页记录数
int TotalRecords &#61; Pds.DataSourceCount; //总记录数
int TotalPages &#61; TotalRecords / PageSize;//总页数&#xff08;总记录数除以每页的记录数&#xff09;
int ModePages &#61; TotalRecords % PageSize;//得到取模的余数(总记录数取模每页的记录数)
if (ModePages > 0)//如果取模数不等于0&#xff0c;则把总页数加1
{
TotalPages &#43;&#61; 1;
}
if (TotalPages &#61;&#61; 0)
{
TotalPages &#61; 1;
}
this.ViewState["TotalPages"] &#61; TotalPages; //保存"总页数"到页面视图中
lblMaxPage.Text &#61; TotalPages.ToString(); //显示页面上的总页数
lblTotalCount.Text &#61; TotalRecords.ToString(); //显示页面上的总记录数
lblPageSize.Text &#61; Pds.PageSize.ToString(); //每页记录数
Pds.CurrentPageIndex &#61; index - 1;
//若是"第一页"
if (index &#61;&#61; 1)
{
lnkFirst.Enabled &#61; false;
lnkPrev.Enabled &#61; false;
if (TotalPages > 1)
{
lnkNext.Enabled &#61; true;
lnkLast.Enabled &#61; true;
}
else
{
lnkNext.Enabled &#61; false;
lnkLast.Enabled &#61; false;
}
}
else
{
lnkFirst.Enabled &#61; true;
lnkPrev.Enabled &#61; true;
//若是"尾页"
if (index &#61;&#61; TotalPages)
{
lnkNext.Enabled &#61; false;
lnkLast.Enabled &#61; false;
}
else
{
lnkNext.Enabled &#61; true;
lnkLast.Enabled &#61; true;
}
}
ddlselectPage.Items.Clear(); //清除防止累加
for (int i &#61; 1; i <&#61; TotalPages; i&#43;&#43;)
{
ddlselectPage.Items.Add(i.ToString());
}
ddlselectPage.Items.FindByText(index.ToString()).Selected &#61; true; //把当前页显示在列表框的第一个
this.rptTest.DataSource &#61; Pds;
this.rptTest.DataBind();
}
#endregion
#region "首页"事件
protected void lnkFirst_Click(object sender, EventArgs e)
{
int index &#61; 1;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "上一页"事件
protected void lnkPrev_Click(object sender, EventArgs e)
{
int index &#61; int.Parse(lblPageIndex.Text);
index--;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "下一页"事件
protected void lnkNext_Click(object sender, EventArgs e)
{
int index &#61; int.Parse(lblPageIndex.Text);
index&#43;&#43;;
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "尾页"事件
protected void lnkLast_Click(object sender, EventArgs e)
{
int index &#61; Convert.ToInt32(this.ViewState["TotalPages"].ToString());
lblPageIndex.Text &#61; index.ToString();
BindList();
}
#endregion
#region "转到"事件
protected void ddlselectPage_SelectedIndexChanged(object sender, EventArgs e)
{
lblPageIndex.Text &#61; ddlselectPage.SelectedValue;
BindList();
}
#endregion
}