Monday, March 26, 2012

PageMethods is undefined

When using the July CTP, I was able to use the PageMethods defined in my web page with no problems.

After loading the beta 1.0 release, I get the error "PageMethods is undefined" when attempting to use WebMethods from my page.

The "Change from the CTP" docs mention that this has been changed to use static page methods and that it's supposed to be easier now. It would be very helpful to see a working sample using PageMethods in the most recent beta release.

Anyone have a working sample?

Ken

hello.

here's a quick test i've written

<%@. Page Language="C#" %>
<%@. Import namespace="LivroAtlas"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
[System.Web.Services.WebMethod]
public static Aluno[] ObtemAlunos()
{
Aluno[] alunos = new Aluno[]{
new Aluno( "João", 15 ),
new Aluno( "Rita", 16 ),
};

return alunos;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="manager" />
<input type="button" value="invocar método da página" onclick="handle()" />
</form>
<script type="text/javascript">
function handle()
{
PageMethods.ObtemAlunos( handleCallback);
}
function handleCallback( res )
{
for( var i = 0; i < res.length; i++ )
{
alert( res[i].Nome + "-" + res[i].Idade );
}
}
</script>
</body>
</html>


Thanks very much, Luis.

I had missed that page methods are now required to be static.

Ken


Could someone please bring back the old PageMethods, when it could access the page control's value, that was great.

hello.

use updatepanels for that and set the childrenastrigger to false so that you don't send back html to the client. there's no need to have pagemethods without them being static.can anyone provide a good scenario where non-static page methods are really the only way to go?


I'm actually using the PageMethod object so that when a user presses an image button, it calls a server side method that pulls values out of session state as well as control values and then makes a call to a back end software that does some processing for our application and returns an object that we then load into a treeview. However, you are not able to reference Session variables from static methods neither are you able to reference controls. This is my example...do you think i'm doing it wrong or do I have a valid scenario?

hello.

how about using an updatepanel or a web method? you do say that you need to refresh the UI...btw, i do think that you can access session state from a static method:

<%@. Page Language="C#" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server">
[System.Web.Services.WebMethod]
public static int O()
{
HttpContext ctx = HttpContext.Current;
ctx.Session["i"] = Convert.ToInt32(ctx.Session["i"]) + 1;
return Convert.ToInt32( ctx.Session["i"] );
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (!this.IsPostBack)
{
HttpContext ctx = HttpContext.Current;
ctx.Session["i"] = 0;
}
}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="manager" />
<div>
<input type="button" runat="server" id="bt" onclick="handleClick()" />
</div>
</form>
<script type="text/javascript">
function handleClick()
{
PageMethods.O(function(res){ alert( res ); });
}

</script>
</body>
</html>

that sample simple returns an int. maybe you can adapt it so that it returns what you need.


This is odd, but I can't seem to get PageMethods to call when the WebMethod is in a codefile. I didn't have a problem with that before. Any thoughts? The exact same code works fine if the WebMethod is part of C# embedded on the page in script tags as Luis has done. I really prefer it the way I had it before, as I prefer using codefiles.

"C#"-->"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> [System.Web.Services.WebMethod] public static string Test() { return "Success"; }</script><html xmlns="http://www.w3.org/1999/xhtml" >"Head1" runat="server"> "form1" runat="server"> "server" ID="manager"> "button"value="invocar método da página" onclick="handle()"> "text/javascript"> function handle() { PageMethods.Test(handleCallback); } function handleCallback( res ) { alert( res ); }

On the other hand, it doesn't work like this:

"C#" AutoEventWireup='false' CodeFile="Default.aspx.cs" Inherits="_Default"-->"-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >"Head1" runat="server"> "form1" runat="server"> "server" ID="manager"> "button"value="invocar método da página" onclick="handle()"> "text/javascript"> function handle() { PageMethods.Test(handleCallback); } function handleCallback( res ) { alert( res ); }using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod]public static string Test() {return"Success"; }} 

There is a bug in the beta build that prevents codebehind page methods from working, just incase requiring static methods weren't already going to make them useless enough..Wink... you have to include them in the aspx file if you want to use them...

See this post for more detailhttp://forums.asp.net/thread/1435443.aspx


hello.

jkolbo is right ony about one thing: there's a bug in the beta build and you can't put them in the code beahind file.

regarding the second part, i will not repeat myself and try to explain (once more) why the old instance page methods sucks...this time, i'll just say why the new static methods are good for. in previous releases, if you didn't want to use page methods due to the performance penalties associated with them, you'd have to use web services. static page methods lets you get the some thing without having to develop webservices


Hi,

Just because someone doesn't want to develop webservices is not a compelling reason to take away the functionalities of the old PageMethods. Performance was still better than a complete postback(which is what most of the internet is using). There should atleast be an alternative. Now we got 2 very similiar methods that do the same thing as oppose to 2 distinct methods that do different things.


Hello.

mnn888:

Performance was still better than a complete postback(which is what most of the internet is using). There should atleast be an alternative. Now we got 2 very similiar methods that do the same thing as oppose to 2 distinct methods that do different things.

this is the kind of thing to which i completely disagree! page methods had to send everything back to the server (ie, all the form's firlds, including hidden ones) and they only returned a value from the server. you don't need a postback to do this! if you need to send some values back to the server why not use javascript + web service method call or static page method call? they're a lot better that instance page methods!

if page methods updated the viwestate+control state, i'd be quite and i would be the 1st to say: bring them back. however, that didn't happened...so i'm not sure on where's the performance gain in using old page methods...can you see it?


One other thing to add. AutoEventWireup for the Page must be set to "true" or else the JS proxy is never generated.


hello.

as you can see by my previous sample, there's no need to have autoeventwireup set to true for gettig the proxy inserted on the client side. why?do?you?say?that?

Luis Abreu:

hello.

how about using an updatepanel or a web method? you do say that you need to refresh the UI...btw, i do think that you can access session state from a static method:

<%@. Page Language="C#" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server">
[System.Web.Services.WebMethod]
public static int O()
{
HttpContext ctx = HttpContext.Current;
ctx.Session["i"] = Convert.ToInt32(ctx.Session["i"]) + 1;
return Convert.ToInt32( ctx.Session["i"] );
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (!this.IsPostBack)
{
HttpContext ctx = HttpContext.Current;
ctx.Session["i"] = 0;
}
}
</script
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="manager" />
<div>
<input type="button" runat="server" id="bt" onclick="handleClick()" />
</div>
</form>
<script type="text/javascript">
function handleClick()
{
PageMethods.O(function(res){ alert( res ); });
}

</script>
</body>
</html>

that sample simple returns an int. maybe you can adapt it so that it returns what you need.

I'm a relative novice to ASP.Net AJAX, so please forgive me if i'm doing something blaringly obviously wrong here, but i copied and pasted the code shown above exactly, and when i press the button on the form, instead of the number "0" being displayed in the alert box, it is in actual fact displaying the entire html source of the page.

Any idea why this is happening?

No comments:

Post a Comment