<em id="hanht"></em>

    <dd id="hanht"></dd>

    <em id="hanht"><acronym id="hanht"></acronym></em>
    
    <button id="hanht"></button>
    <rp id="hanht"><object id="hanht"><blockquote id="hanht"></blockquote></object></rp><em id="hanht"></em>

    首頁 > 開發 > .Net > 正文

    自己開發的自定義分頁控件

    2023-04-28 11:48:22
    字體:
    來源:轉載
    供稿:網友

      一直以來都覺得分頁是麻煩的事情,.NET中雖然有DATAGRID的分頁,但用其他形式的列表仍然需要使用到分頁,一次一次的寫不利于效率及面向對象的方法,用類或用戶控件也總覺得怪怪的,用第3方的自己覺得不放心,也不利于自己進行修改,干脆就自己寫了一個。

      (另外注意:在控件編譯時,可以在AssemblyInfo.cs文件中設置控件的標簽和名稱空間,如:
           using System.Reflection;
           using System.Runtime.CompilerServices;
           using System.Web.UI; 
           [assembly: TagPrefix("PublicControls","PublicControls")]  // 自定義控件前綴
      第一個參數是名稱空間(必須是你的控件類的名稱空間),第二個是標簽名(可自定義)

      記得要加入System.Web.UI;名稱空間,另外 將控件類內的    [DefaultProperty("Text"), 
         ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]這句屏蔽掉

    所有代碼如下:

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



    namespace PublicControls
    {
         ///<summary>
         /// PageNavigation 分頁導航控件。
         ///</summary>
         [DefaultProperty("Text"), 
         ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]
         public class PageNavigation : Control,IPostBackDataHandler,IPostBackEventHandler 
         {
             #region預定義
             
             private string _style;
             private int _count;
             private int _pagesize;
             private int _pageindex;
            private int _pages;         //頁群
             private int _currentpages;
             private string _first;
             private string _pres;
             private string _pre;
             private string _next;
             private string _nexts;
             private string _last;


             ///<summary>
             ///委托頁面索引事件
             ///</summary>
             public event EventHandler PageIndexChange;

             #endregion

             #region屬性
             
             ///<summary>
             ///相關屬性樣式字符串屬性
             ///</summary>
             [Bindable(false), 
             Category("StyleString"), 
             DefaultValue("Align=left")] 
             public string StyleString 
             {
                  get
                  {
                       _style = (string)ViewState["StyleString"];
                       return _style;
                  }

                  set
                  {
                       _style= value;
                       ViewState["StyleString"] = _style;
                  }
             }

             ///<summary>
             ///記錄總數
             ///</summary>
             [Bindable(false), 
             Category("Count"), 
             DefaultValue(0)] 
             public int Count 
             {
                  get
                  {
                       if (this.IsNumeric(ViewState["Count"]))
                       {
                           _count = (int)ViewState["Count"];
                       }
                       return _count;
                  }

                  set
                  {
                       _count = value;
                       ViewState["Count"] = _count;
                       
                  }
             }

             ///<summary>
             ///頁面大小
             ///</summary>
             [Bindable(false), 
             Category("Pagesize"), 
             DefaultValue(10)] 
             public int Pagesize
             {
                  get
                  {
                       
                       if (this.IsNumeric(ViewState["Pagesize"]))
                       {
                           _pagesize = (int)ViewState["Pagesize"];
                       }
                       return _pagesize;
                  }

                  set
                  {
                       _pagesize = value;
                       ViewState["Pagesize"] = _pagesize;
                       
                  }
             }

             ///<summary>
             ///當前頁索引
             ///</summary>
             [Bindable(false), 
             Category("Pageindex"), 
             DefaultValue(0)] 
             public int Pageindex
             {
                  get
                  {
                       if (this.IsNumeric(ViewState["Pageindex"]))
                       {
                           _pageindex = (int)ViewState["Pageindex"];
                       }
                       return _pageindex;
                  }

                  set
                  {
                       _pageindex = value;
                       ViewState["Pageindex"] = _pageindex;
                  }
             }

             ///<summary>
             ///頁群屬性
             ///</summary>
             public int Pages
             {
                  get
                  {
                       if (this.IsNumeric(ViewState["Pages"]))
                       {
                           _pages = (int)ViewState["Pages"];
                       }
                       return _pages;
                  }
                  set
                  {
                       _pages = value;
                       ViewState["Pages"] = _pages;
                  }
             }

             ///<summary>
             ///當前頁群索引
             ///</summary>
             public int CurrentPages
             {
                  get
                  {
                       if (this.IsNumeric(ViewState["CurrentPages"]))
                       {
                           _currentpages = (int)ViewState["CurrentPages"];
                       }
                       return _currentpages;
                  }
                  set
                  {
                       _currentpages = value;
                       ViewState["CurrentPages"] = _currentpages;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("first"), 
             DefaultValue("第一頁")] 
             public string First 
             {
                  get
                  {
                       _first = (string)ViewState["First"];
                       return _first;
                  }

                  set
                  {
                       _first = value;
                       ViewState["First"] = _first;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("pres"), 
             DefaultValue("前N頁")] 
             public string Pres 
             {
                  get
                  {
                       _pres = (string)ViewState["Pres"];
                       return _pres;
                  }

                  set
                  {
                       _pres = value;
                       ViewState["Pres"] = _pres;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("pre"), 
             DefaultValue("前一頁")] 
             public string Pre 
             {
                  get
                  {
                       _pre = (string)ViewState["Pre"];
                       return _pre;
                  }

                  set
                  {
                       _pre = value;
                       ViewState["Pre"] = _pre;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("next"), 
             DefaultValue("后一頁")] 
             public string Next 
             {
                  get
                  {
                       _next = (string)ViewState["Next"];
                       return _next;
                  }

                  set
                  {
                       _next = value;
                       ViewState["Next"] = _next;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("nexts"), 
             DefaultValue("后N頁")] 
             public string Nexts 
             {
                  get
                  {
                       _nexts = (string)ViewState["Nexts"];
                       return _nexts;
                  }

                  set
                  {
                       _nexts = value;
                       ViewState["Nexts"] = _nexts;
                  }
             }

             ///<summary>
             ///標記樣式
             ///</summary>
             [Bindable(false), 
             Category("last"), 
             DefaultValue("最后一頁")] 
             public string Last 
             {
                  get
                  {
                       _last = (string)ViewState["Last"];
                       return _last;
                  }
                  set
                  {
                       _last = value;
                       ViewState["Last"] = _last;
                  }
             }


             #endregion

             #region事件
             ///<summary>
             ///當由類實現時,使服務器控件能夠處理將窗體發送到服務器時引發的事件。
             ///</summary>
             ///<param name="e">所傳遞的參數</param>
             public void RaisePostBackEvent(string e) 
             {
                  //當發生回送的時候改變控件當前索引
                  if(e=="top")
                  {
                       Pageindex = 0;
                       CurrentPages = 0 ;
                  }
                  else if(e=="last")
                  {
                       Pageindex = (Count+Pagesize-1)/Pagesize -1;
                       
                       CurrentPages = ((Count+Pagesize-1)/Pagesize+Pages-1)/ Pages -1;

                       if(Pageindex<0)
                       {
                           Pageindex=0;
                       }
                       if(CurrentPages<0)
                       {
                           CurrentPages=0;
                       }
                       
                  }
                  else if(e=="pre")
                  {
                       if(Pageindex>0)
                       {
                           Pageindex--;
                           if(Pageindex<CurrentPages*Pages && Pageindex>0)
                           {
                                CurrentPages--;
                           }
                       }
                  }
                  else if(e=="next")
                  {
                       if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
                       {
                           Pageindex++;
                           if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
                           {
                                CurrentPages++;
                           }
                       }
                  }
                  else if(e=="pres")
                  {
                       Pageindex -= Pages;
                       if(Pageindex<0)
                       {
                           Pageindex = 0;
                       }
                       if(Pageindex<CurrentPages*Pages && Pageindex>0)
                       {
                           CurrentPages--;
                       }
                  }
                  else if(e=="nexts")
                  {
                       Pageindex += Pages;
                       if(Pageindex>(Count+Pagesize-1)/Pagesize -1)
                       {
                           Pageindex = (Count+Pagesize-1)/Pagesize -1;
                       }
                       if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
                       {
                           CurrentPages++;
                       }
                  }
                  else
                  {
                       Pageindex = int.Parse(e.ToString());
                  }
             
                  //發生回送事件時引發OnPageIndexChange事件
                  OnPageIndexChange(System.EventArgs.Empty);
             }

             ///<summary>
             ///當由類實現時,為 ASP.NET 服務器控件處理回發數據。
             ///</summary>
             ///<param name="postDataKey">數據集合元素索引</param>
             ///<param name="values">string 的排序集合</param>
             ///<returns></returns>
             public bool LoadPostData(string postDataKey, NameValueCollection values) 
             {
                  //Pageindex = Int32.Parse(values[this.UniqueID].Split('|')[0]);
                  //CurrentPages = Int32.Parse(values[this.UniqueID].Split('|')[1]);
                  return false;
             }

             ///<summary>
             ///當由類實現時,用信號要求服務器控件對象通知 ASP.NET 應用程序該控件的狀態已更改。
             ///</summary>
             public void RaisePostDataChangedEvent() 
             {
             }

             ///<summary>
             ///當頁面索引改變,觸發委托
             ///</summary>
             ///<param name="e"></param>
             protected void OnPageIndexChange(System.EventArgs e)
             {    
                  //委托給加載控件頁的PageIndexChange事件
                  if (PageIndexChange!=null)
                  {
                       PageIndexChange(this,e);
                  }
             }

             ///<summary>
             ///預呈現
             ///</summary>
             ///<param name="e"></param>
             protected override void OnPreRender(EventArgs e) 
             {
             }

             #endregion

             #region輸出
             ///<summary>
             ///將此控件呈現給指定的輸出參數。
             ///</summary>
             ///<param name="output">要寫出到的 HTML 編寫器 </param>
             protected override void Render(HtmlTextWriter output)
             {

                  string Pagestring = "";  //定義頁碼呈現字符串
                  string Text = null;          //輸出主結構字符串變量定義
                  int NO;

                  //輸出主結構
                  Text = "<div $style$>";
                  
                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"top")+ "/">"+First+"</a> ";

                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"pres")+ "/">"+Pres+"</a> ";

                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"pre")+ "/">"+Pre+"</a> ";

                  Text +="$Pagestring$";

                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"next")+ "/">"+Next+"</a> ";

                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"nexts")+ "/">"+Nexts+"</a> ";
                  
                  Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this,"last")+ "/">"+Last+"</a> ";

                  Text +="</div>";//<input type = hidden name ="+this.UniqueID+" value = "+this.Pageindex+"|"+this.CurrentPages+">";     

                  //當頁大小小于0時,還原為1,防止預算出現除0錯誤
                  if (!(Pagesize>0))
                  {
                       Pagesize = 1;
                  }
                  
                  

                  //計算出總頁數,并循環輸出頁碼
                  for (int i = 0;i< Pages ;i++ )
                  {
                       //獲得頁群內頁碼
                       NO = Pages * CurrentPages + i;

                       if(NO<(Count+Pagesize-1)/Pagesize)
                       {
                           //判斷頁碼是否為當前頁
                           if (Pageindex != NO)
                           {
                                Pagestring += "<a href = /"javascript:"+ Page.GetPostBackEventReference(this,NO.ToString())+ "/">"+(NO+1).ToString()+"</a> ";
                           }
                                //如果不是,頁面部分無連接
                           else
                           {
                                Pagestring += (NO+1).ToString()+" ";
                           }
                       }
                  }

                  if (!(Pagestring.Trim().Length>0))
                  {
                       Pagestring = "1";
                  }
                  
                  //替換對齊屬性
                  Text = Text.Replace("$style$",StyleString);
                
                  //替換頁碼部分
                  Text = Text.Replace("$Pagestring$",Pagestring);
                  

                  output.Write(Text);
                  
             }
             #endregion

             #region其他函數
             ///<summary>
             ///判斷是否為數字字符串
             ///</summary>
             ///<param name="str">需驗證的字符串</param>
             ///<returns>判斷結果,符合條件為True,不符合條件為False</returns>
             public bool IsNumeric(string str)
             {
                  //判斷是否為空
                  if (str == null || str.Length==0) 
                  {
                       return false; 
                  }
                  //循環檢查每個字符
                  foreach(char c in str) 
                  { 
                       if (!Char.IsNumber(c)) 
                       { 
                           return false; 
                       } 
                  } 
                  return true; 
             }

             ///<summary>
             ///判斷是否為數字字符串
             ///</summary>
             ///<param name="obj">需驗證的字符串</param>
             ///<returns>判斷結果,符合條件為True,不符合條件為False</returns>
             public bool IsNumeric(object obj)
             {

                  obj += "";
                  string str = obj.ToString();
                  
                  //判斷是否為空
                  if (str == null || str.Length==0) 
                  {
                       return false; 
                  }
                  //循環檢查每個字符
                  foreach(char c in str) 
                  { 
                       if (!Char.IsNumber(c)) 
                       { 
                           return false; 
                       } 
                  } 
                  return true; 
             }

             #endregion
         }
    }

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    一级特黄大片欧美久久久久_一本一道久久综合狠狠老_JLZZ日本人年轻护士_欧美男男作爱VIDEOS可播放
      <em id="hanht"></em>

      <dd id="hanht"></dd>

      <em id="hanht"><acronym id="hanht"></acronym></em>
      
      <button id="hanht"></button>
      <rp id="hanht"><object id="hanht"><blockquote id="hanht"></blockquote></object></rp><em id="hanht"></em>