Showing posts with label client. Show all posts
Showing posts with label client. Show all posts

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..

Ajax time zone bug

there appears to be a bug in the ajax client code. First, try this in javascript:

var testDate = new Date();

var testDateString = testDate.localeFormat("yyyy-MM-ddTHH:mm:ss.fffffffzzz");

I'm in New York, so the value of testDateString = "2007-10-12T12:10:56.34534+4:00" or something like that.


now try doing this in c#

DateTime testDate = DateTime.Now;

testDateString = testDate.toString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");

in this case, the value of testDateString is equal to something like: "2007-10-12T12:10:56.34534-4:00"

now to see some real fun, add this to the above Javascript:

var messedUpDate = Date.parseLocale(testDateString, "yyyy-MM-ddTHH:mm:ss.fffffffzzz");

you'll find that the messed up date is not equal to the original test date. however in c#

DateTimemessedUpDate = Date.ParseExact(testDateString,"yyyy-MM-ddTHH:mm:ss.fffffffzzz",CultureInfo.CurrentCulture.DateTimeFormat);

will yield a date equivalent to testDate.

Is this a bug? or am I misusing/misunderstanding the localeFormat method?

Hi,

I think parseLocal create a date from a localized date string. I think it works only when you do the followings (it is from the documentation):

Remarks

Use the pareseLocale function to create anobject of type Date from a string. If you provide no custom formats,this function uses theSys.CultureInfo.CurrentCulture property to determine the culture value.

To create a date from a locale-specific string that uses the current culture, set theEnableScriptGlobalization property of theScriptManager control totrue. You must also modify settings in the Web.config file so that theculture attribute of the<globalization> section is set toauto.


I don't think this is a localization problem. As you can see by the results of running my code, all the strings do parse correctly as dates, the problem is that the resulting dates are incorrect.


Ok, I see.

First test:

var testDate = new Date();
var testDateString = testDate.localeFormat("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
window.alert( testDateString ); //2007-10-17T10:11:16.0310310-02:00 <-- good
var messedUpDate = Date.parseLocale(testDateString, "yyyy-MM-ddTHH:mm:ss.fffffffzzz");
window.alert( messedUpDate ); //Wed Oct 17 200714:11:16 GMT+0200 (Central Europe Daylight Time) <-- total wrong!!!

Second test:

var testDate = new Date();
var testDateString = testDate.localeFormat("yyyy-MM-ddTHH:mm:ss.fffffff");
window.alert( testDateString ); //2007-10-17T10:13:32.5155155 <-- good
var messedUpDate = Date.parseLocale(testDateString, "yyyy-MM-ddTHH:mm:ss.fffffff");
window.alert( messedUpDate ); //Wed Oct 17 2007 10:13:32 GMT+0200 (Central Europe Daylight Time) <-- also good

Maybe localFormat and parseLocal don't like time zone formatting?!


The issue on the timezone offset was a bug in the javascript date formatting, and will be fixed in the next release. The reason for the discrepancy between parse and format was that parsing didn't have the same bug.