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