ASP.NET的分页方法(一)

要做一个关于分页写法的专题,这是今天的第一讲,自制分页,可能有些代码需要优化,希望大家给出一些中肯的建议

前台使用的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="0">
                <tbody>
                    <tr>
                        <td colspan="5">
                            <%=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 = 0;
        int endcount = 0;
        //绑定数据源
        public void GetDate() 
        {
            if (Request.QueryString["pager"] != null)
            {
                string GetDateSql = "SELECT TOP " + 10 + " * 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 TOP " + 10 + " * 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 + 10 - 1) / 10;//一共的页数 int j = 0;//记录当前页 string FirstPager = string.Empty;//首页 string LastPager = string.Empty;//尾页 string NextPager = string.Empty;//下一页 string PrePager = string.Empty;//上一页 int next = 0;//记录下一页页码 int pre = 0;//记录上一页页码 for (j = 1; j <= totalPageNum; j++) { //Request.QueryString["value"]接受的是页码值 if (Request.QueryString["value"] == null) { startcount = (1 + 5) > totalPageNum ? totalPageNum - 9 : 1 - 4;//中间页起始序号 //中间页终止序号 endcount = 1 < 5 ? 10 : 1 + 5; if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 if (totalPageNum < endcount) { endcount = totalPageNum; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 } else { startcount = (int.Parse(Request.QueryString["value"]) + 5) > totalPageNum ? totalPageNum - 9 : int.Parse(Request.QueryString["value"]) - 4;//中间页起始序号 //中间页终止序号 endcount = int.Parse(Request.QueryString["value"]) < 5 ? 10 : int.Parse(Request.QueryString["value"]) + 5; if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 if (totalPageNum < endcount) { endcount = totalPageNum; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 } } if (Request.QueryString["value"] == null) { next = 1 + 1; pre = 1; } else { next = int.Parse(Request.QueryString["value"]) + 1; pre = int.Parse(Request.QueryString["value"]) - 1; //判断并进行越界处理 if(pre<=0) { pre = 1; } if(next>totalPageNum) { next = totalPageNum; } } for (j = startcount; j <= endcount; j++) { if (Request.QueryString["value"]==null) { pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (1 - 1) + "&value=" + 1 + "\">" + j + "</a>|"; } else if (int.Parse(Request.QueryString["value"]) == j) { pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (j - 1) + "&value=" + j + "\"><span style=\"color:red\">" + j + "</span></a>|"; } else { //10 * (j - 1)这个公式的意思是(当前页码-1)*10,可以跳转到对应页码查看相关的数据 pager += "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (j - 1) + "&value=" + j + "\">" + j + "</a>|"; } } FirstPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (1 - 1) + "&value=" + 1 + "\"> 首页</a>|"; LastPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (totalPageNum - 1) + "&value=" + totalPageNum + "\"> 尾页</a>|"; NextPager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (next - 1) + "&value=" + next + "\"> 下一页</a>|"; PrePager = "<a class=\"hrefName\" href=\"WebForm2.aspx?pager=" + 10 * (pre - 1) + "&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=" + 10 * (Convert.ToInt32(this.TxtPager.Text) - 1)+"&value=" + this.TxtPager.Text ); } } } }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。