Trying to use PageMethods with a Master Page and Content Page

Last post 05-11-2008 12:49 AM by rmdw. 10 replies.

Sort Posts:

  • Trying to use PageMethods with a Master Page and Content Page

    05-08-2008, 2:14 PM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    I'm most curious to find out if PageMethods works if there's a Master Page involved.  I tried implementing the sample code from this page as follows:

    <%@ Page Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
    <%@ Import Namespace="System.Web.Services" %>
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" EnableViewState="true">
        <Scripts>
          <asp:ScriptReference Path="PageMethod.js"/>
        </Scripts>
      </asp:ScriptManagerProxy>
    
      <script runat="server">
    
        [WebMethod]
        // Get session state value.
        public static string GetSessionValue(string key)
        {
            return (string)HttpContext.Current.Session[key];
        }
    
        [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>
    
      <h2>Using Page Methods with Session State</h2>
      
      <center>
        <table>
          <tr align="left">
            <td>Write current date and time in session state:</td>
            <td>
              <input type="Button" 
                  onclick="SetSessionValue('SessionValue', Date())" 
                  value="Write" />
            </td>
          </tr>
          <tr align="left">
            <td>Read current date and time from session state:</td>
            <td>         
              <input type="Button" 
                  onclick="GetSessionValue('SessionValue')" 
                  value="Read" />
            </td>
          </tr>
        </table>           
      </center>
       
      <hr/>
    
      <span style="background-color:Aqua" id="ResultId"></span>
    
    </asp:Content>

     


    But no matter what I do, I keep on getting some variation of a Javscript "object missing" error.  Might anybody know why?

    Robert W. 

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-09-2008, 11:36 AM
    • Loading...
    • gt1329a
    • Joined on 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 1,520

     Did you set the EnablePageMethods property on your ScriptManager to true?

  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-09-2008, 3:49 PM
    • Loading...
    • cjbnj
    • Joined on 05-09-2008, 7:44 PM
    • Posts 1

    You are getting this error because your clientside HTML code is trying to call your serverside methods... Unless, you have SetSessionValue and GetSessionValue client-side functions defined in your PageMethod.js file you are including. You need to route through PageMethods object in the client-side to call these server-side methods.. So modify your input onclicks to be PageMethods.SetSession... or PageMethods.GetSession... etc

  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-09-2008, 4:00 PM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    Folks, I *think* I've done everything correctly.  I've prepared a sample for you all here.  If you can show me the error of my ways, I'd much appreciate it!

    Robert 

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-09-2008, 8:45 PM
    • Loading...
    • Sashka
    • Joined on 05-10-2008, 12:40 AM
    • Posts 1

    Hello... sorry about my English.

    In the MasterPage you forgot EnablePageMethods

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

    In the ContentPage, you forgot "PageMethods"

    <input type="Button" onclick="PageMethods.SetSessionValue('SessionValue', Date())" value="Write" />

    It works fine.

    Sashka 

     

  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-10-2008, 1:41 AM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    Sashka,

    Thank you for this.  The code now runs without error.  But on my system it doesn't actually do anything!  I was expecting the current time & date to appear with an aqua background.  But nothing happened!  Does it correctly work on your computer?

    Robert 

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-10-2008, 10:40 PM
    • Loading...
    • gt1329a
    • Joined on 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 1,520

    You've referenced PageMethod.js in your sample, but didn't include it in the project.  That's what's going to handle the PageMethod's callback and then update the span on the client side.

    http://www.asp.net/AJAX/Documentation/Live/ViewSample.aspx?sref=Sys.Net.PageMethod/cs/PageMethod.js

  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-10-2008, 11:54 PM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    Dave,

    Accepting the possibility that I haven't drank enough coffee today, I clearly must be losing my brain!  I have built a test project now to what I believe to be 100% correct.  It runs but it still doesn't do anything.  I've zipped it up here.

    Robert

    P.S. Great technical blog by the way.  I was reading one of your articles yesterday and plan to read many more of them in the near future!


     

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-10-2008, 11:57 PM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    Incidentally, on that sample site you provided, I just went here.  I tried clicking on the "Server Time" button.  In Firefox 2.0 it did nothing and in IE7 I got constant errors.  Is the same occurring on your computer?

    Robert

     

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-11-2008, 12:11 AM
    • Loading...
    • gt1329a
    • Joined on 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 1,520

    In your sample, remove the PageMethods prefix from the onclick event of the buttons.

    The way that sample's set up is a little confusing.  PageMethod.js has a couple of functions with the same name as the server side methods, so it makes it harder to understand what's going on.  It's important that you call the JavaScript functions in this example though, because they include code to wire up the callback handlers that display the results of the PageMethods call.

  • Re: Trying to use PageMethods with a Master Page and Content Page

    05-11-2008, 12:49 AM
    • Loading...
    • rmdw
    • Joined on 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 369

    Wow!  It finally works.  Thank you for much for your patience, Dave!!

    Robert
     

    Robert Werner
    Vancouver, BC
    www.pocketpollster.com/beta
Page 1 of 1 (11 items)