To use PageMethods you need to follow these steps:
- You need to use
ScriptManager
and setEnablePageMethods
. (You did). - Create a
static
method in your code behind and use the[WebMethod]
attribute. - Call you method in javascript like you should do in C# but you have more parameter do fill, the
sucess
anderror
callbacks. (You did).
Did you miss any of these steps?
Edit:
Just realized you did this:
function getGiftFileUrl() {
function OnSuccess...
You have yours callbacks inside a function. You need yours callbacks like this:
function OnSuccess(response) {
alert(response);
}
function OnError(error) {
alert(error);
}
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSuccess, OnError);
And you code behind probably will end in something like that:
[WebMethod]
public static string GetGiftFileUrl(string name, int width, int height)
{
//... work
return "the url you expected";
}
Bonus: Since it is a static
method you can’t use this.Session["mySessionKey"]
, but you can do HttpContext.Current.Session["mySessionKey"]
.