PageMethods is undefined

Last post 01-07-2009 3:22 AM by ponkarthik. 26 replies.

Sort Posts:

  • PageMethods is undefined

    05-25-2006, 11:45 AM
    • Member
      225 point Member
    • puterart
    • Member since 12-20-2005, 9:27 PM
    • Posts 63
    I have a page which calls a webmethod within a webservice and everything works fine. However, when I create a webmethod within the codebehind file for the page and I try to call it using PageMethods.SomeFunction() I get a javascript error on stating: PageMethods is undefined. What's going on? Am I possibly missing a line in my web.config file?
  • Re: PageMethods is undefined

    05-25-2006, 7:38 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • TrustedFriends-MVPs
    Hi,

    could you post the relevant code?
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: PageMethods is undefined

    05-26-2006, 9:30 AM
    • Member
      225 point Member
    • puterart
    • Member since 12-20-2005, 9:27 PM
    • Posts 63

    <

    atlas:ScriptManager runat="server" ID="ScriptManager">
          
    <Services>
                
    <atlas:ServiceReference path="~/InvestorInformation.asmx" />
          
    </Services>
    </atlas:ScriptManager>


    <

    script type="text/javascript">
    <!-- 

       function
    txtAccountBalance_Change(val)
       {
             PageMethods.CalculateFundPercent(txtAccountBalance_ChangeComplete);
       }

    //-->
    </script>

    CodeBehind:

    [

    WebMethod]
    protected void CalculateFundPercent()
    {
          //Run calculation
    }
  • Re: PageMethods is undefined

    05-26-2006, 6:51 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • TrustedFriends-MVPs
    Hi,

    puterart:

    protected void CalculateFundPercent()


    PageMethods must be exposed with the public access modifier.
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: PageMethods is undefined

    05-27-2006, 5:28 PM
    • Member
      225 point Member
    • puterart
    • Member since 12-20-2005, 9:27 PM
    • Posts 63

    Ugh, how stupid of me. Thanks!

  • Re: PageMethods is undefined

    06-23-2006, 7:31 AM
    • Member
      91 point Member
    • roopkt
    • Member since 06-21-2006, 6:18 AM
    • Mumbai
    • Posts 100

    hi

    I am also getting the error "Microsoft JScript runtime error: 'PageMethods' is undefined".

    my code is following:

    [

    WebMethod]

    public int Add(int num1, int num2)

    {

    return num1 + num2;

    lblText.Text =

    "hi dost";

    }

    >>>>

    <

    form id="form1" runat="server">

    <

    atlas:ScriptManager ID="ScriptManager1" runat="server" />

    </form >

    <

    script type="text/javascript">

    function MyButtonClick()

    {alert("came");

    PageMethods.Add($("tb1").value, $("tb2").value, Add_Complete, Add_Timeout, Add_Error);

    }

    function Add_Complete()

    {

    if(arguments.length >0)

    $("result").innerText = arguments[0].toString();

    }

    function Add_Timeout()

    {

    alert("timeout");

    }

    function Add_Error(result, response)

    {

    alert("Error");

    // One of these 2 objects has the following methods (I forget which one):

    // get_statusText() gives error text

    // get_statusCode() gives error code

    }

    </

    script>

    please reply

    Regards

    Rupesh kumar Tiwari

     

    Rupesh kumar Tiwari
    India
  • Re: PageMethods is undefined

    08-30-2006, 3:27 AM
    • Member
      25 point Member
    • etamer
    • Member since 05-10-2006, 6:36 AM
    • Posts 5

    Hi Rupesh,

     

    did you find a solution for your problem, i do face the same strange behaviour. Is it just possible with a webservice?

     

     

    Regards

     

    Erhan

  • Re: PageMethods is undefined

    08-30-2006, 3:38 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • TrustedFriends-MVPs

    Hi,

     could you post your code?
     

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: PageMethods is undefined

    08-30-2006, 3:55 AM
    • Member
      25 point Member
    • etamer
    • Member since 05-10-2006, 6:36 AM
    • Posts 5

     

    Hi Garbin,

    here is my Code:

    MasterPage.aspx

    1    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="AtlasWebApplication.Site1" %>
    2    <%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>

    3   
    4   
    5   
    6    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    7   
    8    <html xmlns="http://www.w3.org/1999/xhtml" >
    9    <head runat="server">
    10       <title>Untitled Page</title>
    11                       <script language="javascript">
    12       function GetWebMethod()
    13       {
    14       System.Web.Services.PageMethods.SetLabel2();
    15       }
    16   </script>
    17   </head>
    18   <body>
    19       <form id="form1" runat="server">
    20       <div>
    21           <atlas:ScriptManager  ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
    22  
    23           <atlas:TimerControl ID="timer" runat="server" OnTick="HandleTimerEvent" Interval="5000"></atlas:TimerControl>
    24           <atlas:UpdatePanel id="updatePanel" runat="server">
    25               <Triggers>
    26                   <atlas:ControlEventTrigger ControlID="timer" EventName="Tick" />
    27               </Triggers>
    28               <ContentTemplate>
    29                   <asp:Label ID="label" runat="server" Text="leer"></asp:Label>
    30                   <asp:Button ID="button1"  Text="Button1" runat="server" OnClick="SetLabel" />
    31                   <asp:Button ID="button2" Text="PAGEMEthods" runat="server" OnClientClick="GetWebMethod();" />   
    32               </ContentTemplate>
    33           </atlas:UpdatePanel>
    34           <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    35  
    36  
    37           </asp:ContentPlaceHolder>
    38       </div>
    39       </form>
    40   </body>
    41   </html>

     

    In my  MasterPage.cs

     

    [System.Web.Services.WebMethod()]

    public void SetLabel2()

    {

    label.Text = "Durch WebMethod gesetzt.";

    }

     

     

    I do get the PageMethods undefined error

     

    Regards

     

    Erhan

  • Re: PageMethods is undefined

    08-30-2006, 3:55 AM
    • Member
      25 point Member
    • etamer
    • Member since 05-10-2006, 6:36 AM
    • Posts 5

     

    Hi Garbin,

    here is my Code:

    MasterPage.aspx

    1    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="AtlasWebApplication.Site1" %>
    2    <%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>

    3   
    4   
    5   
    6    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    7   
    8    <html xmlns="http://www.w3.org/1999/xhtml" >
    9    <head runat="server">
    10       <title>Untitled Page</title>
    11                       <script language="javascript">
    12       function GetWebMethod()
    13       {
    14       System.Web.Services.PageMethods.SetLabel2();
    15       }
    16   </script>
    17   </head>
    18   <body>
    19       <form id="form1" runat="server">
    20       <div>
    21           <atlas:ScriptManager  ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
    22  
    23           <atlas:TimerControl ID="timer" runat="server" OnTick="HandleTimerEvent" Interval="5000"></atlas:TimerControl>
    24           <atlas:UpdatePanel id="updatePanel" runat="server">
    25               <Triggers>
    26                   <atlas:ControlEventTrigger ControlID="timer" EventName="Tick" />
    27               </Triggers>
    28               <ContentTemplate>
    29                   <asp:Label ID="label" runat="server" Text="leer"></asp:Label>
    30                   <asp:Button ID="button1"  Text="Button1" runat="server" OnClick="SetLabel" />
    31                   <asp:Button ID="button2" Text="PAGEMEthods" runat="server" OnClientClick="GetWebMethod();" />   
    32               </ContentTemplate>
    33           </atlas:UpdatePanel>
    34           <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    35  
    36  
    37           </asp:ContentPlaceHolder>
    38       </div>
    39       </form>
    40   </body>
    41   </html>

     

    In my  MasterPage.cs

     

    [System.Web.Services.WebMethod()]

    public void SetLabel2()

    {

    label.Text = "Durch WebMethod gesetzt.";

    }

     

     

    I do get the PageMethods undefined error

     

    Regards

     

    Erhan

  • Re: PageMethods is undefined

    08-30-2006, 4:24 AM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • TrustedFriends-MVPs

    Hi,

    basically you're mixing two different approaches, so here's what I suggest:

    1. If you need to update some content on the page from server-side, use an UpdatePanel. I see that you're setting the text of a Label from a WebMethod; this is wrong because a WebMethod won't update the page content. The correct approach is to update the Label's text in the Click event (server-side) of button2.
    2. Use a WebMethod (or page method in this case) when you need to run almast all the page lifecycle (ViewState, events, not rendering) but you need only to return a value (or an object).
    3. Don't put javascript references or code in the <head /> section. If you put references, the files will be reloaded on each partial postback. If you put code, it could crash the UpdatePanel because it expects to find well formed XML. Better add js files through the ScriptManager control.
    4. Add the attribute type="text/javascript" to the script tag when declaring js code. The Atlas framework might need it in some cases.
    5. Protect the javascript code in CDATA or comment sections.
    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: PageMethods is undefined

    06-08-2007, 9:25 AM
    • Member
      4 point Member
    • prsuman
    • Member since 05-08-2007, 10:40 AM
    • Posts 19

    HI,

    its Urgent

    I am Getting Same Error Could u please Help me Out..

     Here is My Code..

    using System.Web.UI.HtmlControls;

    using System.Web.Services;

    using Microsoft.Web.Script.Services;

    using Microsoft.Web.Script;

     

    [WebMethod]public void GetNewMessage()

    {

    string str = "Hello";

    Response.Write(str);

    }

     

    Javascript

    PageMethods.GetNewMessage();

     

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

    <Scripts>

    <asp:ScriptReference path="scripts.js" />

    </Scripts>

    </asp:ScriptManager>

     

    Thanks in Advance,

    Suman...

    Please its Urgent...

  • Re: PageMethods is undefined

    08-13-2007, 11:26 AM
    • Member
      78 point Member
    • inmykingdom
    • Member since 03-06-2003, 3:43 AM
    • Belgium
    • Posts 21

    i was getting the same error message,

    my fault was that i was using a usercontrol, and the PageMethod needs to be declared on the PAGE, not on the usercontrol used in the page

  • Re: PageMethods is undefined

    08-13-2007, 7:18 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    I believe you need EnablePageMethods="true" on your ScriptManager.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: PageMethods is undefined

    08-14-2007, 12:58 PM

     also page methods needs to be static in code behind

     [WebMethod]

    public static bool foo 

Page 1 of 2 (27 items) 1 2 Next >