The TimerControl included in Atlas March CTP has a problem. It cannot be stopped ones started. I guess that this is intentional (that’s what the documentation says) but the fact remains. You often want to be able to update a page, only while some task is run on the server.
A user on the Atlas forum named Rama Krishna (that’s his username) came up with a clean and great solution to create a stopable timer control (see the post here). However, the posted control only worked on the January CTP release.
I took his control and modified and updated it for the March CTP release (All credit to Rama).
The control emits a client-side timer control (xml) and assigns an id to it. (The base TimerControl does not). Then a small piece of java script is registered to handle enabling and disabling the timer at startup.
First put the following in the App_Code folder:
using System;
using Microsoft.Web.UI.Controls;
namespace CustomControls {
public class StopableTimer : TimerControl {
public StopableTimer() {
}
protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (Page.IsPostBack) {
Page.ClientScript.RegisterStartupScript(
Page.GetType(), "TimerStop",
"Sys.Application.findObject('" +
UniqueID +
"').set_enabled(" +
(Enabled ? "true" : "false") +
");"
, true);
}
}
protected override void RenderScript(Microsoft.Web.Script.ScriptTextWriter writer) {
writer.WriteStartElement("timer");
writer.WriteAttributeString("id", UniqueID);
writer.WriteAttributeString("interval",
Interval.ToString(System.Globalization.CultureInfo.InvariantCulture));
writer.WriteAttributeString("enabled", Enabled.ToString());
writer.WriteStartElement("tick");
writer.WriteStartElement("postBack");
writer.WriteAttributeString("target", UniqueID);
writer.WriteAttributeString("eventArgument", string.Empty);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}
When you have created the control, you only need to register it on you aspx page - and use it as you would use a TimerControl
<%@ Register Assembly="App_Code" Namespace="CustomControls" TagPrefix="AppCode" %>