首页 > 编程 > .NET > 正文

Asp.net定时控件

2024-07-10 12:55:42
字体:
来源:转载
供稿:网友
 

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
 }
}


国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表