Click here to Skip to main content
Licence CPOL
First Posted 30 Jul 2006
Views 88,011
Bookmarked 45 times

A Stoppable Timer Control for ASP.NET Atlas

By | 30 Jul 2006 | Article
This article presents a timer control that resolves an issue with the Atlas Timer server control. The issue being the Atlas TimerControl cannot be stopped from the server side code.

Introduction

ASP.NET Atlas has a timer server control which periodically postbacks to the server. However, the TimerControl has a bug that prevents it to be stopped from server side code during partial rendering. Time and again, I encounter the question in Atlas forums which ask for a workaround to this bug. This article is based on my post in Atlas forums, and has detailed explanation on how to solve this problem. Hopefully, the issue will be resolved in a future version of Atlas.

Using the Code

Here are the steps you must take to use this control:

  1. Copy the StoppableTimer.cs file to the App_Code directory of your web site.
  2. Modify the web.config file to add the following element:
    <configuration>
       <system.web>
           <pages>
            <tagMapping>
                <add tagType="Microsoft.Web.UI.Controls.TimerControl" 
                     mappedTagType="VRK.Controls.StoppableTimer,App_Code"/>
            </tagMapping>
    
  3. The configuration setting automatically allows your class to be used instead of the Atlas TimerControl without any changes to existing web pages. In a new page, you can continue to use the control using the following syntax:
    <atlas:TimerControl ID="Timer1" 
          runat="server" Interval="500" OnTick="Timer1_Tick" 
          Enabled="false" />
  4. If the bug is fixed in a future CTP, you can just remove the configuration setting, and everything will work as it is.

How it Works

There are two problems with the Atlas server control which causes this bug:

  1. The control does not generate any ID for the timer.

  2. It does not try to stop the timer in response to postbacks or callbacks.

The timer control presented in the article modifies the rendering of the TimerControl as follows:

protected override void RenderScript(ScriptTextWriter writer)
{
  if (!IsPageInPartialRendering)
  {
    writer.WriteStartElement("timer");
    writer.WriteAttributeString("id", this.UniqueID);
    writer.WriteAttributeString("interval", 
           this.Interval.ToString(CultureInfo.InvariantCulture));
    writer.WriteAttributeString("enabled", this.Enabled.ToString().ToLower());
    writer.WriteStartElement("tick");
    writer.WriteStartElement("postBack");
    writer.WriteAttributeString("target", this.UniqueID);
    writer.WriteAttributeString("eventArgument", string.Empty);
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndElement();
  }
}

As you can see, the control renders the XML-Script except for partial rendering. During partial rendering, the timer object on the client web page is not destroyed, so all that needs to be done is to enable or disable the client-side object. This is done in the OnPreRender method:

protected override void OnPreRender(EventArgs e)
{
   if (IsPageInPartialRendering)
   {
     string scriptSnippetFormat = "$object(\"{0}\").set_enabled({1});";
     string scriptSnippet = 
       String.Format(scriptSnippetFormat, 
       this.UniqueID, 
       this.Enabled.ToString().ToLower());

     Page.ClientScript.RegisterStartupScript(
        GetType(),
        this.UniqueID,
        scriptSnippet,
        true);
    }

    base.OnPreRender(e);
}

The only remaining interesting part of the code is obtaining the instance of the script manager on the page and finding whether the ScriptManager is in partial rendering mode:

bool IsPageInPartialRendering
{
   get
   {
      ScriptManager manager = ScriptManager.GetCurrent(this.Page);
      return (manager != null && manager.IsInPartialRenderingMode);
   }
}

Conclusion

This is a quick fix for a bug that exist in Atlas as I know in June CTP. It may be fixed in the coming releases of Atlas. Fortunately, if you use the fix as described, all you will need to do is to change the configuration setting, and you don't need to change any of the source code of any page.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rama Krishna Vavilala

Architect

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNot cross-browser compatible PinmemberXpnctoc5:21 30 Sep '08  
Generaltype or namespace not found Pinmemberblumonde4:56 15 Dec '06  
GeneralRe: type or namespace not found Pinmemberstatic48:57 26 Jan '07  
GeneralRe: type or namespace not found Pinmembertbenami6:45 31 Jul '08  
Generalchanging interval Pinmemberrodrigo.argenta8:06 14 Nov '06  
GeneralIt did not work with me!! PinmemberAL3600B23:53 10 Nov '06  
GeneralGREAT JOB! THANKS A LOT! Pinmemberarinpcssouth7:05 8 Nov '06  
GeneralYou are a genius Pinmemberlevalencia5:46 8 Nov '06  
GeneralThanks! PinmemberArtem L2:38 6 Nov '06  
GeneralOutstanding Pinmemberbeep17:43 29 Oct '06  
QuestionChange interval? PinmemberMDominik135:21 20 Oct '06  
AnswerRe: Change interval? Pinmembersidvivious5:54 25 Oct '06  
GeneralRe: Change interval? PinmemberMDominik1310:34 25 Oct '06  
QuestionRe: Change interval? Pinmemberziwar1:02 30 Oct '06  
QuestionMater page problem Pinmembermanudea7:53 16 Oct '06  
AnswerRe: Mater page problem PinmemberRama Krishna Vavilala8:40 16 Oct '06  
GeneralProblem Pinmembersrinivas7g6:56 9 Oct '06  
GeneralRe: Problem Pinmembermegagreg8:40 11 Oct '06  
GeneralRe: Problem Pinmembersrinivas7g16:00 11 Oct '06  
GeneralSolves a major problem Pinmembermegagreg9:44 6 Oct '06  
GeneralYou got my 5! Pinmemberbreakpoint22:22 20 Sep '06  
GeneralExcellent Pinmembereef23:29 19 Sep '06  
GeneralI can´t make it work Pinmembersoledadm8:02 18 Sep '06  
GeneralRe: I can´t make it work PinmemberRama Krishna Vavilala8:25 18 Sep '06  
GeneralRe: I can´t make it work Pinmembersoledadm8:46 19 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 30 Jul 2006
Article Copyright 2006 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid