C# .NET

Asp.net 分页控件使用

位置:首页 > C# .NET > C# 提高,2013-05-26
最近整理asp.net项目代码,以前没有记录笔记的习惯,我要利用这次机会把项目常用的一些代码整理归档,今天整理最通用的代码,分页控件。网上有很多分页控件,也很好用但是我这个分页控件是自己开发的,自认为...

最近整理asp.net项目代码,以前没有记录笔记的习惯,我要利用这次机会把项目常用的一些代码整理归档,今天整理最通用的代码,分页控件。

网上有很多分页控件,也很好用但是我这个分页控件是自己开发的,自认为还可以。这个分页控件大概步骤如下:

一、在项目中首先新建一个用户控件,命名为PageShow如图-1所示。                       

图-1

二、在打开的页面当中添加一个asp:Literal控件具体代码如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageShow.ascx.cs" Inherits="Web.control.PageShow" %>

<asp:Literal ID="ltlPager" runat="server"></asp:Literal>

       三、编写后台代码实现分页功能,一些代码注释我都写在代码中

using System;

using System.Collections;

using System.Configuration;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using System.Text.RegularExpressions;

namespace Web.control

{

    public partial class PageShow : System.Web.UI.UserControl

    {

        private int pageSize; //页大小

        private int recordCount; //记录总数

        private int page; //指当前页码

        private int pageCount;  //page总数

        private string url; //当前页地址

        private int width; //控件宽度

        private string cssClass; //控件css类名

        private string align; //对齐方式

        private string pageHtml;

        private bool? enablePageInfo; //是否显示分页信息

        private bool? enableJumpPage; //是否显示跳转信息

        protected void Page_Load(object sender, EventArgs e)

        {

            pageCount = PageCount;

            page = CurrentPage;

            url = Url;

            string filename = System.IO.Path.GetFileName(HttpContext.Current.Request.Path);

            url = filename + url;

            width = Width;

            pageHtml += (width == 0) ? "<div class=\"" + CssClas + "\">" : "<div class=\"" + CssClas + "\" style=\"width:" + width + "px\">";

            if (EnablePageInfo == true) AppendPageInfo();

            pageHtml += (page <= 1) ? "<span class=\"disabled\">上一页</span>" : "<a href=\"" + url.Replace("page=" + page, "page=" + (page - 1)) + "\" title=\"上一页\" class=\"pagerPrev\">上一页</a>";

            pageHtml += (page <= 1) ? "<span class=\"current\">" + page + "</span>" : "<a href=\"" + url.Replace("page=" + page, "page=1") + "\" title=\"跳转到第1页\">1</a>";

            int start = 2;

            int end = (pageCount < 10) ? pageCount : 10;

            if (page > 8)

            {

                pageHtml += "...";

                start = page - 4;

                int end1 = page + 5;

                end = (pageCount < end1) ? pageCount : end1;

            }

            for (int i = start; i < end; i++)

            {

                pageHtml += (i == page) ? "<span class=\"current\">" + i + "</span>" : "<a href=\"" + url.Replace("page=" + page, "page=" + i) + "\" title=\"跳转到第" + i + "页\">" + i + "</a>";

            }

            pageHtml += (page + 5 < pageCount) ? "..." : "";

            pageHtml += (page == pageCount) ? "<span class=\"current\">" + page + "</span>" : "<a href=\"" + url.Replace("page=" + page, "page=" + pageCount) + "\" title=\"跳转到第" + pageCount + "页\">" + pageCount + "</a>";

            pageHtml += (page == pageCount) ? "<span class=\"disabled\">下一页</span>" : "<a href=\"" + url.Replace("page=" + page, "page=" + (page + 1)) + "\" title=\"下一页\" class=\"pagerNext\">下一页</a>";

            if (EnableJumpPage == true) AppendJumpPageScript();

            pageHtml += "</div>";

            ltlPager.Text = pageHtml;

        }

        private void AppendJumpPageScript()

        {

            pageHtml += "&nbsp;<span  class='jumpPage'>跳到第 <input id=\"txtPagebarJumpPage\" class=\"pagebarTextBox\" onfocus=\"this.value=''\" type=\"text\" value=\"" + page + "\" style=\"width:20px;\" /> 页&nbsp;<input type=\"button\" class=\"pagerButton\" onclick=\"jumpPage();\" value=\"确定\" title=\"跳转到指定页\" /></span>" + System.Environment.NewLine;

            pageHtml += "<script type=\"text/javascript\">\n";

            pageHtml += " //<![CDATA[ \n";

            pageHtml += " function jumpPage(){ \n";

            pageHtml += "     var url; \r\n";

            pageHtml += "     var pageNo = document.getElementById('txtPagebarJumpPage').value; \n";

            pageHtml += "     if (document.URL.indexOf('page=') > 0) \r\n";

            pageHtml += "         url =  document.URL.replace('page=" + page + "','page='+pageNo); \n";

            pageHtml += "     else \n";

            pageHtml += "         url =  document.URL.indexOf('?') > 0 ? document.URL + '&amp;page='+ pageNo: document.URL + '?page='+pageNo; \n";

            pageHtml += "     location.href = url; \n";

            pageHtml += " } \r\n";

            pageHtml += " //]]> \r\n";

            pageHtml += "</script> \r\n";

        }

        private void AppendPageInfo()

        {

            pageHtml += "<span class=\"pageInfo\">第" + CurrentPage + "/" + PageCount + "页" + System.Environment.NewLine + "</span>";

        }

        #region 受保护属性

        /// <summary>

        /// 计算页数

        /// </summary>

        private int PageCount

