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.

No comments:

Post a Comment