Monday, March 26, 2012

Execute JS Code onload and on partial postback

Hi All,

I have a ScriptManager tag that contains a script reference to a JS file; "test.js." Test.js contains a function that needs to be executed on the application's loadand on a partial postback.

The reason that it needs to be executed on the application's load event is because if I execute it just using RegisterStartupScripts the script isn't present on the page yet because it is being controlled by Atlas.

So, I create a wrapper function for statement on the fly and I register it to execute on the application's load event. I.e. "Sys.Application.load.add(wrapperFunction);"

That works great the first time through when the page is loaded because the application's load event fires. However, how do I get the same execute statement to fire on a partial postback? The only workaround I have right now is to get a handle to the current ScriptManager and test to see if it is InPartialRenderingMode. If so, I don't do the add to load code, but I just register a startup script instead.

Is there a nicer, easier, cleaner way of doing this.

Thx!!Hi,

I think I have a decent workaround for the moment. My example I was providing was a bit simplistic in comparison with what I actually need to do. I actually needed to completely change what JavaScript commands were executed and their parameters for each time a server side control was loaded. Having said that, the best way I've come up with so far is test on the backend if my ScriptManager is in PartialRenderingMode and handle it accordingly. If it isn't, I use the load event and attach a delegate to execute. If it is, I just execute a the function that was to be the delegate. It's not the prettiest code, but it works reliably and can be repeated for other controls.

Thanks for your help on this.

I need to do the same thing. How did you get it done?

I have a myfuncs.js in the script manager.

I want to call a function in the js file after a updatepanel psotback.

I can not find a good way to fire it. I thought Application.Load would do it but the page only gets loaded once.

and ideas?


I kind of do what I described a few posts back.

I test to see if the control's ScriptManager is in PartialRenderingMode and if so, I just emit the JavaScript straight to the client using the ClientScriptManager.RegisterStartupScript method. If it is not in PartialRenderingMode, I wrap my JavaScript in a function and add it to the Sys.Application.load event and then emit the whole code block to the client again using the RegsiterStartupScript method.

Ex of what it would look like on the client.

function WrappedFunction() {
var x = new Array();
x.push(1);
}

Sys.Application.load.add(WrappedFunction);

OR

var x = new Array();
x.push(1);

HTH

hello.

since the file is already loaded, how about adding an event handler to the propertychanged event and check? there you could check the property that's changedand if it's the inPostBack property and its value is false, you know that you've just finished loading the page. btw, why can't you use the registerstartupscript method? i don't understand what you mean when you say that the script is being controlled by atlas...


Hi.

By "script is being controlled by atlas" I mean that the script file is registered with the ScriptManager.

I.e.
<atlas:ScriptManager ...>
<scripts>
<atlas:ScriptReference path="test.js" ...>
</ ..>
</..
The reason that the script needs to be registered with Atlas is that the function I need to execute on startup and postback has code within it that relies upon the Atlas Sys namespace and therefore must be loaded after the Atlas scripts have been loaded otherwise it'll throw an error complaining it doesn't know what Sys is. (The statement also relies on user input which is the reason it needs to get reexecuted on postback.)

It is my understanding that the page processes any <script ...> tags including any startup scripts first; then loads the Atlas scripts; then loads the scripts that are defined within the ScriptManager Scripts tag.

So, if I just use the RegisterStartupScript method and my scripts are registered with the ScriptManager, my startup statement will execute before the Atlas files (and my custom js file) have been loaded and throw an exception. My code needs to wait for the Atlas files and my custom js file to load before executing so instead, I add the startup statement to a function and add it to the Sys.Application.load event delegate as an event handler. This way it'll wait until Atlas has loaded before executing.

This works fine for the load, but one of the function's parameters is based upon user input and the function needs to be reexecuted whenever the page is postbacked.

The first time through the code looks like

function wrapperFunction () { testFunction (''); }
Sys.Application.load.add (wrapperFunction);

But, on postback the same part of the code would look like

function wrapperFunction () { testFunction('30111'); }
Sys.Application.load.add (wrapperFunction);

Of course, the second code block doesn't do anything on postback because the load event has already fired. To accomplish the same thing on postback the code would just look like:

testFunction ('30111');

as the test.js file where testFunction lives has already been registered and now can be accessed through startup scripts.

(All code blocks are registered to execute on startup.)

I'm not really seeing how I can do what you suggested and add an event to the PageRequestManager as my function call needs to change based upon user input.

So I can test for InPartialRenderingMode in code behind and modify the startup JavaScript accordingly (which is what I'm currently doing), but it seems that there needs to be an event that covers both loading initially and loading after a postback. Emitting a pure JavaScript function call works for postbacks and attaching to the load event of the Application works for loading initially, but I'd like to be able to write one piece of code that works for both cases.

Phew ... I hope that's at least partially clear.

Thx for any and all help.

hello.

yes, now i understand.

handling the propertychanged of the pagerequestmanager is the only way i know to handle the end of partial postback.

what you could do is inject a global variable with the value that you need to pass to your jscript function; since the registerXXX inserted method will be called before the event being fired, you can be sure that the global variable will allways be correctly set up.


To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal
To call specific javascript function or register a new javascript after the update panel partial postback

you can use my custom made control

to download it and to know how to use it

visit my blog.

http://go2amitech.blogspot.com/2010/08/running-specific-javascript-after.html[^]



Amit Panchal

No comments:

Post a Comment