Showing posts with label callback. Show all posts
Showing posts with label callback. Show all posts

Wednesday, March 28, 2012

PageMethods CallBack Wont Fire

What conditions will cause the callback function to not fire when using the PageMethods class?

First my javascript:

<scripttype="text/javascript"language="JavaScript">

function FindTheResult()

{

var txt = document.getElementById("ctl00_contentPlaceHolder_txtBib").value;

PageMethods.FindResultMethod(txt, FindResultDone);

window.status = txt;

returnfalse;

}

function FindResultDone(arg)

{

window.alert(arg);

}

</script>

and then my codebehind:

[WebMethod]

public string FindResultMethod(string bib)

{

return "Hello";

}

FindResults() is firing because my status bar gets updated. But the FindResultDone function is not firing.

A client script error in javascript may have happened in this line:

PageMethods.FindResultMethod(txt, FindResultDone);

In this line, FindResultDone() is expecting a parameter but was not supplied


Ok, that makes FindResultDone actually fire. But how do utilize the return string from my WebMethod:public string FindResultMethod(string bib) ?

Leaving out the ( ) and any paramaters usually just passes the return string back to the callback method. See this atlas.asp.nettutorial code. The callback has no parameters, yet the function does.

This all worked flawlessly with the October release.

AlvinVi wrote:

A client script error in javascript may have happened in this line:

PageMethods.FindResultMethod(txt, FindResultDone);

In this line, FindResultDone() is expecting a parameter but was not supplied


Hi,

mxmissile wrote:


PageMethods.FindResultMethod(txt, FindResultDone);

This statement is correct, you don't need to add the () or any parameters because you have to pass a reference to the method (callback) that will be called when the server method returns.

I've tried your code (that seemed correct to me) on a machine and in fact everything went ok: the callback is fired and the string is displayed.

But then I've tried it on another machine and I got the strange behavior that you reported: the callback isn't fired because the asynchronous request returns a response with status code 500 (Internal Server Error). You could try to make the following call:

PageMethods.FindResultMethod(txt, FindResultDone, FindResultTimeout, FindResultError);

and then add these two functions:

function FindResultTimeout() {
alert('timeout');
}
function FindResultError(result, response) {
alert(response.get_statusCode());
}

Try to run the code and see what happens: probably the FindResultError callback will be fired.

Now, I suppose that you have added an <atlas:ScriptManager> tag in your page. Try to add a reference to a web service (for example, create a simple WebService.asmx)

<atlas:ScriptManager id="scriptManager" runat="server">
<Services>
<atlas:ServiceReference Path="WebService.asmx" />
</Services>
</atlas:ScriptManager>

and the run the code: which callback is fired now? In my case, the FindResultDone callback is fired and the response error disappeared...

I think it's a strange behavior.. I'll try to understand what is happening, but has anyone got ideas on what is going on?

Ok, ill try adding the webmethod to a seperate asmx file to see what happens.

I also have some other data:

function FindResultError(result, response) {
alert(response.get_statusCode());
}

When I do this, the alert window just says "null", so the error function is being called at least I think.

My page is using a Master Page, when I move the code to a "un-master page"ed" aspx file it works great.

I started a new test atlas project and everything is working great, until I start using Master Pages. It fails if I put the <atlas:ScriptManager tag is in the <head> of the Master Page or in the content section.

Anyway thanx for the input, still hacking at it...


Hi,

mxmissile wrote:


My page is using a Master Page, when I move the code to a "un-master page"ed" aspx file it works great.


You mean the first code you posted? I've tried it without a master page and it's working on a machine, but failing on another.

mxmissile wrote:


Ok, ill try adding the webmethod to a seperate asmx file to see what happens.


Just create a new web service (WebService.asmx), reference it in the<atlas:ScriptManager> tag but continue to call the method definedin your page (PageMethods.FindResultMethod). In my case this solved theproblem but it's a strange behavior.

Monday, March 26, 2012

PageRequestManager only processes one asynch callback at a time?

I was under the impression that the asp.net ajax framework was capable of handling multiple asynch callbacks simultaneously and processing the results of each in the order that they come back. However, I accidentally discovered something interesting: it seems that if you begin an asynch callback from the browser while a previous asynch callback is still in process, the first one gets cancelled by the client-side PageRequestManager instance before it begins the second one.

To see what I'm talking about, you can go to this official MS ajax example:http://ajax.asp.net/docs/Samples/System.Web.UI.UpdateProgress1/cs/Default.aspx

Try clicking the first button, then quickly clicking the second button before the first date gets updated on the page. Notice that after a few seconds, the second date changes as a result of the second asynch callback, but the first date never gets updated. This is because the asp.net ajax framework is prematurely ending the first request as soon as you initiate the second request by clicking the second button.

I'm working on a project with multiple UpdatePanels on a page that do completely separate things in their server-side code, and ideally we would like the user to be able to initiate a callback in one panel and then immediately initiate a callback in the second panel, and have both of the asynchronous callbacks processing at the same time. At the moment, it doesn't seem like this is a possibility in the asp.net ajax framework.

If anybody can tell me how I might be able to get around this issue I would be very appreciative.

hello.

well, what you ask isn't doable with the current code of the platform. you can only have more than one remote call if you're using web services (and even in these cases you might face some problems which have been described here: http://www.codeproject.com/Ajax/aspnetajaxtips.asp)

Saturday, March 24, 2012

PageRequestManagerServerException

I have a callback funtion but I get the following error when I use this when the login credential has expired:

Sys.WebForms.PageRequestManagerServerException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

I.e It tries to get information from a function that is not reachable as the user has been loged out.

If the page is reloaded i will get rerouted to the login page but as i don't reload the page and only using a callback i don't get rerouted - i only get the above error message.

Any Ideas?

You can capture the error on the client side and redirect the user there using the Javascript window.location

Seehttp://www.asp.net/AJAX/Documentation/Live/tutorials/CustomizingErrorHandlingforUpdatePanel.aspx for handling errrors from the UpdatePanel; if you are using PageMethods seehttp://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx for capturing errors.

-Damien


Try to capture asynchronous post back error as the follow.


ScriptManager.AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(ScriptManager_AsyncPostBackError);


Determine if it is asynchronous post back : ScriptManager.IsInAsyncPostBack;

Get synchronous post back error message: ScriptManager.AsyncPostBackErrorMessage;


Get asynchronous post back source: ScriptManager.AsyncPostBackSourceElementID;

Let me know if this helpful to you