Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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