Solved

AJAX .NET -- Timer control -- How to call a Javascript function?

Posted on 2008-10-03
1
444 Views
Last Modified: 2013-11-08
Hello,

I have a AJAX.NET Timer control running in a page and it polls the server every 5 seconds.  I need it to call a Javascript function.  Is this even possible?  Any ideas?

In the included code snippet, you'll see that I'm trying to force a write of "alert('hi!');"  That doesn't work, but I need to do something like that.

Thanks
protected void timerPoll_Tick(object sender, EventArgs e)

    {

        Label1.Text = "The time is: " + DateTime.Now.ToShortTimeString();
 

        Literal1.Text = "alert('hi!');";

    }

Open in new window

0
Comment
Question by:sushik15
1 Comment
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 22643656
No, that is not possible. You can create javascript from the server side with something like this:

        Page.ClientScript.RegisterStartupScript(typeof(Page), "script1", "alert('Hi');", true);

if you do it while the page is still processing on the server, for instance in the Page_Load handler. The server has to render the script to the browser in the response, just like the other controls. During an async postback, it can't render script to the page.

You could however write a javascript function to do something every five seconds (not using a timer control). its clock would be more or less synchronized with the async timer control, but they would work independently of each other. It wouldn't necessarily occur write after you poll the server. If you want that to happen you would have to write it all in javascript. If you are interested in that you could start by looking here at the timer information:
http://grizzlyweb.com/webmaster/javascripts/refresh.asp

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now