• C#自定义分页控件


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

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

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

    一:用AspNetPager.dll实现的分页

    图:

     

     

    生成的源码
    <div id="ctl00_Content_AspNetPager1">
    <a disabled="disabled" style="margin-right:5px;">首页</a>
    <a disabled="disabled" style="margin-right:5px;">上一页</a>
    <span class="al" style="margin-right:5px;">1</span>
    <a href="NewsList.aspx?page=2" style="margin-right:5px;">2</a>
    <a href="NewsList.aspx?page=3" style="margin-right:5px;">3</a>
    <a href="NewsList.aspx?page=4" style="margin-right:5px;">4</a>
    <a href="NewsList.aspx?page=5" style="margin-right:5px;">5</a>
    <a href="NewsList.aspx?page=6" style="margin-right:5px;">6</a>
    <a href="NewsList.aspx?page=7" style="margin-right:5px;">7</a>
    <a href="NewsList.aspx?page=2" style="margin-right:5px;">下一页</a>
    <a href="NewsList.aspx?page=7" style="margin-right:5px;">尾页</a>
    </div>

      二:58同城的分页

      图:

      

    生成的源码
    <div class="pager">
    <strong><span>1</span></strong><a href="/zufang/pn2/"><span>2</span></a>
    <a href="/zufang/pn3/"><span>3</span></a>
    <a href="/zufang/pn4/"><span>4</span></a>
    <a href="/zufang/pn5/"><span>5</span></a>
    <a href="/zufang/pn6/"><span>6</span></a>
    <a href="/zufang/pn7/"><span>7</span></a>
    <a href="/zufang/pn8/"><span>8</span></a>
    <a href="/zufang/pn9/"><span>9</span></a>
    <a href="/zufang/pn10/"><span>10</span></a>
    <a href="/zufang/pn11/"><span>11</span></a>
    <a href="/zufang/pn12/"><span>12</span></a>
    <a class="next" href="/zufang/pn2/"><span>下一页</span></a>
    </div>

     

      三:我的分页控件 

    程序代码
    <os:PagingControl runat="server" ID="paginglist" PageSize="4" CssCurrent="curpage"  ImgFirst="/images/index/btn_first.jpg"
    ImgPrev="/images/index/btn_prev.jpg" ImgNext="/images/index/btn_next.jpg" ImgLast="/images/index/btn_end.jpg" CssClass="pagelist">
    </os:PagingControl>
    paginglist.RecordCount = 200;
    paginglist.LoadControl();
    生成源码
    <div class='pagelist'>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=1'><img src=/images/index/btn_first.jpg/></a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=23'><img src=/images/index/btn_prev.jpg/></a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=14'>14</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=15'>15</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=16'>16</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=17'>17</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=18'>18</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=19'>19</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=20'>20</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=21'>21</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=22'>22</a>
    <a href='/CommonQuestion/QuestionListContent.aspx?questionTypeID=0&page=23'>23</a>
    <span class=curpage>24</span>
    </div>

    程序源码
    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("")]
    publicclass PagingControl : Literal
    {
    #region 分页属性
    privateint pageSize =10;
    publicint PageSize
    {
    set {
    if (value >0)
    {
    pageSize
    = value;
    }
    }
    get { return pageSize; }
    }

    privateint pageIndex =1;
    publicint PageIndex
    {
    set
    {
    if (value >0)
    {
    pageIndex
    = value;
    }
    }
    get { return pageIndex; }
    }

    publicint RecordCount
    {
    set;
    get;
    }

    privateint PageCount =0;

    //分页参数
    privatestring paraName ="page";
    publicstring ParaName
    {
    set
    {
    if (value !=null&& value.Trim() !="")
    {
    paraName
    = value.Trim();
    }
    }
    get {
    return paraName;
    }
    }
    #endregion

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

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

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

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

    //首页样式
    publicstring CssFirst
    {
    set;
    get;
    }

    //尾页样式
    publicstring CssLast
    {
    set;
    get;
    }

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

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

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

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

    //尾页
    publicstring ImgLast
    {
    set;
    get;
    }
    #endregion

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

    //获取paraname之外的请求参数
    privatevoid 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();
    }

    privatestring GetUrl(int pageIndex)
    {
    returnstring.Format("{0}{1}={2}", this.ATargetHref,this.paraName, pageIndex);
    }

    //初始化属性
    privatevoid 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]);
    }
    }

    //生成控件
    privatevoid 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();
    }

    privatestring GetCurrentPage()
    {
    if (this.CssCurrent ==null||this.CssCurrent.Trim() =="")
    {
    returnstring.Format("<span>{0}</span>", this.PageIndex);
    }
    else {
    returnstring.Format("<span class={0}>{1}</span>", this.CssCurrent, this.PageIndex);
    }
    }

    ///<summary>
    /// 加载控件
    ///</summary>
    publicvoid 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;
    }

    // 标签实体
    privateclass Tag
    {
    //链接
    publicstring HRef
    {
    set;
    get;
    }

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

    //样式
    publicstring CssClass
    {
    set;
    get;
    }

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

    }
    }

      自定义控件注册:

      在web.config中,节点<pages> <controls>中加入:

        <add tagPrefix="os" namespace="ObjectCommon.Libary.Component" assembly="ObjectCommon.Libary"/>

  • 相关阅读:
    Spring学习(一):理解IoC容器
    Spring学习(零):我们为什么要学习Spring
    git push提交报错,提示文件过大,且去掉大文件也报同样的错误
    There was a problem with the instance info replicator
    Eureka配置instanceId显示IP
    Cannot execute request on any known server或DiscoveryClient_UNKNOWN/DESKTOP-MQ8D0C9:8761
    Hexo优化 | 创建sitemap站点地图并向Google提交
    matlab与python读取tiff文件
    Visual studio中编译和使用libpng和zlib
    关于softmax、argmax、softargmax
  • 原文地址:https://www.cnblogs.com/hlxs/p/2145608.html
Copyright © 2020-2023  润新知