C#自己写的一个自定义分页控件

 

C#自己写的一个自定义分页控件(源码下载,没有DEMO)

分页以前一直是用第三方分页控件AspNetPager.dll,使用起来也挺方便的,就是样式设置起来感觉不是很好,于是经理决定我们自己做一个分页控件,这个任务就交到我的手上。这个自定义分页控件是模仿58同城的分页及结合我们现在项目的需要写的。感觉比较实用,没什么亮点。

  其实分页控件也没啥,就是生成相关的HTML代码,你需要啥HTML代码,用你的代码生成就是了,不管是AspNetPager.dll还是58同城也都是生成分页相关的HTML代码,其实就是一堆a标签,点分页的时候,分页控件所在的页面会刷新。

 

using System.Text;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Collections.Generic;
using System.Web.UI.WebControls;

//自定义分页控件
namespace ObjectCommon.Libary.Component
{
    [ToolboxData("<{0}:PagingControl runat=\"server\"></{0}:PagingControl>"), DefaultProperty("")]
    public class PagingControl : Literal
    {
        #region 分页属性
        private int pageSize = 10;
        public int PageSize
        {
            set {
                if (value > 0)
                {
                    pageSize = value;
                }
            }
            get { return pageSize; }
        }

        private int pageIndex = 1;
        public int PageIndex
        {
            set
            {
                if (value > 0)
                {
                    pageIndex = value;
                }
            }
            get { return pageIndex; }
        }
       
        public int RecordCount
        {
            set;
            get;
        }

        private int PageCount = 0;
        
        //分页参数
        private string paraName = "page";
        public string ParaName
        {
            set
            {
                if (value != null && value.Trim() != "")
                {
                    paraName = value.Trim();
                }
            }
            get {
                return paraName;
            }
        }
        #endregion

        #region Css
        //分页控件的样式
        public string CssClass
        {
            set;
            get;
        }

        //上一页样式
        public string CssPrev
        {
            set;
            get;
        }

        //下一页样式
        public string CssNext
        {
            set;
            get;
        }

        //当前页样式
        public string CssCurrent
        {
            set;
            get;
        }

        //首页样式
        public string CssFirst
        {
            set;
            get;
        }

        //尾页样式
        public string CssLast
        {
            set;
            get;
        }

        //其它页样式
        public string CssOther
        {
            set;
            get;
        }
        #endregion

        #region Img
        //首页图片
        public string ImgFirst
        {
            set;
            get;
        }

        //上一页页图片
        public string ImgPrev
        {
            set;
            get;
        }

        //下一页页图片
        public string ImgNext
        {
            set;
            get;
        }

        //尾页
        public string ImgLast
        {
            set;
            get;
        }
        #endregion

        // A标签的HRef
        private string ATargetHref
        {
            set;
            get;
        }

        //获取paraname之外的请求参数
        private void GetUrl()
        {
            HttpContext ctx = HttpContext.Current;
            string hostName ="http://"+ ctx.Request.Url.Host + ":" + ctx.Request.Url.Port ;
            string url = ctx.Request.Url.ToString().Substring(hostName.Length).Split('?')[0] + "?";
            StringBuilder query = new StringBuilder(url);
            foreach (string key in ctx.Request.QueryString)
            {
                if (key!=null && key.ToLower() != this.ParaName)
                {
                    query.Append(string.Format("{0}={1}&", key, ctx.Request.QueryString[key]));
                }
            }
            this.ATargetHref = query.ToString();
        }

        private string GetUrl(int pageIndex)
        {
            return string.Format("{0}{1}={2}", this.ATargetHref,this.paraName, pageIndex);
        }
         
