使用者自訂控制項的應用。

來源:互聯網
上載者:User
控制項
asp.net中的 使用者自訂控制項 特點 1:實現服用;2:方便建立(相對與組件)。 以下為一個分頁導航條的sample, 接見於Webdiyer,相信很多人已經如雷貫耳了,我也不多介紹。本問只是簡單的 練習之作,沒有什麼深奧的演算法和架構。

----behindCode---------------------------------------------------------------------------------------------------------------

namespace GallonKit
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 /// <summary>
 ///  PageBar 的摘要說明。
 /// </summary>
 
 public delegate void PageLocalBtn(uint index);

 public class PageBar : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.LinkButton btn_fistPage;
  protected System.Web.UI.WebControls.LinkButton btn_lastPage;
  protected System.Web.UI.WebControls.LinkButton btn_prePage;
  protected System.Web.UI.WebControls.LinkButton btn_nextPage;
  protected System.Web.UI.WebControls.Label lb_PageCount;
  protected System.Web.UI.WebControls.TextBox tb_pageIndex;
  protected System.Web.UI.WebControls.LinkButton btn_local;
  public PageLocalBtn LocalBtnClick;
  

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者代碼以初始化頁面
  
  }

  private bool IsUint(string strInt)
  {
   try
   {
    uint.Parse(strInt);
   }
   catch(Exception e)
   {
    return false;
   }

   return true;
   
  }

  
  
  public uint pageIndex
  {
   get{
    if (this.IsUint(this.tb_pageIndex.Text.Trim()))
       return uint.Parse(this.tb_pageIndex.Text.Trim());
    return 0;
    }

   set{
    if (this.IsUint(value.ToString()))
     this.tb_pageIndex.Text = value.ToString();
    else
     throw new System.Exception("頁數範圍不正確!");

   }
  }

  

  public uint pageCount
  {
   get
   {
    object obj = ViewState["PageCount__"];
    return (obj == null)?0:(uint)obj;
   }

   set
   {
    
    if (this.IsUint(value.ToString()))
    {
     this.lb_PageCount.Text = value.ToString();
     ViewState["PageCount__"] = value;
    }
    else
     throw new System.Exception("頁數範圍不正確!");
   }

  }

  
  #region Web Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  設計器支援所需的方法 - 不要使用代碼編輯器
  ///  修改此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btn_fistPage.Click += new System.EventHandler(this.btn_fistPage_Click);
   this.btn_prePage.Click += new System.EventHandler(this.btn_prePage_Click);
   this.btn_nextPage.Click += new System.EventHandler(this.btn_nextPage_Click);
   this.btn_lastPage.Click += new System.EventHandler(this.btn_lastPage_Click);
   this.btn_local.Click += new System.EventHandler(this.btn_local_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void EnableAllBtn()
  {
   this.btn_fistPage.Enabled = true;
   this.btn_lastPage.Enabled = true;
   this.btn_prePage.Enabled  = true;
   this.btn_nextPage.Enabled = true;

  }

  private void ChangeBtnStatus(char BtnIndex)
  {
   switch(BtnIndex)
   {
    case 'F':
     this.btn_fistPage.Enabled = false;
     this.btn_prePage.Enabled = false;
     break;
    case 'L':
     this.btn_nextPage.Enabled = false;
     this.btn_lastPage.Enabled = false;
     break;
    case 'P':
     this.btn_prePage.Enabled = false;
     break;
    case 'N':
     this.btn_nextPage.Enabled = false;
     break;
   }
  }
  
  private void btn_lastPage_Click(object sender, System.EventArgs e)
  {
   EnableAllBtn();

   if( LocalBtnClick != null)
   {
    LocalBtnClick(this.pageCount);
   }

   this.ChangeBtnStatus('L');
  }

  private void btn_fistPage_Click(object sender, System.EventArgs e)
  {
   EnableAllBtn();
   if( LocalBtnClick != null)
   {
    LocalBtnClick(1);
   }

   this.ChangeBtnStatus('F');
  }

  private void btn_prePage_Click(object sender, System.EventArgs e)
  {
   EnableAllBtn();

   if (LocalBtnClick  == null) return;
   if (this.pageIndex>1)
    this.pageIndex--;
   else
   {
    this.ChangeBtnStatus('P');
    this.ChangeBtnStatus('F');
   }

   this.LocalBtnClick(this.pageIndex);
  
  }

  private void btn_nextPage_Click(object sender, System.EventArgs e)
  {
   EnableAllBtn();
   if (LocalBtnClick  == null) return;
   if (this.pageIndex<this.pageCount)
    this.pageIndex++;
   else
   {
    this.ChangeBtnStatus('N');
    this.ChangeBtnStatus('L');

   }

   this.LocalBtnClick(this.pageIndex);
  }

  private void btn_local_Click(object sender, System.EventArgs e)
  {
   EnableAllBtn();
   if (this.pageIndex <=1)
   {
    this.tb_pageIndex.Text = "1";
    this.ChangeBtnStatus('F');
   }
   else
   {
    if(this.pageIndex >= this.pageCount )
    {
     this.pageIndex = this.pageCount;
     this.ChangeBtnStatus('L');
    }
   }

   if( LocalBtnClick != null)
    LocalBtnClick(this.pageIndex);
   
  }
 }
}
====================aspx檔案=====================================================



 | 首頁 | 前頁 | 後頁 | 末頁 |1 Go |總頁數


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。