一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

ASP.Net 分頁控件源碼

特性:
、支持datagrid,datalist等分種數據顯示控件,理論上只要有datasource的控件都可以
、支持url方式分頁,url分頁的每一頁都可以被搜索引擎搜到,并且支持url重寫
、控件樣式可自定義
、封裝完全,使用過程序只需寫一行代碼
復制代碼 代碼如下:
/******************************************************************
** 文件名:Pager.cs
** 創建人:楊響武
** 日 期:2005年7月27日
** 修改人:
** 日 期:
** 描 述:
**
**----------------------------------------------------------------
******************************************************************/

using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace MyCompany.Controls
{
/// <summary>
/// 分頁控件
/// </summary>
public class Pager : Label, INamingContainer
{
#region 成員變量和構造函數
HttpContext context = HttpContext.Current;
Label countLabel;
HyperLink previousButton;
HyperLink nextButton;
HyperLink firstButton;
HyperLink lastButton;
HyperLink[] pagingLinkButtons;

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

protected override void CreateChildControls()
{
Controls.Clear();

AddCountLabel();

AddPageButtons();

AddPreviousNextHyperLinks();

AddFirstLastHyperLinks();
}
#endregion

#region 呈現方法
protected override void Render(HtmlTextWriter writer)
{
// 修改總頁數小于等于1的時候不呈現任何控件
// int totalPages = CalculateTotalPages();
//
// if (totalPages <= 1)
// return;

AddAttributesToRender(writer);

writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);

RenderCountLabel(writer);

RenderFirst(writer);

RenderPrevious(writer);

RenderPagingButtons(writer);

RenderNext(writer);

RenderLast(writer);
}

void RenderCountLabel(HtmlTextWriter writer)
{
countLabel.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderFirst (HtmlTextWriter writer)
{
firstButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderLast (HtmlTextWriter writer)
{
lastButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderPrevious (HtmlTextWriter writer)
{
previousButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderNext(HtmlTextWriter writer)
{
nextButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

private void RenderButtonRange(int start, int end, HtmlTextWriter writer)
{
for (int i = start; i < end; i++)
{
if (PageIndex == i)
{
Literal l = new Literal();
l.Text = "<font color =red>" + (i + 1).ToString() + "</font>";

l.RenderControl(writer);
}
else
{
pagingLinkButtons.RenderControl(writer);
}
if (i < (end - 1))
writer.Write(" ");
}

LiteralControl l1 = new LiteralControl(" ");
l1.RenderControl(writer);
}

private void RenderPagingButtons(HtmlTextWriter writer)
{
int totalPages;

totalPages = CalculateTotalPages();

if (totalPages <= 10)
{
RenderButtonRange(0, totalPages, writer);
}
else
{
int lowerBound = (PageIndex - 4);
int upperBount = (PageIndex + 6);

if (lowerBound <= 0)
lowerBound = 0;

if (PageIndex <= 4)
RenderButtonRange(0, 10, writer);

else if (PageIndex < (totalPages - 5))
RenderButtonRange(lowerBound, (PageIndex + 6), writer);

else if (PageIndex >= (totalPages - 5))
RenderButtonRange((totalPages - 10), totalPages, writer);
}
}

#endregion

#region 控件樹方法
/// <summary>
/// 信息標簽
/// </summary>
void AddCountLabel()
{
countLabel = new Label();
countLabel.ID = "countLabel";
countLabel.Text = string.Format(text, CalculateTotalPages().ToString("n0"));

Controls.Add(countLabel);
}

private void AddPageButtons()
{
pagingLinkButtons = new HyperLink[CalculateTotalPages()];

for (int i = 0; i < pagingLinkButtons.Length; i++)
{
pagingLinkButtons = new HyperLink();
pagingLinkButtons.EnableViewState = false;
pagingLinkButtons.Text = (i + 1).ToString();
pagingLinkButtons.ID = i.ToString();
pagingLinkButtons.CssClass = linkCss;
pagingLinkButtons.NavigateUrl = GetHrefString(i);

Controls.Add(pagingLinkButtons);
}
}

/// <summary>
/// 首頁末頁
/// </summary>
void AddFirstLastHyperLinks()
{
firstButton = new HyperLink();
firstButton.ID = "First";
firstButton.Text = "首頁";
if(PageIndex != 0 && CalculateTotalPages() > 0)
{
firstButton.NavigateUrl = GetHrefString(0);
}
else
{
firstButton.Enabled = false;
}

Controls.Add(firstButton);

lastButton = new HyperLink();
lastButton.ID = "Last";
lastButton.Text = "末頁";
if(PageIndex != CalculateTotalPages() - 1)
{
lastButton.NavigateUrl = GetHrefString(CalculateTotalPages() - 1);
}
else
{
lastButton.Enabled = false;
}
Controls.Add(lastButton);
}

/// <summary>
///
/// </summary>
void AddPreviousNextHyperLinks()
{
previousButton = new HyperLink();
previousButton.ID = "Prev";
previousButton.Text = "";
if (HASPrevious)
{
previousButton.NavigateUrl = GetHrefString(PageIndex - 1);
}
else
{
previousButton.Enabled = false;
}
Controls.Add(previousButton);

nextButton = new HyperLink();
nextButton.ID = "Next";
nextButton.Text = "";
if(HasNext)
{
nextButton.NavigateUrl = GetHrefString(PageIndex + 1);
}
else
{
nextButton.Enabled = false;
}
Controls.Add(nextButton);
}
#endregion

#region 私有屬性
private bool HASPrevious
{
get
{
if (PageIndex > 0)
return true;

return false;
}
}

private bool HasNext
{
get
{
if (PageIndex + 1 < CalculateTotalPages() )
return true;

return false;
}
}
#endregion

#region 幫助方法和公共屬性

/// <summary>
/// 獲取分頁導航按鈕的超鏈接字符串
/// </summary>
/// <param name="pageIndex">該分頁按鈕相對應的頁索引</param>
/// <returns>分頁導航按鈕的超鏈接字符串</returns>
private string GetHrefString(int pageIndex)
{
string url = Page.Request.RawUrl;

if (Regex.IsMatch(url, @"(/d+).shtml", RegexOptions.IgnoreCase))
{
url = Regex.Replace(url, @"(/d+).shtml", pageIndex + ".shtml");
}

if (Regex.IsMatch(url, @"PageIndex=(/d+)", RegexOptions.IgnoreCase))
{
url = Regex.Replace(url, @"PageIndex=(/d+)", "PageIndex=" + pageIndex.ToString());
}

return Page.Server.UrlPathEncode(url);
}

public int CalculateTotalPages()
{
int totalPagesAvailable;

if (TotalRecords == 0)
return 0;

totalPagesAvailable = TotalRecords / PageSize;

if ((TotalRecords % PageSize) > 0)
totalPagesAvailable++;

return totalPagesAvailable;
}

/// <summary>
/// 當前頁
/// </summary>
public int PageIndex
{
get
{
int _pageIndex = 0;

if ((!Page.IsPostBack) && (context.PageIndex > 0))
_pageIndex = context.PageIndex;
else
_pageIndex = Convert.ToInt32(ViewState["PageIndex"]);

if (_pageIndex < 0)
return 0;
else
{
if (_pageIndex > CalculateTotalPages()-1)
return CalculateTotalPages()-1;
else
return _pageIndex;
}
}
set
{
ViewState["PageIndex"] = value;
}
}

/// <summary>
/// 每頁記錄數
/// </summary>
public int PageSize
{
get
{
int pageSize = Convert.ToInt32(ViewState["PageSize"]);

if (pageSize == 0)
{
return 10;
}

return pageSize;
}
set
{
ViewState["PageSize"] = value;
}

}

/// <summary>
/// 總記錄數
/// </summary>
public int TotalRecords
{
get
{
return Convert.ToInt32(ViewState["TotalRecords"]);
}
set
{
ViewState["TotalRecords"] = value;
}
}

private string text = "共{0}頁";

private string linkCss = "";
/// <summary>
/// 鏈接樣式
/// </summary>
public string LinkCss
{
get { return linkCss; }
set { linkCss = value;}
}

private string textCss = "";
/// <summary>
/// 文字樣式
/// </summary>
public string TextCss
{
get { return textCss; }
set { textCss = value;}
}

public string TextFormat
{
get
{
return text;
}
set
{
text = value;
}
}

#endregion
}
}

在頁面上只需要向下面這樣寫:
復制代碼 代碼如下:
<%@ Control Language="c#" %>
<%@ Register TagPrefix="Anchor" Namespace="MyCompany.Controls" Assembly="MyCompany.Controls" %>
<ANCHOR:PAGER id="Pager1" runat="server"></ANCHOR:PAGER>

AspNet技術ASP.Net 分頁控件源碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本久久精品免视看国产成人 | 国产精品久久久久久久久久一区 | 久久久久久久久女黄9999 | 成人在线激情 | 日韩一区二区三区免费 | 免费激情 | 亚洲乱码在线 | 91精品国 | 2021国产精品视频 | 天天操天天干天天爽 | 草草视频手机在线观看视频 | 伊人色综| 麻豆91在线播放 | 国产一级久久免费特黄 | 四川农村女人一级毛片 | 超清中文乱码精品字幕在线观看 | 91情侣高清精品国产 | 亚洲国产成人精品小蝌蚪 | 国产精品成人自拍 | 激情综合欧美 | 久久怡红院国产精品 | 91久久国产视频 | 亚洲视频二区 | aⅴ一区二区三区无卡无码 aaaaaa精品视频在线观看 | 91短视频在线 | 激情文学小说区另类小说同性 | 桃花视频在线观看高清版mv | 99久久伊人一区二区yy5o99 | 日韩综合nv一区二区在线观看 | 韩国精品一区二区久久 | 亚洲黄色在线观看 | 5g成人影院| 色婷婷精品视频 | 16女性下面扒开无遮挡免费 | 视频国产91| 黄色在线观看免费 | 欧美国产综合在线 | 国产精品伦理久久久久久 | 久久精品免费全国观看国产 | 黄色网页在线播放 | 国产麻豆91欧美一区二区 |