        //初始化属性
        private void Initializtion()
        {
            GetUrl();
            this.PageCount = (this.RecordCount - 1) / this.PageSize + 1;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[this.ParaName]))
            {
                this.PageIndex = int.Parse(HttpContext.Current.Request[this.ParaName]);
            }
        }

        //生成控件
        private void LoadControl(List<Tag> divPaging)
        {
            if (divPaging == null || divPaging.Count == 0)
            {
                this.Text = null;
                return;
            }
            StringBuilder sb = new StringBuilder(256);
            if (this.CssClass == null || this.CssClass.Trim() == "")
            {
                sb.Append("<div>");
            }
            else
            {
                sb.Append(string.Format("<div class='{0}'>", this.CssClass));
            }
            for (int i = 0; i < divPaging.Count;i++)
            {
                if (divPaging[i] == null)
                {
                    sb.Append(GetCurrentPage());
                }
                else
                {
                    sb.Append(divPaging[i].ToString());
                }
            }
            sb.Append("</div>");
            this.Text = sb.ToString();
        }

        private string GetCurrentPage()
        {
            if (this.CssCurrent == null || this.CssCurrent.Trim() == "")
            {
                return string.Format("<span>{0}</span>", this.PageIndex);
            }
            else {
                return string.Format("<span class={0}>{1}</span>", this.CssCurrent, this.PageIndex);
            }
        }
         
        /// <summary>
        /// 加载控件
        /// </summary>
        public void LoadControl()
        {
            if (this.RecordCount<=this.PageSize)
            {
                this.Text = null;
            }
            else
            {
                Initializtion();
                LoadControl(AddATarget());
            }
        }
         
        #region 创建标签
        private Tag CreateATarget(string img,string css,int pageIndex,string content)
        {
            Tag a = new Tag();
            a.HRef = GetUrl(pageIndex);
            if (img == null || img.Trim() == "")
            {
                a.InnerHtml = content;
            }
            else {
                a.InnerHtml = string.Format("<img src={0}/>",img);
            }

            if (css != null && css.Trim() != "")
            {
                a.CssClass = css;
            }
            return a;
        }

    
        #endregion

        // 向集合中添加标签
        private List<Tag> AddATarget()
        {
            List<Tag> divPaging = new List<Tag>();
             
            if (this.PageIndex > 1)
            {
                divPaging.Add(CreateATarget(this.ImgFirst, this.CssFirst, 1, "首页"));//添加首页
                divPaging.Add(CreateATarget(this.ImgPrev, this.CssPrev, this.PageIndex - 1, "上一页"));//添加上一页
            }
             
            int end = this.PageIndex + 5;
            if (end < 11)
            {
                end = 11;
            }
            if (end > this.PageCount)
            {
                end = this.PageCount;
            }
          
            int start = end - 10 > 0 ? end - 10 : 1;

            for (int i = start; i <= end; i++)
            {
                if (i == this.PageIndex)
                {
                    divPaging.Add(null);
                }
                else
                {
                    divPaging.Add(CreateATarget(null, this.CssOther, i, i.ToString()));
                }
            }

            if (this.PageIndex < this.PageCount)
            {
                divPaging.Add(CreateATarget(this.ImgNext, this.CssNext, this.PageIndex + 1, "下一页"));//添加下一页
                divPaging.Add(CreateATarget(this.ImgLast, this.CssLast, this.PageCount, "尾页"));//添加尾页
            }
             
            return divPaging;
        }
 
        // 标签实体
        private class Tag 
        {
            //链接
            public string HRef
            {
                set;
                get;
            }

            //链接显示的内容
            public string InnerHtml
            {
                set;
                get;
            }

            //样式
            public string CssClass
            {
                set;
                get;
            }

            public override string ToString()
            { 
                if (this.CssClass != null && this.CssClass.Trim() != "")
                { 
                    this.CssClass = string.Format(" class='{0}'", this.CssClass);
                }
                return string.Format("<a href='{0}'{1}>{2}</a>", this.HRef, this.CssClass, this.InnerHtml);
            }
        }

    }
}


你可能感兴趣的:(C#自己写的一个自定义分页控件)