Saturday, March 24, 2012

Call a WebMethod from javascript (I dont want to use any server side buttons only javascri

I think my problem is slightly different.

I have defined a region of an image using jquery (javascript) I check the click event, it something like onclick for button, so as you can see its all javascript nothing server side button, etc

I want to load a .asmx dynamically in an update panel. I know how to do this using a link button. Now what I want to do is to call a method that load this file in my server code behind.

This is what I tried to do so far. I simplified the problem.

' <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
<Scripts>
<asp:ScriptReference Path="JavaScript/loadmypages.js"/>
</Scripts>
</asp:ScriptManager>

'

loadmypages.js is a simple javascript that call my function (i am hoping that it will) through PageMethods. this is a simple style of my javascript.

'

// JScript File

var Load_content2 = function()
{
PageMethods.myfunction_on_code_behind(OnComplete1, OnTimeOut1, OnError1);
return false;
}

function OnComplete1(result)
{
alert(result);
}

function OnTimeOut1(result)
{
document.getElementById('lblMsg').innerHTML ="Time out";
}

function OnError1(result)
{
document.getElementById('lblMsg').innerHTML ="There is an error!";
}

'

I have tried to make my method accesable for javascript like this:

'

using System.Web.Services;

[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public string myfunction_on_code_behind()
{
return "Its good if it works";
}

'

but every time i click on that region the script debugger say PageMethods is not defined.

Could someone help me to solve this problem ? am I missing something here ? as you can see I have even enabled page method: EnablePageMethods="true"

Thank you

Hi,

Please note that the method should be a static method.
Hope this helps.
Can you post your whole webservice file here? I think you are missing something.

Has there been any updates to MS Ajax that don't require PageMethods to use static methods yet?


No, nor would I expect one in the future.

No comments:

Post a Comment