Asp.net定时控件
2008-03-08 12:55:37 来源:WEB开发网核心提示:using System;using System.Web.UI;using System.Web.UI.WebControls;using System.ComponentModel;[assembly:TagPRefix("Beyondbit.App.Web.UI.WebControls", &
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
}
}
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
}
}
- ››asp.net页面弄成伪静态页面
- ››Asp.net 中将汉字转换成拼音的方法
- ››ASP.NET及JS中的cookie基本用法
- ››ASP.NET获取MS SQL Server安装实例
- ››asp.net实现调用百度pai 在线翻译英文转中文
- ››ASP.NET页面选项进行提示判断
- ››Asp.net定时执行程序
- ››ASP.NET中利用DataList实现图片无缝滚动
- ››ASP.NET验证控件RequiredFieldValidator
- ››ASP.NET中使用System.Net.Mail发邮件
- ››ASP.NET中获取用户控件中控件的ID
- ››ASP.NET中FileBytes写成文件并存档
更多精彩
赞助商链接