PageMethods is undefined error!

Welcome to the club of “wht the fuck is this” group! I believe you are using a ASP.NET master page or a user control ( something other than a normal ASP.NET page). This stuff works just fine on a ASP.NET Page with a ScriptManager but gives itches in other scenarios. Rather than wasting time, Ill show how it works in a Master page.

Here is a Master Page. Nothing but a ScriptManager with EnablePageMethods=”true”.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication2.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>


<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >


</asp:ScriptManager>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

</form>
</body>
</html>


Here is a sample Content Page.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MasterPageFile="~/Site1.Master" Inherits="WebApplication2.Default" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script runat="server">

[System.Web.Services.WebMethod]
// Get session state value.
public static string GetSessionValue(string key)
{
return (string)HttpContext.Current.Session[key];
}

[System.Web.Services.WebMethod]
// Set session state value.
public static string SetSessionValue(string key, string value)
{
HttpContext.Current.Session[key] = value;
return (string)HttpContext.Current.Session[key];
}

</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" >
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Scripts>
<asp:ScriptReference Path="PageMethod.js"/>
</Scripts>
</asp:ScriptManagerProxy>
<input type="Button"
onclick="SetSessionValue('SessionValue', Date())"
value="Write" />
<br />
<br />
<input type="Button"
onclick="GetSessionValue('SessionValue')"
value="Read" />

<span style="background-color:Aqua" id="ResultId"></span>

</asp:content>

And here is the PageMethod.js file, if anyone needs to see whats in it, it has the javascript functions for my button events and other functions for callback, nothing special here!

var displayElement;

// Initializes global variables and session state.
function pageLoad() {
displayElement = $get("ResultId");

PageMethods.SetSessionValue("SessionValue", Date(),
OnSucceeded, OnFailed);
}

// Gets the session state value.
function GetSessionValue(key) {
PageMethods.GetSessionValue(key,
OnSucceeded, OnFailed);
}

//Sets the session state value.
function SetSessionValue(key, value) {
PageMethods.SetSessionValue(key, value,
OnSucceeded, OnFailed);
}

// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName) {
if (methodName == "GetSessionValue") {
displayElement.innerHTML = "Current session state value: " +
result;
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName) {
if (error !== null) {
displayElement.innerHTML = "An error occurred: " +
error.get_message();
}
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


There, thats it. Astalavista!

1 Comment:

Anonymous said...

I'm still hving the 'PageMethods is undefined' error after done everything you done here....

Know how to solve?

Post a Comment