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.
No comments:
Post a Comment