在Asp.net中原来WinForm中的Time控件就不能用了, 所以要实现在Asp.net中定时执行的业务,需要重写Asp.net下面的Time控件,如下是Delphi和.NET高手黄忠诚所写的WebTimer控件,使用时候,需要设置interval = true,enabled = true,具体怎么用自己去试吧,祝好运!
//****************************************************************************
/////////////////////////////////////////////////////////////////////////////
/// Power ASP.NET Controls
///
/// write by Jeffray Hwang in Taiwan Taipen.
/// this program is for my book: <in-Depth ASP.NET Server Controls/Components>
////////////////////////////////////////////////////////////////////////////
using System;
using System.Web.UI;
using System.ComponentModel;
using System.Collections;
using System.Text;
namespace PowerAsp.NET.Controls
{
/// <summary>
/// Summary description for WebCustomControl1.
/// </summary>
[
Designer(typeof(PowerAsp.NET.Designer.WebTimerDesigner))
]
public class WebTimer : System.Web.UI.Control,System.Web.UI.IPostBackEventHandler
{
#region private member
private int _interval=5000;
private bool _enabled=false;
private static readonly object EventTimer=new object();
#endregion
#region property
[
Browsable(false)
]
public bool Visible
{
get
{
return base.Visible;
}
set
{
base.Visible=value;
}
}
[
DefaultValue(5000),
Bindable(false),
Description("duration millisecond")
]
public int Interval {
get
{
return _interval;
}
set
{
_interval=value;
}
}
[
DefaultValue(false),
Bindable(false),
Description("Enable Timer")
]
public bool Enabled{
get
{
return _enabled;
}
set
{
_enabled=value;
}
}
#endregion
#region events
public event EventHandler Timer
{
add
{
Events.AddHandler(EventTimer,value);
}
remove
{
Events.RemoveHandler(EventTimer,value);
}
}
#endregion
#region event handler functions
protected virtual void OnTimer(EventArgs e)
{
EventHandler timerHandler=(EventHandler)Events[EventTimer];
if(timerHandler !=null)
{
timerHandler(this,e);
}
}
#endregion
#region IPostBackEventHandler implements
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnTimer(EventArgs.Empty);
}
#endregion
#region utility functions
private string buildJavaScript()
{
StringBuilder sb=new StringBuilder();
sb.Append("/n<script language=/"javascript/"> /n <!-- ");
sb.AppendFormat("/n setTimeout( /"{0}/",{1});",
new object[] {
this.Page.GetPostBackEventReference(this),
_interval.ToString()
});
sb.Append("/n//-->/n</script>");
return sb.ToString();
}
#endregion
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
Page.GetPostBackEventReference(this,"");
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter writer)
{
if(Page != null)
Page.VerifyRenderingInServerForm(this);
if(_enabled)
writer.Write(buildJavaScript());
}
}
}
//**************************************************************************