using
System;
using System.Web.UI;
using System.Text;
namespace Goodspeed.Web
{
/**//// <summary>
/// Summary description for WebTimer
/// </summary>
public class WebTimer:Control, IPostBackEventHandler
{
- Properties -#region - Properties -
private int _interval = 5000;
public int Interval
{
get { return _interval; }
set { _interval = value; }
}
private bool _enabled;
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
private static readonly object EventTimer = new object();
public bool Visible
{
get { return base.Visible; }
set { base.Visible = value; }
}
#endregion
- Constructor -#region - Constructor -
public WebTimer()
{
//
// TODO: Add constructor logic here
//
}
#endregion
- Event -#region - Event -
public event EventHandler Timer
{
add { Events.AddHandler(EventTimer, value); }
remove { Events.RemoveHandler(EventTimer, value); }
}
#endregion
/**//// <summary>
/// event handler functions
/// </summary>
/// <param name="e"></param>
protected virtual void OnTime(EventArgs e)
{
EventHandler TimerHandler = (EventHandler)Events[EventTimer];
if (TimerHandler != null)
{
TimerHandler(this, e);
}
}
- IPostBackEventHandler -#region - IPostBackEventHandler -
/**//// <summary>
/// 实现IPostBackEventHandler接口
/// </summary>
/// <param name="eventArgs"></param>
void IPostBackEventHandler.RaisePostBackEvent(string eventArgs)
{
OnTime(EventArgs.Empty);
}
#endregion
private string BulidJavascript()
{
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.AppendFormat("setTimeout(/"{0}/",{1})", new object[] { this.Page.GetPostBackEventReference(this), _interval.ToString() });
sb.Append("</script>");
return sb.ToString();
}
- Override -#region - Override -
/**//// <summary>
///
/// </summary>
/// <param name="e"></param>
/// <remarks>
/// 此时调用page函数
/// </remarks>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//获取对客户端脚本函数的引用,调用该函数将使服务器发送回该页。
//该方法还将一个参数传递到在服务器上执行回发处理的服务器控件。
Page.GetPostBackEventReference(this, "");
}
/**//// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <remarks>
/// page已进入绘制状态,此时调用page对象的任何函数都太迟了
/// </remarks>
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
//确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
Page.VerifyRenderingInServerForm(this);
}
if (_enabled)
{
writer.Write(BulidJavascript());
}
}
#endregion
}
}
using System.Web.UI;
using System.Text;
namespace Goodspeed.Web
{
/**//// <summary>
/// Summary description for WebTimer
/// </summary>
public class WebTimer:Control, IPostBackEventHandler
{
- Properties -#region - Properties -
private int _interval = 5000;
public int Interval
{
get { return _interval; }
set { _interval = value; }
}
private bool _enabled;
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
private static readonly object EventTimer = new object();
public bool Visible
{
get { return base.Visible; }
set { base.Visible = value; }
}
#endregion
- Constructor -#region - Constructor -
public WebTimer()
{
//
// TODO: Add constructor logic here
//
}
#endregion
- Event -#region - Event -
public event EventHandler Timer
{
add { Events.AddHandler(EventTimer, value); }
remove { Events.RemoveHandler(EventTimer, value); }
}
#endregion
/**//// <summary>
/// event handler functions
/// </summary>
/// <param name="e"></param>
protected virtual void OnTime(EventArgs e)
{
EventHandler TimerHandler = (EventHandler)Events[EventTimer];
if (TimerHandler != null)
{
TimerHandler(this, e);
}
}
- IPostBackEventHandler -#region - IPostBackEventHandler -
/**//// <summary>
/// 实现IPostBackEventHandler接口
/// </summary>
/// <param name="eventArgs"></param>
void IPostBackEventHandler.RaisePostBackEvent(string eventArgs)
{
OnTime(EventArgs.Empty);
}
#endregion
private string BulidJavascript()
{
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.AppendFormat("setTimeout(/"{0}/",{1})", new object[] { this.Page.GetPostBackEventReference(this), _interval.ToString() });
sb.Append("</script>");
return sb.ToString();
}
- Override -#region - Override -
/**//// <summary>
///
/// </summary>
/// <param name="e"></param>
/// <remarks>
/// 此时调用page函数
/// </remarks>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//获取对客户端脚本函数的引用,调用该函数将使服务器发送回该页。
//该方法还将一个参数传递到在服务器上执行回发处理的服务器控件。
Page.GetPostBackEventReference(this, "");
}
/**//// <summary>
///
/// </summary>
/// <param name="writer"></param>
/// <remarks>
/// page已进入绘制状态,此时调用page对象的任何函数都太迟了
/// </remarks>
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
//确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
Page.VerifyRenderingInServerForm(this);
}
if (_enabled)
{
writer.Write(BulidJavascript());
}
}
#endregion
}
}