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" "">

<html xmlns="" >
<head runat="server">

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

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

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



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">

// Get session state value.
public static string GetSessionValue(string key)
return (string)HttpContext.Current.Session[key];

// Set session state value.
public static string SetSessionValue(string key, string value)
HttpContext.Current.Session[key] = value;
return (string)HttpContext.Current.Session[key];


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

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


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) {
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: " +

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

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