Solved

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

Posted on 2008-10-03
1
467 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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