        {

            get

            {

                return (recordCount == 0) ? 1 : (int)(Math.Ceiling((double)recordCount / pageSize));

            }

        }

        /// <summary>

        /// 当前页数

        /// </summary>

        private int CurrentPage

        {

            get

            {

                string query = Request.Url.Query;

                string pageString = "page=1";

                Regex regex = new Regex(@"(page=\d*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                Match match = regex.Match(query);

                if (match.Success)

                {

                    pageString = match.Groups[1].ToString();

                }

                if (pageString.IndexOf("=") > -1)

                {

                    string[] arrPage = pageString.Split(new Char[] { '=' });

                    page = Convert.ToInt32(arrPage[1]);

                }

                else

                {

                    page = 1;

                }

                if (page > PageCount) { page = PageCount; }

                if (page < 1) { page = 1; }

                return (int)page;

            }

        }

        /// <summary>

        /// 设置页码跳转URL

        /// </summary>

        private string Url

        {

            get

            {

                string query = Request.Url.Query;

                Regex regex = new Regex(@"(page=\d*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (!regex.Match(query).Success) { query += (query.IndexOf("?") > -1 ? "&amp;page=1" : "?page=1"); }

                return query;

            }

        }

        #endregion

        #region  公共属性

        /// <summary>

        /// 页面大小

        /// </summary>

        public int PageSize

        {

            get { return (pageSize == 0) ? 10 : pageSize; }

            set { pageSize = value; }

        }

        /// <summary>

        /// 记录总数

        /// </summary>

        public int RecordCount

        {

            get { return recordCount; }

            set { recordCount = value; }

        }

        /// <summary>

        /// 分页样式名称

        /// </summary>

        public string CssClas

        {

            get { return (string.IsNullOrEmpty(cssClass)) ? "sabrosus" : cssClass; }

            set { cssClass = value; }

        }

        /// <summary>

        /// 对齐方式 center:居中;left:左对齐;right:右对齐

        /// </summary>

        public string Align

        {

            get

            {

                if (string.IsNullOrEmpty(align))

                    return "center";

                switch (align.ToLower())

                {

                    case "left":

                        return "left";

                    case "right":

                        return "right";

                    default:

                        return "center";

                }

            }

            set

            {

                align = value;

            }

        }

        public int Width

        {

            get { return width; }

            set { width = value; }

        }

        /// <summary>

        /// 是否显示退转页面信息

        /// </summary>

        public bool? EnableJumpPage

        {

            get { return (enableJumpPage == null) ? false : enableJumpPage; }

            set { enableJumpPage = value; }

        }

        /// <summary>

        /// 是否显示页面信息,例如10/100共一百页当前第10页

        /// </summary>

        public bool? EnablePageInfo

        {

            get { return (enablePageInfo == null) ? false : enablePageInfo; }

            set { enablePageInfo = value; }

        }

        #endregion

    }

}

 

说一说这段代码中的一些重点:

?的用处。在c#中的?是不常用的一个方法。

如果是一个?经常这样用,数据类型?:表示可空类型,就是一种特殊的值类型,它的值可以为null用于给变量设初值得时候,给变量赋值为null,而不是0。例如 int? a 表示给int类型变量赋null值

如果是两个?则用于判断并赋值,先判断当前变量是否为null,如果是就可以赋后面的值,否则跳过。例如 obj = obj1 ?? new Object()

表示如果obj不为空赋值obj1,否知赋值new Object()等价于如下代码

if (obj == null)
      obj = new Object();
else
  obj = obj1;

四、css样式美化

目前为止我已经开发了分页控件的大部分功能,但是分页控件现在很不美观,需要我们编写Css样式进行美化,我编写的Css文件名叫Pagelist.css代码如下,如果您感兴趣可以自己编写Css代码进行美化。

.Pagelist{ height:30px; line-height:30px; text-align:right; margin-right:20px;}

.Pagelist a{padding:3px 8px; border:1px #CCC solid; margin:0px 3px;}

.pagerPrev{ padding:3px 8px; border:1px #CCC solid; margin:0px 3px;}

.current{padding:3px 8px; border:1px #CCC solid; margin:0px 3px; background:#F06; color:#FFF;}

.disabled{padding:3px 8px; border:1px #CCC solid; margin:0px 3px; background:#F6F6F6; color:#999;}

.pagebarTextBox{ border:1px #ccc solid;padding:3px 8px; }

.pagerButton{ border:1px #ccc solid;padding:3px 8px; }

五、控件调用

Ok我们的控件已经开发完毕,下面就是如何使用这个分页控件了,首先呢我们在页面中引用这个控件和Ccss样式,具体代码如下:

在页面头部引入控件:

<%@ Register Src="../control/PageShow.ascx" TagName="PageShow" TagPrefix="uc1" %>

在head之间引入让是表:

<link href="../skin/purple/Pagelist.css" rel="stylesheet" type="text/css" />

在需要显示分页的地方编写控件代码:

      <div class="Pagelist">

        <uc1:PageShow ID="PageShow1" runat="server" CssClas="Pagelist" EnableJumpPage="true" EnablePageInfo="true" Width="960" />

        </div>

在后台编写代码实现分页控件的功能:

if (myp.recordCount > 20)

            {

                PageShow1.PageSize = 20;

                PageShow1.RecordCount = myp.recordCount;

            }

            else

            {

                PageShow1.Visible = false;

        }

这样我们的分页控件就开发完毕了最终效果如图-2所示。

图-2

TAGS:Asp.net 分页Asp.net 分页控件Asp.net 分页控件使用

猜你喜欢

NewHot手机版