Controlling the ASP.NET Timer Control with JavaScript
Have you ever wanted to control your <asp:Timer> control from client-side code?
Let’s say you’ve named your timer ‘Timer1’. The first step is to create a reference to this component:
var timer = Sys.Application.findComponent(‘<%= Timer1.ClientID %>’);
Or, better yet, use the $find() shortcut:
var timer = $find(‘<%= Timer1.ClientID %>’);
You can then easily access the timer’s interval and enabled properties, as well as start and stop the timer.
//returns the timer’s interval in milliseconds:
var waitTime = timer.get_interval;
//sets the timer’s interval to 5000 milliseconds (or 5 seconds):
timer.set_interval(5000);
//returns whether or not the timer is enabled:
var isTimerEnabled = timer.get_enabled();
//disables the timer:
timer.set_enabled(false);
//starts the timer:
timer._startTimer();
//stops the timer:
timer._stopTimer();
For the more adventurous of you who would like to look at the client-side behavior code for the Timer control and who elected to download the source code for the AJAX Control Toolkit, you can probably find the .js file at:
Drive:\Program Files\Microsoft ASP.NET\AJAX Control Toolkit\AJAXControlToolkit\Compat\Timer\Timer.js
(If you don’t know what you are doing, do not make any changes to this file!)
3 Comments »
Leave a Reply
-
Recent
- Highlighting a GridView Row When Clicking a CheckBox
- Determining a Browser’s Dimensions with Javascript
- Executing Server-side Code from JavaScript
- Wrapping ASP.NET AJAX TabContainer Tabs
- Handling Multiple Asynchronous Postbacks
- Disabling a Trigger Control During Asynchronous PostBack
- Maintain Scroll Position after Asynchronous Postback
- Selecting an AJAX AccordionPane by ID
- Easy SQL “If Record Exists, Update It. If Not, Insert It.”
- View Source Trick for Pages with Partial Rendering
- Controlling the ASP.NET Timer Control with JavaScript
- A Client-side Ajax Login for ASP.NET
-
Links
-
Archives
- January 2008 (3)
- December 2007 (4)
- November 2007 (6)
-
Categories
-
RSS
Entries RSS
Comments RSS
It would be more helpful if you provided info about how to add a timer control at the client side thorugh java script. But this is a great effort as well. Thanks
Nachiket:
Thanks for the feedback. The ASP.NET AJAX Timer control is a server control which cannot be added via Javascript. However, it’s behaviors can be controlled via client-side script, which is what the article addressed.
If you want a purely client-side timer solution, look at the Javascript window.setInterval() method.
All is accurate and on business. It is well written, I thank.