Solved

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

Posted on 2008-10-03
1
477 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
[X]
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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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