要做一个关于分页写法的专题,这是今天的第一讲,自制分页,可能有些代码需要优化,希望大家给出一些中肯的建议
前台使用的repeater绑定的数据:
<form id="form1" runat="server">
<div>
<ul style="list-style: none">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li><%#Eval("Title") %></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div> <div id="pagerwrapper">
<table id="pager" cellspacing="">
<tbody>
<tr>
<td colspan="">
<%=RePager %>
跳转至:<asp:TextBox ID="TxtPager" runat="server"></asp:TextBox>页
<asp:Button ID="BtnOk" runat="server" Text="确定" OnClick="BtnOk_Click" />
</td>
</tr>
</tbody>
</table>
</div>
</form>
后台代码写法如下:注释已标明:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetDate(); }
//这样写可以避免第一次加载之后再次加载不显示的问题
GetPager();
}
string ConStr = ConfigurationManager.ConnectionStrings["Connection"].ToString(); public string pager = string.Empty;
public string RePager = string.Empty;
int startcount = ;
int endcount = ;
//绑定数据源
public void GetDate()
{
if (Request.QueryString["pager"] != null)
{
string GetDateSql = "SELECT * FROM ( SELECT ROW_NUMBER() over(order by id) as pid,*FROM [Info]) AS aa where aa.pid>=" + Request.QueryString["pager"] + ""; using (SqlConnection conn = new SqlConnection(ConStr))
{
conn.Open();
DataSet dt = new DataSet();
SqlDataAdapter sdt = new SqlDataAdapter(GetDateSql, conn);
sdt.Fill(dt);
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
}
else
//初次加载不传入任何页码,默认显示前十条
{
string GetDateSql = "SELECT * FROM ( SELECT ROW_NUMBER() over(order by id) as pid,*FROM [Info]) AS aa "; using (SqlConnection conn = new SqlConnection(ConStr))
{
conn.Open();
DataSet dt = new DataSet();
SqlDataAdapter sdt = new SqlDataAdapter(GetDateSql, conn);
sdt.Fill(dt);
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
} }
//得到页码
public void GetPager()
{
string GetTotal = "SELECT COUNT(*) FROM [Info]";
using (SqlConnection conn = new SqlConnection(ConStr))
{
conn.Open();
SqlCommand comm = new SqlCommand(GetTotal,conn);
int i = Convert.ToInt32(comm.ExecuteScalar());
int totalPageNum = (i + - ) / ;//一共的页数
int j = ;//记录当前页
string FirstPager = string.Empty;//首页
string LastPager = string.Empty;//尾页 string NextPager = string.Empty;//下一页
string PrePager = string.Empty;//上一页 int next = ;//记录下一页页码
int pre = ;//记录上一页页码 for (j = ; j <= totalPageNum; j++)
{
//Request.QueryString["value"]接受的是页码值
if (Request.QueryString["value"] == null)
{
startcount = ( + ) > totalPageNum ? totalPageNum - : - ;//中间页起始序号
//中间页终止序号
endcount = < ? : + ;
if (startcount < ) { startcount = ; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (totalPageNum < endcount) { endcount = totalPageNum; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
}
else
{
startcount = (int.Parse(Request.QueryString["value"]) + ) > totalPageNum ? totalPageNum - : int.Parse(Request.QueryString["value"]) - ;//中间页起始序号
//中间页终止序号
endcount = int.Parse(Request.QueryString["value"]) < ? : int.Parse(Request.QueryString["value"]) + ;
if (startcount < ) { startcount = ; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (totalPageNum < endcount) { endcount = totalPageNum; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
}
} if (Request.QueryString["value"] == null)
{
next = + ;
pre = ;
}
else
{
next = int.Parse(Request.QueryString["value"]) + ;
pre = int.Parse(Request.QueryString["value"]) - ;
//判断并进行越界处理
if(pre<=)
{
pre = ;
}
if(next>totalPageNum)
{
next = totalPageNum;
}
} for (j = startcount; j <= endcount; j++)
{
if (Request.QueryString["value"]==null)
{
pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * ( - ) + "&value=" + + "\">" + j + "</a>|";
}
else if (int.Parse(Request.QueryString["value"]) == j)
{
pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * (j - ) + "&value=" + j + "\"><span style=\"color:red\">" + j + "</span></a>|";
}
else
{
//10 * (j - 1)这个公式的意思是(当前页码-1)*10,可以跳转到对应页码查看相关的数据
pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * (j - ) + "&value=" + j + "\">" + j + "</a>|";
}
} FirstPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * ( - ) + "&value=" + + "\"> 首页</a>|";
LastPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * (totalPageNum - ) + "&value=" + totalPageNum + "\"> 尾页</a>|";
NextPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * (next - ) + "&value=" + next + "\"> 下一页</a>|";
PrePager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + * (pre - ) + "&value=" + pre + "\"> 上一页</a>|"; RePager = FirstPager + PrePager+ pager + NextPager + LastPager;//拼凑字符串
}
}
//跳转到第几页的方法
protected void BtnOk_Click(object sender, EventArgs e)
{
if (Request.QueryString["value"] == null)
{
Response.Redirect("WebForm2.aspx?pager=" + * (Convert.ToInt32(this.TxtPager.Text) - )+"&value=" + this.TxtPager.Text );
}
}
}
}
附带几条公式,如有不对,希望大家指正
信息条数/每页显示条数=一共的页数
开始页码= (当前显示页+ 5) > 总页? 总页 - 9 : 当前显示页 - 4;//中间页起始序号
//中间页终止序号
结束页码= 当前显示页 < 5 ? 10 : 当前显示页+ 5;