Asp.net定時控制項

來源:互聯網
上載者:User

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

[assembly:TagPrefix("Beyondbit.App.Web.UI.WebControls", "BBit")]
namespace Beyondbit.App.Web.UI.WebControls
{

 #region ElapsedEventArgs  class
 public class ElapsedEventArgs  : EventArgs
 {
  public int SignalTime;
 }
 #endregion
 
 /// <summary>
 /// 用戶端定時器
 /// </summary>
 [DefaultProperty("Enabled"),
  ToolboxData("<{0}:ClientTimer runat=server></{0}:ClientTimer>")]
 public class ClientTimer : System.Web.UI.Control , System.Web.UI.IPostBackEventHandler // , System.Web.UI.IPostBackDataHandler
 {
  public delegate void ElapsedEventHandler( object sender, ElapsedEventArgs e );
  public event ElapsedEventHandler Elapsed;

  protected virtual void OnElapsed( ElapsedEventArgs e)
  {
   if ( Elapsed != null )
    Elapsed(this, e);
  }

  private int signalTime = 0;

  private int interval = 0 ;
  [Description("設定或擷取定時時間(毫秒)")]
  public int Interval
  {
   set
   {
    if( value <= 0 )
     throw new ArgumentException( "Interval時間間隔不能小於或等於0" );
    interval = value ;
   }
   get
   {
    return interval ;
   }
  }

  private bool enabled = false ;
  [Description("設定或擷取定時器是否啟用")]
  public bool Enabled
  {
   set{ enabled = value ; }
   get{ return enabled ;  }
  }

  //public bool AutoReset = false ;

  [Description("停止定時器")]
  public void Stop()
  {
   enabled = false ;
  }

  protected override void OnPreRender(EventArgs e)
  {
   base.OnPreRender (e);

   if( this.enabled == false ) return;

   if( interval == 0 ) throw new ArgumentException( "定時器已啟動,但Interval時間間隔沒有設定" );

   //js
   string jsId = this.UniqueID + "_js";
   string clientFunction = this.UniqueID + "_OnElapsed";

   signalTime = signalTime + this.interval ;

   string js = "<script language='javascript'>" +
    "window.setTimeout( "+clientFunction+" , "+ this.signalTime +" );" +
    "function " + clientFunction + "(){" +
     Page.GetPostBackEventReference( this , "" + this.signalTime ) +
    ";}" +
    "</script> ";

   Page.RegisterStartupScript( jsId , js );

  }

  /// <summary>
  /// 將此控制項呈現給指定的輸出參數。
  /// </summary>
  /// <param name="output"> 要寫出到的 HTML 編寫器 </param>
  protected override void Render(HtmlTextWriter output)
  {
   //output.Write(Text);
  }

  #region IPostBackEventHandler 成員

  public void RaisePostBackEvent( string eventArgument )
  {
   if( eventArgument == "" ) throw new Exception( "eventArgument參數為空白!" );
   signalTime = int.Parse( eventArgument ) ;

   ElapsedEventArgs e = new ElapsedEventArgs();
   e.SignalTime = signalTime ;

   this.OnElapsed( e );
  }

  #endregion

  #region IPostBackDataHandler 成員

//  public void RaisePostDataChangedEvent()
//  {
//   // TODO:  添加 ClientTimer.RaisePostDataChangedEvent 實現
//  }
//
//  public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
//  {
//   // TODO:  添加 ClientTimer.LoadPostData 實現
//
//   string s = postCollection[ this.UniqueID + "_signalTime" ];
//   if( s == null || s == "" ) return false;
//
//   signalTime = Convert.ToInt32( s );
//
//   return false;
//  }

  #endregion
 }
}

相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.