Asp.net Scriptmanager Pagemethods Is Undefined
Solution 1:
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:
functiongetGiftFileUrl() {
functionOnSuccess...
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]
publicstaticstringGetGiftFileUrl(string name, int width, int height)
{
//... workreturn"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"]
.
Solution 2:
In your codebehind create this method:
[WebMethod]
publicstaticvoidGetGiftFileUrl(string value1, int value2, int value3)
{
// Do Stuff
}
your js script should resemble this too:
<scripttype="text/javascript">functiongetGiftFileUrl() {
PageMethods.GetGiftFileUrl("hero", 1024, 768, OnSucceeded, OnFailed);
}
functionOnSucceeded(response) {
alert(response);
}
functionOnFailed(error) {
alert(error);
}
getGiftFileUrl();
</script>
Solution 3:
I've realize why the PageMethod object was undefinded, because ScriptManager component placed next from the script that uses PageMethod, so when page is rendered and script executed, there is no PageMethod at this moment. So i need to call getGiftFileUrl() on button click or on window load event, when all scripts on page are ready to use.
Solution 4:
<scripttype="text/javascript">functionGenerate()
{
var result = PageMethods.GenerateOTP(your parameter, function (response)
{
alert(response);
});
}
</script>
Will 100% work.
Post a Comment for "Asp.net Scriptmanager Pagemethods Is Undefined"