Saturday, March 24, 2012

Ajax Timer - call client JavaScript

Hi,

how can I call a client JavaScript function on ASP.NET AJAX timer tick?

Also, how can I trigger a trigger in an UpdatePanel within a JavaScript function?

Thanks,

Daikoku

Hello,

Find out the event ASP.NET AJAX timer tick ,& the way by which u can subscribe this javascript function to this event

Swati


Sorry, but I'm not following. Can you give a quick example of what you mean?

Thanks!


Hi,

following article will help you

http://www.developerfusion.co.uk/show/5340/,

otherwise post your code , we will find out the other way

Swati


Hi,

It seems that Timer doesn't provide tick event on client. But according to client life cycle, you can use Sys.WebForms.PageRequestManager class' s endRequest event to simulate it.

Please try this:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> </script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <script type="text/javascript"> function endRequestHandler(sender, args) { yourFunction(); } function yourFunction() { alert("aaa"); } function pageLoad() { if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); } </script> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <asp:Timer ID="Timer1" runat="server" Interval="6000"> </asp:Timer> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
Hope this helps.

Hi Raymond,

Is there a way to stop / start the timer from the client side script ? also it seems, i cannot abort the timer post back ?

Thx,

Gopi


It's MUCH easier to just use the Sys.Preview.UI.Timer object.

var t = new Sys.Preview.UI.Timer();
t.add_tick(myFunction);

you can use t.stop() and t.play() to control whether the timer fires or not.


Thanks Paul,

I cannot find any documentation for Sys.Preview.UI.Timer() [in ajax.asp.net] ? I want to know how set the timer interval , plus how to dispose the object etc..

Thanks,

Gopi


The second question : How to trigger a trigger from javascript:

I have a button inside the UpdatePanel. The button has UseSubmitBehavior=False (because with True, it does not catch its event handler, when running in FireFox). I have following JS function to trigger the button :

function myTrigger(myId) {
var hidRefresh = document.getElementById("<%=hidRefreshIMG.ClientID%>");
var hidField = document.getElementById("<%=Hidden1.ClientID%>");
hidField.value=myId;
hidRefresh.click(); }

The hidField-thing is to pass an argument back to the server.

The above may not be very beautiful but it works - I found the solution in this posting:

http://forums.asp.net/t/1114574.aspx


It's in the ASP.Net Futures release. iirc changing the timer interval is as simple as t.set_interval(number) where t is your timer instance. You can also just use window.setTimeout() if you're just tryign to call another function and you're not too worried about scoping issues.


oh! ok, future (May 2007) release has some framework upgrade plus other component install, so we cannot consider this solution at this point. Anyhow, thanks for your help.

If anyone know how to start /stop the current version [ajax ver 1.0] of Timer from the client side, that would be great !

Thx,

Gopi


Have you tried calling _startTimer() method? Though it's considered to be private according to the naming convention.

Like this:

var timer = $find("Timer1");

_startTimer();


u mean timer._startTimer() right ? yes, tried that, $find('Timer') returns an object but timer._startTimer exception out, saying no method or property !


<%@. Page Language="C#" AutoEventWireup="true" CodeFile="SetTimerInterval.aspx.cs" Inherits="SetTimerInterval" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"><asp:scriptmanager runat="server"></asp:scriptmanager> <asp:Timer ID="Timer1" runat="server" Interval="3000"> </asp:Timer> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> </asp:UpdatePanel>   <input id="Button1" type="button" value="ChangeInterval" onclick="setTimer();"/> <input id="Button2" type="button" value="Start" onclick="startTimer();"/> <input id="Button3" type="button" value="Stop" onclick="stopTimer();"/> <textarea id="TraceConsole"></textarea> <script type="text/javascript"> function setTimer() { var timer = $find("Timer1"); Sys.Debug.trace(timer.get_interval()); timer.set_interval(100); Sys.Debug.trace(timer.get_interval()); } function startTimer() { var timer = $find("Timer1"); timer._startTimer(); } function stopTimer() { var timer = $find("Timer1"); timer._stopTimer(); } </script> </form></body></html>

Thanks Raymond...

But sometimes I have also faced the problem like

Sys.WebForms.PageRequestMangerServerErrorException: An unkown error occured while processing the request on the server. The status code returned from the server was:500

Any solution for this..

No comments:

Post a Comment