Showing posts with label async. Show all posts
Showing posts with label async. Show all posts

Monday, March 26, 2012

Executing Client Side JS after an async postback event returns

My issue has to do with firing a purely client side javascript event after the async postback returns from the server. I have scoured the documentation and these forums and not found, what I am sure to be a very simple thing to do. If anyone can point me in the right direction that would be greatly appreciated.

TIA

more information would be helpful.

Are you using JavaScript for the postback or an Ajax control, or what?


I'm using the AJAX ASP.NET controls. I have a single update panel that when it returns from processing an async event, I want it to automatically execute a JavaScript method that performs some UI effects.


This is exactly the same problem that we are experiencing. We are having a terrible time accomplishing this task and we need help as well.

If we register a "start-up" JavaScript block, it runs only once when the page first loads. It does not run again, even if we execute the same code.

Please help!


I think I found a solution:

http://ajax.asp.net/docs/overview/AJAXClientEvents.aspx

This describes how you can run client-side events after every successful AJAX UpdatePanel refresh. I used "PageLoading" and "PageLoaded" methods to accomplish what I need.


I have now found how to get back to the client-side JS by implementing this :

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);function EndRequestHandler(sender, args) { //do stuff here }

However, what I can't figure out now is what information is available to be able to figure out, for example, what event triggered the postback event, or what methods were invoked on the server or client side so that I can call the function that i want to process.



check out:

http://www.codeguru.com/csharp/.net/net_asp/scripting/article.php/c5337/


The final solution I came up with did indeed involve the aforementioned EndRequest but also capturing data from the BeingRequest. Not sure if there is a more elegant solution, but here's my JS.

1//This is a global JS variable that will be passed2var _PostBackElement;34//this sets the beginrequest and endrequest handlers5window.onload = function()6{7 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);8 Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);9}101112function BeginRequestHandler(sender, args)13 {14//This captures the element that Creates the15 //async postback into the global variable16 _PostBackElement = args.get_postBackElement();17 }1819function EndRequestHandler(sender, args)20 {21//check for the existence of the ID of the element that22 //caused the postback23if (_PostBackElement.id.indexOf("mSaveSearchButton") > -1)24 {25//do stuff here26 }27else if (_PostBackElement.id.indexOf("mSavedSearchLinkButton") > -1)28 {29//do other stuff30 }31 }

Basically, this captures the ID of the requesting element and then sets it in a global variable, then you use that to determine what action was being taken and on the EndRequest do whatever subsequent UI stuff you want.

That's it!


Try ScriptManager Register Startup Script instead of Page ClientScript Manager.

Execute client script in a async postback

Hi,

I´ve just replaced the july atlas ctp with ajax 1.0. I have a button as part of a UpdatePanel. In the serverside clickevent of the butten I want to call a client script but it doesn´t work. Even a simple alert('test') doesn´t work. I´ve used Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "test", "alert('test');", true); as well as the similar command ScriptManager.RegisterClientScriptBlock. Can anybody help me?


Thx

Hi,

You need to call the new static registration method on ScriptManager to register the script. For the first parameter pass in the button that's inside the UpdatePanel to get its script to be sent down along with the UpdatePanel's new contents.

Thanks,

Eilon


hello.

btw, i have 2 questions about the new methods:

1. what happens if you're aa component developer? i mean, say that you need to support full and partial postbacks so that you allways sent something back to the client side. if you use the the new registerXXX from scriptmanager, you're creating a dependency between your control and ajax extensions. what happens if you don't wont ajax extensions on your app?

2. even though i see the possibilities in associating a script with a control placed inside an updaptepanel (while performing the script registration), what happens when you just need to insert a script back on the page during a partial postback? an example; you have a simple page with an updatepanel and you set the childrenastrigger to false. the objective of the panel is to let you get info from the controls, do some server side processing and then return a script to client (yes, a little off, but it's something that might be used by the old instance page methods fans since there's no instance page methods in the current release). since the chidrenastriggers is set to false, you can't send back a script from the server side (even though you assaociate it with a control inside the updatepanel) because the panel isn't being refreshed and due to that, it won't be included in the page.


More info about script registration in my recent post:http://forums.asp.net/thread/1440058.aspx

Thanks,

Eilon


Finally it works, thanks a lot!

Eilon:

More info about script registration in my recent post:http://forums.asp.net/thread/1440058.aspx

Thanks,

Eilon


hello.


still some questions remain:

1. my controls must always link to the ajax extensions dll even if the pages that use them aren't using ajax

2. i've presented a scenario where i just use an updatepanel to get all the controls on the server side and i need to send back a javascript message to the client. how can i accomplish this (i've set the childrenastrigger to false since i don't want to refresh the panel). if there was an overload for inserting javascript on the page without associating the instruction to a control, it would solve my problem (note that in those cases, i know that there's no chance of cleaning the script but i'd prefer to have that option since most of the time i'll be sending method calls from the server side).


Hi Luis,

1. See another of my recent posts:http://forums.asp.net/thread/1445844.aspx

2. The way I would like to see this implemented is that instead of the page registering the script, the page should add a control to the UpdatePanel and that control should register its own script. Nevertheless, it's a suggestion I've been considering myself. For example maybe if you pass in the Page or something we'll always include the script. I'm just afraid that it's a feature that people will abuse to no end and perhaps cause even more problems than it solves. Definitely under consideration, though.

Thanks,

Eilon


you may also look here:

http://forums.asp.net/thread/1445682.aspx