Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, March 28, 2012

Ajax UpdatePanel and onload=JavaScript Function

Hi,

I am trying to work through a program but getting really stuck.

I have a asp.net (aspx) page which shows a table of data from a SQL query to the DB. This all works fine. I have a <body onload='resizediv()';> which calls a JavaScript code that resizes the DIV that the table data is displayed in - just so it adds a scrolling area if the table data is >400px deep.

This all works just fine and as it should.

So I thought I would Ajax enable it with a panelupdate control which I wrapped around the Table and DIC that displays the data from the DB. Again this all works as it should - clicking on my "refresh" control button the ajax re-loads the table data without the nasty page flicker.

BUT - my onload='resizediv();' JS function is not called when the updatepanel is refreshed ... it is called the first time the page loads but not when I click on the button control that does the refresh of the panel. Is there anyway to make this JS function fire each time the Ajax panel is updated ?

Apologies if this description is hard to understand, but I am very new to asp.net and Ajax ... :(

Thanks in advance ...

Ro

No ideas on how this could be done ? or am I just looking at this from the wrong angle ?

Thanks

Ro


Instead of using onload=..., try:

<script type="text/javascript">
Sys.Application.add_load(function() { ... });
</script>

If memory serves, the Load event should fire on initial page load as well as any async postbacks.


Steve,

Excellent reply, just tried this and indeed it works with load and partial page loads.

This will help me out no-end ....

Many thanks,

Ro

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 Tabs OnClientActiveTabChanged error

I have 3 tabs .OnClientActiveTabChanged event I am calling a function which is defined in a separate javascript file.My code works when I have the script code in same file as aspx page. But it throws javascript error when i have the code in a seprate javascript file. "Null is null or not an object" var InslastName = $get(''); var InsFirstName = $get('');The InslastName and InsFirstName are always getting as null when using external script file.I tried to use the ids 'txtInslastName' and 'txtInsFirstName' in the function,but getting error.I have many attributes in my aspx page . So i cant pass ClientID as parameter to the function.Is there any way to get resolve this issue?

Hi Sarathmn,

We cannot use Control.ClientID on a separate js file. Your problem is mainly caused by the ClientID issue. When a TextBox inside a TabPanel, its ClientID looks like this TabContainer1_TabPanel2_TextBox1, so you can find out the real ClientID from the generated HTML code and modify your js functions. Here is the another way which I would prefer.

<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" OnClientActiveTabChanged="tabChanged">

function tabChanged(sender, args){
tabIndex = sender.get_activeTabIndex();
switch(tabIndex)
{
case 1: myfunction('"<%=TextBox1.ClientID%>"');break;
case 2: myfunction('"<%=TextBox2.ClientID%>"');break;
break;
}
}

myfunction is a function inside the separate js file.

I hope this help.

Best regards,

Jonathan


Thanks Jonathan for your reply.

I have a doubt.

Can I assume that the client side id of the controls inside a TabPanel have "TabContainer1_TabPanel2_" pre-fixed with the actual id?


Hi Sarathmn,

Currently, yes. But if you put the Tab control inside another contol, it is hard to say.

I hope this help.

Best regards,

Jonathan