.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET and AJAX: So Freaking Easy (Part 4: Timers)

Ever wondered how to add a timer to your ASP.NET application? Well, yeah, you guessed correctly again: so freaking easy. Two steps to adding a timer:

  1. Put the timer control on your page inside your <contenttemplate> tags.
  2. Set the Interval property of the timer to whatever span of time you want between partial-page post-backs (in milliseconds, ie: 1000 = 1 second)

That was too easy, you say. Okay, you want to do more? How about adding one timer to control multiple UpdatePanels? To do this, you will use a technique from my second post in this series about triggers. First, follow the steps above, except put your timer elsewhere on the page outside of your UpdatePanel. Next:

  1. In the UpdatePanels you want controlled by your timer, add an <asyncpostbacktrigger> to your <triggers> section, and specify the Timer control in the ID attribute (ie: <asyncpostbacktrigger id=”myTimerControl” />)
  2. Next, in that same trigger tag, add the attribute “eventname” and put in “Tick” as the value (ie: <asyncpostbacktrigger id=”myTimerControl” eventname=”Tick” />)
  3. Sit back and watch as all your UpdatePanels automatically update themselves.

If you really want to blow your mind, try adding several timers to your page that refresh at different intervals and linking them up to different UpdatePanels.

Seriously, so freaking easy, Microsoft. What happened to the days of making things challenging? KIDDING! KIDDING! Oh man, I’m sorry guys, I blew it for all of us.

EDIT: Remember, you can alter the behavior of the Timer control programmatically as well. For instance, to turn off the timer, you can create a button or checkbox or something that does: myTimer.Enabled = False. To alter the interval: myTimer.Interval = 60000 (or whatever).


July 23, 2007 - Posted by | AJAX, ASP.NET, Timer, Tips & Tricks

1 Comment »

  1. No matter what I do, my timer triggers the onload of all updatepanels on the page. I have an updatepanel and a timer in it’s template on the masterpage. It’s mode is set to conditional, as are all the update panels on the site.

    I tried using an asynchtrigger set to the id of the timer. I also tried using a OnTick=”FundamentalTick” function to update just that panel. No matter what, all the updatepanel_load events are triggering.

    Comment by Mike | October 27, 2008 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: