自定义分页控件

创建自定义服务器控件:

分页控件:

分页控件属性:

页面级分页代码:

.CS

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace TianCai.Infrastructure.WebControls
{
 [ToolboxData("<{0}:PageBar runat=\"server\"></{0}:PageBar>")]
 public class PageBar : WebControl, IPostBackEventHandler
 {
  [Category("Layout")]
  [DefaultValue(0)]
  public int CellPadding
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "CellPadding")] == null)
    {
     return 0;
    }
    return (int)this.ViewState[string.Concat(this.UniqueID, "CellPadding")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "CellPadding")] = value;
   }
  }

  [Category("Layout")]
  [DefaultValue(0)]
  public int CellSpacing
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "CellSpacing")] == null)
    {
     return 0;
    }
    return (int)this.ViewState[string.Concat(this.UniqueID, "CellSpacing")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "CellSpacing")] = value;
   }
  }

  [Browsable(false)]
  [Category("Data")]
  public int CurrentPage
  {
   get
   {
    int pageCount = (this.ViewState[string.Concat(this.UniqueID, "CurrentPage")] == null ? 1 : (int)this.ViewState[string.Concat(this.UniqueID, "CurrentPage")]);
    if (pageCount > this.PageCount)
    {
     pageCount = this.PageCount;
     this.CurrentPage = pageCount;
    }
    if (pageCount <= 1)
    {
     return 1;
    }
    return pageCount;
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "CurrentPage")] = value;
   }
  }

  [Category("Navigation")]
  [DefaultValue("首页")]
  public string FirstPageText
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "FirstPageText")] == null)
    {
     return "首页";
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "FirstPageText")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "FirstPageText")] = value;
   }
  }

  [Category("Layout")]
  public override Unit Height
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "Height")] == null)
    {
     return Unit.Pixel(20);
    }
    return (Unit)this.ViewState[string.Concat(this.UniqueID, "Height")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "Height")] = value;
   }
  }

  [Category("Navigation")]
  [DefaultValue("末页")]
  public string LastPageText
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "LastPageText")] == null)
    {
     return "末页";
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "LastPageText")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "LastPageText")] = value;
   }
  }

  [Category("Navigation")]
  [DefaultValue("下一页")]
  public string NextPageText
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "NextPageText")] == null)
    {
     return "下一页";
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "NextPageText")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "NextPageText")] = value;
   }
  }

  [Browsable(false)]
  public int PageCount
  {
   get
   {
    if (this.RecordCount == 0)
    {
     return 0;
    }
    return (int)Math.Ceiling((double)this.RecordCount / (double)this.PageSize);
   }
  }

  [Category("Navigation")]
  [DefaultValue("CurrentPage")]
  public string PageParamName
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "PageParamName")] == null)
    {
     return "CurrentPage";
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "PageParamName")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "PageParamName")] = value;
   }
  }

  [Category("Appearance")]
  public string PagerCssClass
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")] == null)
    {
     return string.Empty;
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")] = value;
   }
  }

  [Category("Data")]
  [DefaultValue(12)]
  public int PageSize
  {
   get
   {
    int num = (this.ViewState[string.Concat(this.UniqueID, "PageSize")] == null ? 12 : (int)this.ViewState[string.Concat(this.UniqueID, "PageSize")]);
    if (num <= 0)
    {
     return 0;
    }
    return num;
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "PageSize")] = value;
   }
  }

  [Category("Navigation")]
  [DefaultValue("上一页")]
  public string PrePageText
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "PrePageText")] == null)
    {
     return "上一页";
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "PrePageText")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "PrePageText")] = value;
   }
  }

  [Browsable(false)]
  [Category("Data")]
  public int RecordCount
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "RecordCount")] == null)
    {
     return 0;
    }
    return (int)this.ViewState[string.Concat(this.UniqueID, "RecordCount")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "RecordCount")] = value;
   }
  }

  [Category("Appearance")]
  public string TableCellCssClass
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")] == null)
    {
     return string.Empty;
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")] = value;
   }
  }

  [Category("Appearance")]
  public string TableCssClass
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "TableCssClass")] == null)
    {
     return string.Empty;
    }
    return (string)this.ViewState[string.Concat(this.UniqueID, "TableCssClass")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "TableCssClass")] = value;
   }
  }

  [Category("Layout")]
  public override Unit Width
  {
   get
   {
    if (this.ViewState[string.Concat(this.UniqueID, "Width")] == null)
    {
     return Unit.Percentage(100);
    }
    return (Unit)this.ViewState[string.Concat(this.UniqueID, "Width")];
   }
   set
   {
    this.ViewState[string.Concat(this.UniqueID, "Width")] = value;
   }
  }

  public PageBar()
  {
  }

  private void GetHTML(HtmlTextWriter writer)
  {
   string[] pagerLinks = this.GetPagerLinks();
   writer.Write("<script language='javascript'>\n");
   writer.Write(string.Concat("function ", this.UniqueID, "ChangePage(pageCount,page)\n"));
   writer.Write("{");
            //writer.Write("if (page == pageCount) return;\n");
   writer.Write("if (page > pageCount || page < 1 || isNaN(page))");
   writer.Write("\r\n                {\r\n                    alert_s('请输入正确的页码!');\r\n                    return;\r\n                } \t\t\t        \t\t\t                                \r\n            ");
   writer.Write(string.Concat("__doPostBack('", this.UniqueID, "',page)"));
   writer.Write("\r\n            }\r\n            </script>\r\n            ");
            //隐藏栏位【当前页】
   HtmlInputHidden htmlInputHidden = new HtmlInputHidden();
   htmlInputHidden.Attributes.Add("name", "currentPage");
   htmlInputHidden.Value = this.CurrentPage.ToString();

   HtmlTable htmlTable = new HtmlTable()
   {
    Width = this.Width.ToString(),
    Height = this.Height.ToString(),
    CellPadding = this.CellPadding,
    CellSpacing = this.CellSpacing
   };
   htmlTable.Attributes.Add("class", this.TableCssClass);

   HtmlTableRow htmlTableRow = new HtmlTableRow();

   HtmlTableCell htmlTableCell = new HtmlTableCell()
   {
    Align = "left",
    Width = "100"
   };
   if (this.TableCellCssClass.Length != 0)
   {
    htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
   }

   ControlCollection controls = htmlTableCell.Controls;
   int recordCount = this.RecordCount;
            controls.Add(new LiteralControl(string.Concat("&nbsp;共&nbsp;","<i style=\"color:#056dae;font-style: normal;\">",recordCount.ToString(),"</i>","&nbsp;条数据")));
   htmlTableRow.Cells.Add(htmlTableCell);

   htmlTableCell = new HtmlTableCell()
   {
    Align = "center"
   };
            //if (this.TableCellCssClass.Length != 0)
            //{
            //    htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
            //}
            htmlTableCell.Attributes.Add("class","pageclassstylehome");
            //首页
   HyperLink hyperLink = new HyperLink();
            //hyperLink.Attributes.Add("class", this.PagerCssClass);
            hyperLink.Attributes.Add("class","pageclassstyle pageclassstylehome");
            hyperLink.Attributes.Add("style","border-left:1px solid #ddd;");
   if (pagerLinks[0] == null)
   {
    hyperLink.Enabled = false;
   }
   else
   {
    hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[0]));
   }
   hyperLink.Text = this.FirstPageText;
   htmlTableCell.Controls.Add(hyperLink);
            //空格
   htmlTableCell.Controls.Add(new LiteralControl("&nbsp;"));
            //上一页
   hyperLink = new HyperLink()
   {
    ID = "prePage"
   };
   //hyperLink.Attributes.Add("class", this.PagerCssClass);
            hyperLink.Attributes.Add("class","pageclassstyle");
   if (pagerLinks[1] == null)
   {
    hyperLink.Enabled = false;
   }
   else
   {
    hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[1]));
   }
   hyperLink.Text = this.PrePageText;
   htmlTableCell.Controls.Add(hyperLink);
            //空格
   htmlTableCell.Controls.Add(new LiteralControl("&nbsp;"));
            //下一页
   hyperLink = new HyperLink();
   //hyperLink.Attributes.Add("class", this.PagerCssClass);
            hyperLink.Attributes.Add("class","pageclassstyle");
   if (pagerLinks[2] == null)
   {
    hyperLink.Enabled = false;
   }
   else
   {
    hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[2]));
   }
   hyperLink.Text = this.NextPageText;
   htmlTableCell.Controls.Add(hyperLink);
            //空格
   htmlTableCell.Controls.Add(new LiteralControl("&nbsp;"));
            //末页
   hyperLink = new HyperLink();
   //hyperLink.Attributes.Add("class", this.PagerCssClass);
            hyperLink.Attributes.Add("class","pageclassstyle");
   if (pagerLinks[3] == null)
   {
    hyperLink.Enabled = false;
   }
   else
   {
    hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[3]));
   }
   hyperLink.Text = this.LastPageText;
   htmlTableCell.Controls.Add(hyperLink);
            //空格
   htmlTableCell.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

   HtmlInputText htmlInputText = new HtmlInputText()
   {
    ID = string.Concat(this.UniqueID, "GoToPage"),
    Name = string.Concat(this.UniqueID, "GoToPage")
   };
            htmlInputText.Attributes.Add("style","width:30px; background:#f5f5f5; color:#3399d5;");
            htmlInputText.Attributes.Add("class","pageclassstyle");
   htmlInputText.Value = this.CurrentPage.ToString();
            //转到
   hyperLink = new HyperLink();
   //hyperLink.Attributes.Add("class", this.PagerCssClass);
            hyperLink.Attributes.Add("class","pageclassstyle");
   if (this.PageCount <= 1)
   {
    hyperLink.Enabled = false;
   }
   else
   {
    object[] uniqueID = new object[] { "javascript:", this.UniqueID, "ChangePage(", this.PageCount, ",document.getElementById('", htmlInputText.ClientID, "').value);" };
    hyperLink.NavigateUrl = string.Concat(uniqueID);
   }
   hyperLink.Text = "转到";
   htmlTableCell.Controls.Add(hyperLink);

            //页数
   htmlTableCell.Controls.Add(htmlInputText);

            //汉子页
   htmlTableCell.Controls.Add(new LiteralControl("<a class=\"pageclassstyle\">页</a>"));
   htmlTableRow.Cells.Add(htmlTableCell);

            //汉子第m/n页
   htmlTableCell = new HtmlTableCell()
   {
    Align = "right"
   };
   if (this.TableCellCssClass.Length != 0)
   {
    htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
   }
   ControlCollection controlCollections = htmlTableCell.Controls;
   object[] currentPage = new object[] { "第&nbsp;","<i style=\"color:#056dae;font-style: normal;\">", this.CurrentPage, "&nbsp;/&nbsp;", this.PageCount, "</i> &nbsp;页&nbsp;" };
   controlCollections.Add(new LiteralControl(string.Concat(currentPage)));
   htmlTableRow.Cells.Add(htmlTableCell);

   htmlTable.Rows.Add(htmlTableRow);
   htmlTable.RenderControl(writer);
   htmlInputHidden.RenderControl(writer);
  }

  private string[] GetPagerLinks()
  {
   string str;
   string str1;
   string str2;
   string str3;
   string[] strArrays = new string[5];
   int num = 1;
   int num1 = 1;
   if (this.RecordCount <= 0)
   {
    this.CurrentPage = 0;
    return strArrays;
   }
   string pagerUrl = this.GetPagerUrl();
   num = (this.CurrentPage > 1 ? this.CurrentPage - 1 : 1);
   num1 = (this.CurrentPage + 1 > this.PageCount ? this.PageCount : this.CurrentPage + 1);
   string[] strArrays1 = strArrays;
   if (this.CurrentPage == 1)
   {
    str = null;
   }
   else
   {
    str = "1";
   }
   strArrays1[0] = str;
   string[] strArrays2 = strArrays;
   if (this.CurrentPage == num)
   {
    str1 = null;
   }
   else
   {
    str1 = num.ToString();
   }
   strArrays2[1] = str1;
   string[] strArrays3 = strArrays;
   if (this.CurrentPage == num1)
   {
    str2 = null;
   }
   else
   {
    str2 = num1.ToString();
   }
   strArrays3[2] = str2;
   string[] strArrays4 = strArrays;
   if (this.CurrentPage == this.PageCount)
   {
    str3 = null;
   }
   else
   {
    str3 = this.PageCount.ToString();
   }
   strArrays4[3] = str3;
   strArrays[4] = pagerUrl;
   return strArrays;
  }

  private string GetPagerUrl()
  {
   StringBuilder stringBuilder = new StringBuilder(HttpContext.Current.Request.Path);
   stringBuilder.Append('?');
   NameValueCollection queryString = HttpContext.Current.Request.QueryString;
   for (int i = 0; i < queryString.Count; i++)
   {
    if (queryString.GetKey(i) != this.PageParamName)
    {
     stringBuilder.Append(queryString.GetKey(i));
     stringBuilder.Append('=');
     stringBuilder.Append(HttpContext.Current.Server.UrlEncode(queryString.Get(i)));
     stringBuilder.Append('&');
    }
   }
   stringBuilder.Append(this.PageParamName);
   stringBuilder.Append('=');
   return stringBuilder.ToString();
  }

  protected virtual void  OnPageIndexChange(EventArgs e)
  {
   if (this.PageIndexChange != null)
   {
    this.PageIndexChange(this, e);
   }
  }

  public void RaisePostBackEvent(string eventArgument)
  {
   this.CurrentPage = int.Parse(eventArgument);
   this.OnPageIndexChange(new EventArgs());
  }

  protected override void Render(HtmlTextWriter writer)
  {
   this.GetHTML(writer);
   base.Render(writer);
  }

  public event EventHandler PageIndexChange;
 }
}

分页.CSS:

.pageclassstyle
{
  display:block;float: left; cursor:pointer;
  width: 60px;
  height: 28px;
  border: 1px solid #ddd;
  text-align: center;
  line-height: 30px;
  border-left: none;
  }
 .pageclassstyle:hover{ color:#3399d5; background:#f5f5f5;}
.pageclassstylehome{ margin-left:35%;}
.wenjiancaozuo{ display:block; position:absolute; z-index:999;  color:#056dae !important; padding:2px 8px; border-radius:2px; font-size:12px;  margin-left: 152px;
  margin-top: -18px;}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WordPress自定义分页可以通过使用内置方法the_posts_pagination来输出分页式导航。这个方法可以根据后台设置的文章篇数来影响分页功能。使用方法如下: the_posts_pagination(参数); 其中,参数可以根据需要进行设置。你可以参考了解更多关于参数的说明和用法。 此外,你还可以在index.php页面中添加相应的代码来实现自定义分页。可以在样式文件中引入style.css,然后在代码中通过循环显示文章标题、发布时间等相关信息,并在需要显示分页的地方调用lingfeng_pagenavi()函数来实现分页导航。你可以参考了解更多关于index.php页面的代码示例。 如果你在WordPress官方或者其他资源中没有找到合适的分页件,可以尝试在搜索引擎中使用不同的关键词进行搜索,可能会找到适合你的需求的分页件。一些搜索引擎如搜狗也可以提供相关的搜索结果,可能会有适合你的分页件。你可以参考了解更多关于寻找WordPress分页件的经验。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [wordpress自定义分页件、获取文章中第1张图片](https://blog.csdn.net/tiz198183/article/details/125003497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值