也发一个自定义分页控件
生活随笔
收集整理的這篇文章主要介紹了
也发一个自定义分页控件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
分頁控件有頁面選擇10 20 50顯示,跳轉(zhuǎn)到第幾頁,上頁,下頁功能。如圖
代碼為:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CommModules.UI
{
??? /// <summary>
??? /// 分頁控件,頁面選擇10 20 50顯示,跳轉(zhuǎn)到第幾頁,上頁下頁.
??? /// </summary>
??? class ctrlPagination : Label, INamingContainer
??? {
??????? #region "定義包含控件名稱"
??????? /// <summary>
??????? /// 每頁顯示10條記錄
??????? /// </summary>
??????? public Button TenButton;
??????? /// <summary>
??????? /// 每頁顯示20條記錄
??????? /// </summary>
??????? public Button TwentyButton;
??????? /// <summary>
??????? /// 每頁顯示50條記錄
??????? /// </summary>
??????? public Button FiftyButton;
??????? /// <summary>
??????? /// 上一頁 帶連接(有上一頁)
??????? /// </summary>
??????? public LinkButton previousButton;
??????? /// <summary>
??????? /// 下一頁 帶連接(有下一頁)
??????? /// </summary>
??????? public LinkButton nextButton;
??????? /// <summary>
??????? /// 上一頁 不帶連接(無上一頁)
??????? /// </summary>
??????? public Literal previousLabel;
??????? /// <summary>
??????? /// 下一頁 不帶連接(無有下一頁)
??????? /// </summary>
??????? public Literal nextLabel;
??????? /// <summary>
??????? /// "每頁顯示"說明,在10butten前
??????? /// </summary>
??????? public Literal TenLabel;
??????? /// <summary>
??????? /// 錯誤提示暫時不使用
??????? /// </summary>
??????? public Label litError;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)到第幾頁的頁碼輸入框
??????? /// </summary>
??????? public TextBox txtGO;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)button
??????? /// </summary>
??????? public Button lbtnGO;
??????? #endregion
??????? #region "頁面重寫函數(shù)"
??????? /// <summary>
??????? /// 重寫 提供使 ASP.NET 服務器控件能夠維護其子控件列表的集合容器
??????? /// </summary>
??????? ///
??????? public override ControlCollection Controls
??????? {
??????????? get
??????????? {
??????????????? return base.Controls;
??????????? }
??????? }
??????? /// <summary>
??????? /// 創(chuàng)建子控件
??????? /// </summary>
??????? protected override void CreateChildControls()
??????? {
??????????? Controls.Clear();
??????????? //定義顯示控件
??????????? AddControls();
??????? }
??????? /// <summary>
??????? /// 呈現(xiàn)控件到頁面上
??????? /// </summary>
??????? /// <param name="writer"></param>
??????? protected override void Render(HtmlTextWriter writer)
??????? {
??????????? int totalPages = CalculateTotalPages();
??????????? AddAttributesToRender(writer);
??????????? writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "lblTenStart";
??????????? if (!IsEN)
??????????? {
??????????????? TenLabel.Text = "每頁顯示 ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "Every Page: ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? TenButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "lblTenEnd";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? TwentyButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp2";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? FiftyButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp4";
??????????? //int intStartRecord=1;
??????????? //int intEndRecord=10;
??????????? //intStartRecord=Convert.ToInt32(PageIndex ) * PageSize + 1;
??????????? //intEndRecord=Convert.ToInt32(PageIndex ) * PageSize+PageSize;
??????????? //if (intEndRecord > TotalRecords)
??????????? //{
??????????? //??? intEndRecord = TotalRecords;
??????????? //}
??????????? //string strStartRow = "0";
??????????? //if (TotalRecords > 0)
??????????? //{
??????????? //??? strStartRow = intStartRecord.ToString();
??????????? //}
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = " Total Records: " + TotalRecords.ToString() + " ";//, Current Records <strong>" + strStartRow + "</strong>~<strong>" + intEndRecord.ToString() + "</strong> ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "條? 共 <strong>" + TotalRecords.ToString() + "</strong> 條";//,當前是第 <strong>" + strStartRow + "</strong>~<strong>" + intEndRecord.ToString() + "</strong> 條 ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp3";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? //上一頁
??????????? if (HasPrevious)
??????????? {
??????????????? previousButton.RenderControl(writer);
??????????? }
??????????? else
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? previousLabel = new Literal();
??????????????????? previousLabel.ID = "Previous";
??????????????????? previousLabel.Text = "<span>" + this.Previous + "</span>";
??????????????????? previousLabel.RenderControl(writer);
??????????????? }
??????????????? else
??????????????? {
??????????????????? previousLabel = new Literal();
??????????????????? previousLabel.ID = "上 頁";
??????????????????? previousLabel.Text = "<span>" + this.Previous + "</span>";
??????????????????? previousLabel.RenderControl(writer);
??????????????? }
??????????? }
??????????? // 第 1/ 1 頁
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp4";
??????????? string strStartPage = "0";
??????????? if (TotalRecords > 0)
??????????? {
??????????????? strStartPage = Convert.ToInt32(PageIndex + 1).ToString();
??????????? }
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = "? <strong>" + strStartPage + "</strong>/ <strong>" + CalculateTotalPages().ToString() + "</strong> ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = " 第 <strong>" + strStartPage + "</strong>/ <strong>" + CalculateTotalPages().ToString() + " </strong>頁 ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? //下一頁
??????????? if (HasNext)
??????????? {
??????????????? nextButton.Text = this.Next;
??????????????? nextButton.RenderControl(writer);
??????????? }
??????????? else
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? nextLabel = new Literal();
??????????????????? nextLabel.ID = "Next";
??????????????????? nextLabel.Text = "<span>" + this.Next + "</span>";
??????????????????? nextLabel.RenderControl(writer);
??????????????? }
??????????????? else
??????????????? {
??????????????????? nextLabel = new Literal();
??????????????????? nextLabel.ID = "下 頁";
??????????????????? nextLabel.Text = "<span>" + this.Next + "</span>";
??????????????????? nextLabel.RenderControl(writer);
??????????????? }
??????????? }
??????????? //轉(zhuǎn)到? 頁 GO
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp5";
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = " Goto ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = " 跳轉(zhuǎn)";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? if (TotalRecords > 0)
??????????? {
??????????????? int intGo = PageIndex + 1;
??????????????? txtGO.Text = intGo.ToString();
??????????? }
??????????? txtGO.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp5";
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = "";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "頁";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? litError = new Label();
??????????? litError.ForeColor = System.Drawing.Color.Red;
??????????? litError.RenderControl(writer);
??????????? lbtnGO.RenderControl(writer);
??????? }
??????? #endregion
??????? /// <summary>
??????? /// 設置分頁控件中子控件的樣式
??????? /// </summary>
??????? private void PageSizeStyle()
??????? {
??????????? if (PageSize == 10)
??????????? {
??????????????? TenButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? FiftyButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.BackColor = System.Drawing.Color.White;
??????????????? TenButton.ForeColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.Black;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????????? if (PageSize == 20)
??????????? {
??????????????? TwentyButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? FiftyButton.BackColor = System.Drawing.Color.White;
??????????????? TenButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.Black;
??????????????? TenButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????????? if (PageSize == 50)
??????????? {
??????????????? FiftyButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? TenButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.BackColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.White;
??????????????? TenButton.ForeColor = System.Drawing.Color.Black;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????? }
??????? #region "實例定義的控件"
??????? /// <summary>
??????? /// 實例化分頁控件的子控件
??????? /// </summary>
??????? void AddControls()
??????? {
??????????? TenButton = new Button();
??????????? TenButton.ID = "TenButton";
??????????? TenButton.Text = "10";
??????????? TenButton.ToolTip = "10";
??????????? TenButton.CommandArgument = "10";
??????????? TenButton.Width = 22;
??????????? TenButton.Height = 20;
??????????? TenButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? TenButton.BorderStyle = BorderStyle.Solid;
??????????? TenButton.BorderWidth = 1;
??????????? TenButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(TenButton);
??????????? TwentyButton = new Button();
??????????? TwentyButton.ID = "TwentyButton";
??????????? TwentyButton.Text = "20";
??????????? TwentyButton.ToolTip = "20";
??????????? TwentyButton.CommandArgument = "20";
??????????? TwentyButton.Width = 22;
??????????? TwentyButton.Height = 20;
??????????? TwentyButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? TwentyButton.BorderStyle = BorderStyle.Solid;
??????????? TwentyButton.BorderWidth = 1;
??????????? TwentyButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(TwentyButton);
??????????? FiftyButton = new Button();
??????????? FiftyButton.ID = "FiftyButton";
??????????? FiftyButton.Text = "50";
??????????? FiftyButton.ToolTip = "50";
??????????? FiftyButton.CommandArgument = "50";
??????????? FiftyButton.Width = 22;
??????????? FiftyButton.Height = 20;
??????????? FiftyButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? FiftyButton.BorderStyle = BorderStyle.Solid;
??????????? FiftyButton.BorderWidth = 1;
??????????? FiftyButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(FiftyButton);
??????????? //設置默認樣式
??????????? PageSizeStyle();
??????????? //上一頁
??????????? previousButton = new LinkButton();
??????????? previousButton.ID = "previousButton";
??????????? previousButton.Text = this.Previous;
??????????? previousButton.ToolTip = this.Previous;
??????????? previousButton.CommandArgument = "Prv";
??????????? previousButton.Click += new System.EventHandler(PageIndex_Click);
??????????? Controls.Add(previousButton);
??????????? // 下一頁
??????????? nextButton = new LinkButton();
??????????? nextButton.ID = "nextButton";
??????????? nextButton.Text = this.Next;
??????????? nextButton.ToolTip = this.Next;
??????????? nextButton.CommandArgument = "Next";
??????????? nextButton.Click += new System.EventHandler(PageIndex_Click);
??????????? Controls.Add(nextButton);
??????????? //跳轉(zhuǎn)
??????????? txtGO = new TextBox();
??????????? txtGO.ID = "txtGO";
??????????? txtGO.Text = string.Empty;
??????????? txtGO.Width = 30;
??????????? txtGO.MaxLength = 5;
??????????? Controls.Add(txtGO);
??????????? lbtnGO = new Button();
??????????? lbtnGO.ID = "lbtnGO";
??????????? lbtnGO.Text = "GO? ";
??????????? lbtnGO.Width = 38;
??????????? lbtnGO.Attributes.Add("onclick", "return CtrlPagingGoValid(this,'" + txtGO.ClientID + "'," + CalculateTotalPages().ToString() + "," + (string.IsNullOrEmpty(txtGO.Text) ? Convert.ToString(this.PageIndex + 1) : txtGO.Text) + "," + this.IsEN.ToString().ToLower() + ")");
??????????? lbtnGO.CssClass = "gobutton";
??????????? litError = new Label();
??????????? litError.ID = "litError";
??????????? //lbtnGO.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? //lbtnGO.BorderStyle = BorderStyle.Solid;
??????????? //lbtnGO.BorderWidth = 1;
??????????? //lbtnGO.BackColor = System.Drawing.Color.LightGray;
??????????? //lbtnGO.CommandArgument = txtGO.Text;
??????????? lbtnGO.Click += new System.EventHandler(PageGo_Click);
??????????? Controls.Add(lbtnGO);
??????? }
??????? #endregion
??????? #region "判斷上一頁是否到了第一頁 或者到了最后一頁"
??????? /// <summary>
??????? /// 判斷是否有上一頁
??????? /// </summary>
??????? private bool HasPrevious
??????? {
??????????? get
??????????? {
??????????????? if (PageIndex > 0)
??????????????????? return true;
??????????????? return false;
??????????? }
??????? }
??????? /// <summary>
??????? /// 判斷是否有下一頁
??????? /// </summary>
??????? private bool HasNext
??????? {
??????????? get
??????????? {
??????????????? if (PageIndex + 1 < CalculateTotalPages())
??????????????????? return true;
??????????????? return false;
??????????? }
??????? }
??????? #endregion
??????? #region "控件暴露給外面的事件"
??????? /// <summary>
??????? /// 暴露控件外上一頁,下一頁事件
??????? /// </summary>
??????? public event System.EventHandler IndexChanged;
??????? /// <summary>
??????? /// 上一頁,下一頁事件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void PageIndex_Click(Object sender, EventArgs e)
??????? {
??????????? string strMark = ((LinkButton)sender).CommandArgument.ToString();
??????????? if (strMark == "Prv")
??????????? {
??????????????? PageIndex = PageIndex - 1;
??????????? }
??????????? else
??????????? {
??????????????? PageIndex = PageIndex + 1;
??????????? }
??????????? if (null != IndexChanged)
??????????????? IndexChanged(sender, e);
??????? }
??????? /// <summary>
??????? /// 暴露控件外每頁顯示條數(shù)事件 10 20 50 三個規(guī)格
??????? /// </summary>
??????? public event System.EventHandler RecordsChanged;
??????? /// <summary>
??????? /// 每頁顯示條數(shù)事件 10 20 50 三個規(guī)格
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void RecordsChanged_Click(Object sender, EventArgs e)
??????? {
??????????? PageSize = Convert.ToInt32(((Button)sender).CommandArgument);
??????????? PageSizeStyle();
??????????? PageIndex = 0;
??????????? txtGO.Text = "";
??????????? if (null != RecordsChanged)
??????????????? RecordsChanged(sender, e);
??????? }
??????? /// <summary>
??????? /// 暴露控件外跳轉(zhuǎn)頁面事件
??????? /// </summary>
??????? public event System.EventHandler PageGo;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)頁面事件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void PageGo_Click(Object sender, EventArgs e)
??????? {
??????????? try
??????????? {
??????????????? int.Parse(txtGO.Text);
??????????? }
??????????? catch
??????????? {
??????????????? return;
??????????? }
??????????? if (CalculateTotalPages() < int.Parse(txtGO.Text))
??????????? {
??????????????? txtGO.Text = CalculateTotalPages().ToString();
??????????? }
??????????? litError.Text = "";
??????????? if (int.Parse(txtGO.Text) < CalculateTotalPages())
??????????? {
??????????????? PageIndex = Convert.ToInt32(int.Parse(txtGO.Text) - 1);
??????????? }
??????????? else
??????????? {
??????????????? PageIndex = CalculateTotalPages() - 1;
??????????? }
??????????? if (null != PageGo)
??????????????? PageGo(sender, e);
??????? }
??????? #endregion
??????? #region "根據(jù)每頁顯示數(shù)目和總記錄數(shù)獲取顯示頁數(shù)"
??????? /// <summary>
??????? /// 獲取總共多少頁數(shù)據(jù)
??????? /// </summary>
??????? /// <returns></returns>
??????? public int CalculateTotalPages()
??????? {
??????????? int totalPagesAvailable;
??????????? if (TotalRecords == 0)
??????????????? return 0;
??????????? totalPagesAvailable = TotalRecords / PageSize;
??????????? if ((TotalRecords % PageSize) > 0)
??????????????? totalPagesAvailable++;
??????????? return totalPagesAvailable;
??????? }
??????? /// <summary>
??????? /// 向頁面注冊腳本數(shù)據(jù)
??????? /// </summary>
??????? /// <param name="e"></param>
??????? protected override void OnPreRender(EventArgs e)
??????? {
??????????? //暫時不需要腳本的提示,還是頁面作判斷
??????????? //Page.RegisterStartupScript("PagerValidScript", s_Js);
??????????? base.OnPreRender(e);
??????? }
??????? #endregion
??????? #region "屬性"
??????? /// <summary>
??????? /// 當前頁索引
??????? /// </summary>
??????? public int PageIndex
??????? {
??????????? get
??????????? {
??????????????? int _pageIndex = 0;
??????????????? if (ViewState["PPageIndex"] == null)
??????????????? {
??????????????????? _pageIndex = 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? _pageIndex = Convert.ToInt32(ViewState["PPageIndex"]);
??????????????? }
??????????????? if (_pageIndex < 0)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return _pageIndex;
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["PPageIndex"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 每頁顯示記錄條數(shù)
??????? /// </summary>
??????? public int PageSize
??????? {
??????????? get
??????????? {
??????????????? if (ViewState["PageSize"] == null)
??????????????? {
??????????????????? return 10;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return Convert.ToInt32(ViewState["PageSize"]);
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["PageSize"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 總記錄條數(shù)
??????? /// </summary>
??????? public int TotalRecords
??????? {
??????????? get
??????????? {
??????????????? return Convert.ToInt32(ViewState["TotalRecords"]);
??????????? }
??????????? set
??????????? {
??????????????? ViewState["TotalRecords"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 上一頁
??????? /// </summary>
??????? private string Previous
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "Previous";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "上 頁";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 下一頁面
??????? /// </summary>
??????? private string Next
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "Next";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "下 頁";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 跳轉(zhuǎn)
??????? /// </summary>
??????? private string GoTo
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "GO";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "GO";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 是否英文
??????? /// </summary>
??????? public bool IsEN
??????? {
??????????? get
??????????? {
??????????????? if (ViewState["IsEN"] == null)
??????????????????? return false;
??????????????? else
??????????????????? return Convert.ToBoolean(ViewState["IsEN"]);
??????????? }
??????????? set
??????????? {
??????????????? ViewState["IsEN"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 是否英文
??????? /// </summary>
??????? public string strGoTxt
??????? {
??????????? get
??????????? {
??????????????? return txtGO.Text;
??????????? }
??????????? set
??????????? {
??????????????? txtGO.Text = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 當前頁索引
??????? /// </summary>
??????? public int CurrentIndex
??????? {
??????????? get
??????????? {
??????????????? int _pageIndex = 0;
??????????????? if (ViewState["PPageIndex"] == null)
??????????????? {
??????????????????? _pageIndex = 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? _pageIndex = Convert.ToInt32(ViewState["PPageIndex"]);
??????????????? }
??????????????? if (_pageIndex <= 0)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? if (_pageIndex >= TotalRecords / PageSize)
??????????????????? {
??????????????????????? if (TotalRecords % PageSize > 0)
??????????????????????? {
??????????????????????????? PageIndex = TotalRecords / PageSize;
??????????????????????????? return TotalRecords / PageSize;
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? PageIndex = TotalRecords / PageSize - 1;
??????????????????????????? return TotalRecords / PageSize - 1;
??????????????????????? }
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? return _pageIndex;
??????????????????? }
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["CurrentIndex"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 注冊到頁面驗證
??????? /// </summary>
??????? public const string s_Js = @"
?? ??? ?<script type=""text/javascript"" language=""javascript"">
?? ??? ??? ?function CtrlPagingGoValid(sender, target, PageNum, curIndex, IsEn)
?? ??? ??? ?{
?? ??? ??? ??? ?var curNum = document.getElementById(target).value.replace(/(^"s*)|("s*$)/g, """").replace(/(^" *)|(" *$)/g, """");
?? ??? ??? ??? ?document.getElementById(target).value = curNum;
?? ??? ??? ??? ?var numreg = new RegExp(""^""d+$"");
?? ??? ??? ??? ?if (curNum.length == 0 || !numreg.test(curNum) )
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (IsEn)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?alert(""Page Index Error!"");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?alert(""頁碼錯誤!"");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ?</script>
??????? ";
??????? #endregion
??? }
}
調(diào)用代碼:int intStart, intEnd, intRows;//intRows返回查詢數(shù)據(jù)集合的總記錄數(shù)量
intRows = 0;
//設置其實記錄位置
intStart = CtrlPag.PageIndex * CtrlPag.PageSize; //獲取SQLRead的開始記錄
intEnd = CtrlPag.PageSize;?????????????????????? //獲取記錄條數(shù)量
DataTable dtb;? //獲取查詢記錄的總記錄數(shù)
CtrlPag.TotalRecords = intRows;//給控件設置總記錄數(shù)
GrideView.PageSize = CtrlPag.PageSize; //為GrideView設置當前頁顯示記錄條數(shù)
//下面就不用解釋了哦
GrideView.DataSource = dtb;
GrideView.DataBind();
代碼為:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace CommModules.UI
{
??? /// <summary>
??? /// 分頁控件,頁面選擇10 20 50顯示,跳轉(zhuǎn)到第幾頁,上頁下頁.
??? /// </summary>
??? class ctrlPagination : Label, INamingContainer
??? {
??????? #region "定義包含控件名稱"
??????? /// <summary>
??????? /// 每頁顯示10條記錄
??????? /// </summary>
??????? public Button TenButton;
??????? /// <summary>
??????? /// 每頁顯示20條記錄
??????? /// </summary>
??????? public Button TwentyButton;
??????? /// <summary>
??????? /// 每頁顯示50條記錄
??????? /// </summary>
??????? public Button FiftyButton;
??????? /// <summary>
??????? /// 上一頁 帶連接(有上一頁)
??????? /// </summary>
??????? public LinkButton previousButton;
??????? /// <summary>
??????? /// 下一頁 帶連接(有下一頁)
??????? /// </summary>
??????? public LinkButton nextButton;
??????? /// <summary>
??????? /// 上一頁 不帶連接(無上一頁)
??????? /// </summary>
??????? public Literal previousLabel;
??????? /// <summary>
??????? /// 下一頁 不帶連接(無有下一頁)
??????? /// </summary>
??????? public Literal nextLabel;
??????? /// <summary>
??????? /// "每頁顯示"說明,在10butten前
??????? /// </summary>
??????? public Literal TenLabel;
??????? /// <summary>
??????? /// 錯誤提示暫時不使用
??????? /// </summary>
??????? public Label litError;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)到第幾頁的頁碼輸入框
??????? /// </summary>
??????? public TextBox txtGO;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)button
??????? /// </summary>
??????? public Button lbtnGO;
??????? #endregion
??????? #region "頁面重寫函數(shù)"
??????? /// <summary>
??????? /// 重寫 提供使 ASP.NET 服務器控件能夠維護其子控件列表的集合容器
??????? /// </summary>
??????? ///
??????? public override ControlCollection Controls
??????? {
??????????? get
??????????? {
??????????????? return base.Controls;
??????????? }
??????? }
??????? /// <summary>
??????? /// 創(chuàng)建子控件
??????? /// </summary>
??????? protected override void CreateChildControls()
??????? {
??????????? Controls.Clear();
??????????? //定義顯示控件
??????????? AddControls();
??????? }
??????? /// <summary>
??????? /// 呈現(xiàn)控件到頁面上
??????? /// </summary>
??????? /// <param name="writer"></param>
??????? protected override void Render(HtmlTextWriter writer)
??????? {
??????????? int totalPages = CalculateTotalPages();
??????????? AddAttributesToRender(writer);
??????????? writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "lblTenStart";
??????????? if (!IsEN)
??????????? {
??????????????? TenLabel.Text = "每頁顯示 ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "Every Page: ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? TenButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "lblTenEnd";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? TwentyButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp2";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? FiftyButton.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp4";
??????????? //int intStartRecord=1;
??????????? //int intEndRecord=10;
??????????? //intStartRecord=Convert.ToInt32(PageIndex ) * PageSize + 1;
??????????? //intEndRecord=Convert.ToInt32(PageIndex ) * PageSize+PageSize;
??????????? //if (intEndRecord > TotalRecords)
??????????? //{
??????????? //??? intEndRecord = TotalRecords;
??????????? //}
??????????? //string strStartRow = "0";
??????????? //if (TotalRecords > 0)
??????????? //{
??????????? //??? strStartRow = intStartRecord.ToString();
??????????? //}
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = " Total Records: " + TotalRecords.ToString() + " ";//, Current Records <strong>" + strStartRow + "</strong>~<strong>" + intEndRecord.ToString() + "</strong> ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "條? 共 <strong>" + TotalRecords.ToString() + "</strong> 條";//,當前是第 <strong>" + strStartRow + "</strong>~<strong>" + intEndRecord.ToString() + "</strong> 條 ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp3";
??????????? TenLabel.Text = " ";
??????????? TenLabel.RenderControl(writer);
??????????? //上一頁
??????????? if (HasPrevious)
??????????? {
??????????????? previousButton.RenderControl(writer);
??????????? }
??????????? else
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? previousLabel = new Literal();
??????????????????? previousLabel.ID = "Previous";
??????????????????? previousLabel.Text = "<span>" + this.Previous + "</span>";
??????????????????? previousLabel.RenderControl(writer);
??????????????? }
??????????????? else
??????????????? {
??????????????????? previousLabel = new Literal();
??????????????????? previousLabel.ID = "上 頁";
??????????????????? previousLabel.Text = "<span>" + this.Previous + "</span>";
??????????????????? previousLabel.RenderControl(writer);
??????????????? }
??????????? }
??????????? // 第 1/ 1 頁
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp4";
??????????? string strStartPage = "0";
??????????? if (TotalRecords > 0)
??????????? {
??????????????? strStartPage = Convert.ToInt32(PageIndex + 1).ToString();
??????????? }
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = "? <strong>" + strStartPage + "</strong>/ <strong>" + CalculateTotalPages().ToString() + "</strong> ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = " 第 <strong>" + strStartPage + "</strong>/ <strong>" + CalculateTotalPages().ToString() + " </strong>頁 ";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? //下一頁
??????????? if (HasNext)
??????????? {
??????????????? nextButton.Text = this.Next;
??????????????? nextButton.RenderControl(writer);
??????????? }
??????????? else
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? nextLabel = new Literal();
??????????????????? nextLabel.ID = "Next";
??????????????????? nextLabel.Text = "<span>" + this.Next + "</span>";
??????????????????? nextLabel.RenderControl(writer);
??????????????? }
??????????????? else
??????????????? {
??????????????????? nextLabel = new Literal();
??????????????????? nextLabel.ID = "下 頁";
??????????????????? nextLabel.Text = "<span>" + this.Next + "</span>";
??????????????????? nextLabel.RenderControl(writer);
??????????????? }
??????????? }
??????????? //轉(zhuǎn)到? 頁 GO
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp5";
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = " Goto ";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = " 跳轉(zhuǎn)";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? if (TotalRecords > 0)
??????????? {
??????????????? int intGo = PageIndex + 1;
??????????????? txtGO.Text = intGo.ToString();
??????????? }
??????????? txtGO.RenderControl(writer);
??????????? TenLabel = new Literal();
??????????? TenLabel.ID = "nbsp5";
??????????? if (IsEN)
??????????? {
??????????????? TenLabel.Text = "";
??????????? }
??????????? else
??????????? {
??????????????? TenLabel.Text = "頁";
??????????? }
??????????? TenLabel.RenderControl(writer);
??????????? litError = new Label();
??????????? litError.ForeColor = System.Drawing.Color.Red;
??????????? litError.RenderControl(writer);
??????????? lbtnGO.RenderControl(writer);
??????? }
??????? #endregion
??????? /// <summary>
??????? /// 設置分頁控件中子控件的樣式
??????? /// </summary>
??????? private void PageSizeStyle()
??????? {
??????????? if (PageSize == 10)
??????????? {
??????????????? TenButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? FiftyButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.BackColor = System.Drawing.Color.White;
??????????????? TenButton.ForeColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.Black;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????????? if (PageSize == 20)
??????????? {
??????????????? TwentyButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? FiftyButton.BackColor = System.Drawing.Color.White;
??????????????? TenButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.Black;
??????????????? TenButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????????? if (PageSize == 50)
??????????? {
??????????????? FiftyButton.BackColor = System.Drawing.Color.LightSkyBlue;
??????????????? TenButton.BackColor = System.Drawing.Color.White;
??????????????? TwentyButton.BackColor = System.Drawing.Color.White;
??????????????? FiftyButton.ForeColor = System.Drawing.Color.White;
??????????????? TenButton.ForeColor = System.Drawing.Color.Black;
??????????????? TwentyButton.ForeColor = System.Drawing.Color.Black;
??????????? }
??????? }
??????? #region "實例定義的控件"
??????? /// <summary>
??????? /// 實例化分頁控件的子控件
??????? /// </summary>
??????? void AddControls()
??????? {
??????????? TenButton = new Button();
??????????? TenButton.ID = "TenButton";
??????????? TenButton.Text = "10";
??????????? TenButton.ToolTip = "10";
??????????? TenButton.CommandArgument = "10";
??????????? TenButton.Width = 22;
??????????? TenButton.Height = 20;
??????????? TenButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? TenButton.BorderStyle = BorderStyle.Solid;
??????????? TenButton.BorderWidth = 1;
??????????? TenButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(TenButton);
??????????? TwentyButton = new Button();
??????????? TwentyButton.ID = "TwentyButton";
??????????? TwentyButton.Text = "20";
??????????? TwentyButton.ToolTip = "20";
??????????? TwentyButton.CommandArgument = "20";
??????????? TwentyButton.Width = 22;
??????????? TwentyButton.Height = 20;
??????????? TwentyButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? TwentyButton.BorderStyle = BorderStyle.Solid;
??????????? TwentyButton.BorderWidth = 1;
??????????? TwentyButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(TwentyButton);
??????????? FiftyButton = new Button();
??????????? FiftyButton.ID = "FiftyButton";
??????????? FiftyButton.Text = "50";
??????????? FiftyButton.ToolTip = "50";
??????????? FiftyButton.CommandArgument = "50";
??????????? FiftyButton.Width = 22;
??????????? FiftyButton.Height = 20;
??????????? FiftyButton.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? FiftyButton.BorderStyle = BorderStyle.Solid;
??????????? FiftyButton.BorderWidth = 1;
??????????? FiftyButton.Click += new System.EventHandler(RecordsChanged_Click);
??????????? Controls.Add(FiftyButton);
??????????? //設置默認樣式
??????????? PageSizeStyle();
??????????? //上一頁
??????????? previousButton = new LinkButton();
??????????? previousButton.ID = "previousButton";
??????????? previousButton.Text = this.Previous;
??????????? previousButton.ToolTip = this.Previous;
??????????? previousButton.CommandArgument = "Prv";
??????????? previousButton.Click += new System.EventHandler(PageIndex_Click);
??????????? Controls.Add(previousButton);
??????????? // 下一頁
??????????? nextButton = new LinkButton();
??????????? nextButton.ID = "nextButton";
??????????? nextButton.Text = this.Next;
??????????? nextButton.ToolTip = this.Next;
??????????? nextButton.CommandArgument = "Next";
??????????? nextButton.Click += new System.EventHandler(PageIndex_Click);
??????????? Controls.Add(nextButton);
??????????? //跳轉(zhuǎn)
??????????? txtGO = new TextBox();
??????????? txtGO.ID = "txtGO";
??????????? txtGO.Text = string.Empty;
??????????? txtGO.Width = 30;
??????????? txtGO.MaxLength = 5;
??????????? Controls.Add(txtGO);
??????????? lbtnGO = new Button();
??????????? lbtnGO.ID = "lbtnGO";
??????????? lbtnGO.Text = "GO? ";
??????????? lbtnGO.Width = 38;
??????????? lbtnGO.Attributes.Add("onclick", "return CtrlPagingGoValid(this,'" + txtGO.ClientID + "'," + CalculateTotalPages().ToString() + "," + (string.IsNullOrEmpty(txtGO.Text) ? Convert.ToString(this.PageIndex + 1) : txtGO.Text) + "," + this.IsEN.ToString().ToLower() + ")");
??????????? lbtnGO.CssClass = "gobutton";
??????????? litError = new Label();
??????????? litError.ID = "litError";
??????????? //lbtnGO.BorderColor = System.Drawing.Color.LightSkyBlue;
??????????? //lbtnGO.BorderStyle = BorderStyle.Solid;
??????????? //lbtnGO.BorderWidth = 1;
??????????? //lbtnGO.BackColor = System.Drawing.Color.LightGray;
??????????? //lbtnGO.CommandArgument = txtGO.Text;
??????????? lbtnGO.Click += new System.EventHandler(PageGo_Click);
??????????? Controls.Add(lbtnGO);
??????? }
??????? #endregion
??????? #region "判斷上一頁是否到了第一頁 或者到了最后一頁"
??????? /// <summary>
??????? /// 判斷是否有上一頁
??????? /// </summary>
??????? private bool HasPrevious
??????? {
??????????? get
??????????? {
??????????????? if (PageIndex > 0)
??????????????????? return true;
??????????????? return false;
??????????? }
??????? }
??????? /// <summary>
??????? /// 判斷是否有下一頁
??????? /// </summary>
??????? private bool HasNext
??????? {
??????????? get
??????????? {
??????????????? if (PageIndex + 1 < CalculateTotalPages())
??????????????????? return true;
??????????????? return false;
??????????? }
??????? }
??????? #endregion
??????? #region "控件暴露給外面的事件"
??????? /// <summary>
??????? /// 暴露控件外上一頁,下一頁事件
??????? /// </summary>
??????? public event System.EventHandler IndexChanged;
??????? /// <summary>
??????? /// 上一頁,下一頁事件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void PageIndex_Click(Object sender, EventArgs e)
??????? {
??????????? string strMark = ((LinkButton)sender).CommandArgument.ToString();
??????????? if (strMark == "Prv")
??????????? {
??????????????? PageIndex = PageIndex - 1;
??????????? }
??????????? else
??????????? {
??????????????? PageIndex = PageIndex + 1;
??????????? }
??????????? if (null != IndexChanged)
??????????????? IndexChanged(sender, e);
??????? }
??????? /// <summary>
??????? /// 暴露控件外每頁顯示條數(shù)事件 10 20 50 三個規(guī)格
??????? /// </summary>
??????? public event System.EventHandler RecordsChanged;
??????? /// <summary>
??????? /// 每頁顯示條數(shù)事件 10 20 50 三個規(guī)格
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void RecordsChanged_Click(Object sender, EventArgs e)
??????? {
??????????? PageSize = Convert.ToInt32(((Button)sender).CommandArgument);
??????????? PageSizeStyle();
??????????? PageIndex = 0;
??????????? txtGO.Text = "";
??????????? if (null != RecordsChanged)
??????????????? RecordsChanged(sender, e);
??????? }
??????? /// <summary>
??????? /// 暴露控件外跳轉(zhuǎn)頁面事件
??????? /// </summary>
??????? public event System.EventHandler PageGo;
??????? /// <summary>
??????? /// 跳轉(zhuǎn)頁面事件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? void PageGo_Click(Object sender, EventArgs e)
??????? {
??????????? try
??????????? {
??????????????? int.Parse(txtGO.Text);
??????????? }
??????????? catch
??????????? {
??????????????? return;
??????????? }
??????????? if (CalculateTotalPages() < int.Parse(txtGO.Text))
??????????? {
??????????????? txtGO.Text = CalculateTotalPages().ToString();
??????????? }
??????????? litError.Text = "";
??????????? if (int.Parse(txtGO.Text) < CalculateTotalPages())
??????????? {
??????????????? PageIndex = Convert.ToInt32(int.Parse(txtGO.Text) - 1);
??????????? }
??????????? else
??????????? {
??????????????? PageIndex = CalculateTotalPages() - 1;
??????????? }
??????????? if (null != PageGo)
??????????????? PageGo(sender, e);
??????? }
??????? #endregion
??????? #region "根據(jù)每頁顯示數(shù)目和總記錄數(shù)獲取顯示頁數(shù)"
??????? /// <summary>
??????? /// 獲取總共多少頁數(shù)據(jù)
??????? /// </summary>
??????? /// <returns></returns>
??????? public int CalculateTotalPages()
??????? {
??????????? int totalPagesAvailable;
??????????? if (TotalRecords == 0)
??????????????? return 0;
??????????? totalPagesAvailable = TotalRecords / PageSize;
??????????? if ((TotalRecords % PageSize) > 0)
??????????????? totalPagesAvailable++;
??????????? return totalPagesAvailable;
??????? }
??????? /// <summary>
??????? /// 向頁面注冊腳本數(shù)據(jù)
??????? /// </summary>
??????? /// <param name="e"></param>
??????? protected override void OnPreRender(EventArgs e)
??????? {
??????????? //暫時不需要腳本的提示,還是頁面作判斷
??????????? //Page.RegisterStartupScript("PagerValidScript", s_Js);
??????????? base.OnPreRender(e);
??????? }
??????? #endregion
??????? #region "屬性"
??????? /// <summary>
??????? /// 當前頁索引
??????? /// </summary>
??????? public int PageIndex
??????? {
??????????? get
??????????? {
??????????????? int _pageIndex = 0;
??????????????? if (ViewState["PPageIndex"] == null)
??????????????? {
??????????????????? _pageIndex = 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? _pageIndex = Convert.ToInt32(ViewState["PPageIndex"]);
??????????????? }
??????????????? if (_pageIndex < 0)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return _pageIndex;
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["PPageIndex"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 每頁顯示記錄條數(shù)
??????? /// </summary>
??????? public int PageSize
??????? {
??????????? get
??????????? {
??????????????? if (ViewState["PageSize"] == null)
??????????????? {
??????????????????? return 10;
??????????????? }
??????????????? else
??????????????? {
??????????????????? return Convert.ToInt32(ViewState["PageSize"]);
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["PageSize"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 總記錄條數(shù)
??????? /// </summary>
??????? public int TotalRecords
??????? {
??????????? get
??????????? {
??????????????? return Convert.ToInt32(ViewState["TotalRecords"]);
??????????? }
??????????? set
??????????? {
??????????????? ViewState["TotalRecords"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 上一頁
??????? /// </summary>
??????? private string Previous
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "Previous";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "上 頁";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 下一頁面
??????? /// </summary>
??????? private string Next
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "Next";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "下 頁";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 跳轉(zhuǎn)
??????? /// </summary>
??????? private string GoTo
??????? {
??????????? get
??????????? {
??????????????? if (IsEN)
??????????????? {
??????????????????? return "GO";
??????????????? }
??????????????? else
??????????????? {
??????????????????? return "GO";
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 是否英文
??????? /// </summary>
??????? public bool IsEN
??????? {
??????????? get
??????????? {
??????????????? if (ViewState["IsEN"] == null)
??????????????????? return false;
??????????????? else
??????????????????? return Convert.ToBoolean(ViewState["IsEN"]);
??????????? }
??????????? set
??????????? {
??????????????? ViewState["IsEN"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 是否英文
??????? /// </summary>
??????? public string strGoTxt
??????? {
??????????? get
??????????? {
??????????????? return txtGO.Text;
??????????? }
??????????? set
??????????? {
??????????????? txtGO.Text = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 當前頁索引
??????? /// </summary>
??????? public int CurrentIndex
??????? {
??????????? get
??????????? {
??????????????? int _pageIndex = 0;
??????????????? if (ViewState["PPageIndex"] == null)
??????????????? {
??????????????????? _pageIndex = 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? _pageIndex = Convert.ToInt32(ViewState["PPageIndex"]);
??????????????? }
??????????????? if (_pageIndex <= 0)
??????????????? {
??????????????????? return 0;
??????????????? }
??????????????? else
??????????????? {
??????????????????? if (_pageIndex >= TotalRecords / PageSize)
??????????????????? {
??????????????????????? if (TotalRecords % PageSize > 0)
??????????????????????? {
??????????????????????????? PageIndex = TotalRecords / PageSize;
??????????????????????????? return TotalRecords / PageSize;
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? PageIndex = TotalRecords / PageSize - 1;
??????????????????????????? return TotalRecords / PageSize - 1;
??????????????????????? }
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? return _pageIndex;
??????????????????? }
??????????????? }
??????????? }
??????????? set
??????????? {
??????????????? ViewState["CurrentIndex"] = value;
??????????? }
??????? }
??????? /// <summary>
??????? /// 注冊到頁面驗證
??????? /// </summary>
??????? public const string s_Js = @"
?? ??? ?<script type=""text/javascript"" language=""javascript"">
?? ??? ??? ?function CtrlPagingGoValid(sender, target, PageNum, curIndex, IsEn)
?? ??? ??? ?{
?? ??? ??? ??? ?var curNum = document.getElementById(target).value.replace(/(^"s*)|("s*$)/g, """").replace(/(^" *)|(" *$)/g, """");
?? ??? ??? ??? ?document.getElementById(target).value = curNum;
?? ??? ??? ??? ?var numreg = new RegExp(""^""d+$"");
?? ??? ??? ??? ?if (curNum.length == 0 || !numreg.test(curNum) )
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (IsEn)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?alert(""Page Index Error!"");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?alert(""頁碼錯誤!"");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?return true;
?? ??? ??? ?}
?? ??? ?</script>
??????? ";
??????? #endregion
??? }
}
調(diào)用代碼:int intStart, intEnd, intRows;//intRows返回查詢數(shù)據(jù)集合的總記錄數(shù)量
intRows = 0;
//設置其實記錄位置
intStart = CtrlPag.PageIndex * CtrlPag.PageSize; //獲取SQLRead的開始記錄
intEnd = CtrlPag.PageSize;?????????????????????? //獲取記錄條數(shù)量
DataTable dtb;? //獲取查詢記錄的總記錄數(shù)
CtrlPag.TotalRecords = intRows;//給控件設置總記錄數(shù)
GrideView.PageSize = CtrlPag.PageSize; //為GrideView設置當前頁顯示記錄條數(shù)
//下面就不用解釋了哦
GrideView.DataSource = dtb;
GrideView.DataBind();
轉(zhuǎn)載于:https://www.cnblogs.com/tomaggie/archive/2008/02/25/1080867.html
總結
以上是生活随笔為你收集整理的也发一个自定义分页控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP提取中文首字母
- 下一篇: 异常详细信息: System.Runti