Why doesn't PageMethod work?

I'm using the example from http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/ but keep getting this error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1026: ) expected

Source Error:

 

Line 31:     <ContentTemplate>
Line 32:         <asp:Label ID="Label1" runat="server" Text="Update Me!"></asp:Label>
Line 33:         <asp:Button ID="Button1" runat="server" Text="Postback Update" onclick="UpdateTime();" />
Line 34:     </ContentTemplate>
Line 35:     </asp:updatepanel>
 


Can anyone tell what I'm doing wrong?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageMethods.aspx.cs" Inherits="ajax.PageMethods" %>
 
<!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>Untitled Page</title>
        <script language="javascript" type="text/javascript">
    function UpdateTime() {
      PageMethods.GetCurrentDate(OnSuceeded, OnFailed);
      }
      
      function OnSucceeded(result, userContext, methodName){
      $get('Label1').innerHTML = result;
      }
      
      function OnFailed(result, userContext, methodName){
      $get('Label1').innerHTML = result;
      }
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
 
    <div>
    <asp:updatepanel ID="Updatepanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="Update Me!"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Postback Update" onclick="UpdateTime();" />
    </ContentTemplate>
    </asp:updatepanel>
    </div>
    </form>
</body>
</html>
 
//code behind method:
[System.Web.Services.WebMethod]
        public static string GetCurrentDate()
        {
            return DateTime.Now.ToLongDateString();
        }

Open in new window

brettrAsked:
Who is Participating?
 
jabcocoCommented:
I don't know all about your page structure but if you fallow this example, it should perfectly works.

http://weblogs.asp.net/sohailsayed/archive/2008/02/23/calling-methods-in-a-codebehind-function-pagemethods-from-client-side-using-ajax-net.aspx

Pages.aspx
----------------
<%@ Import Namespace="System.Web.Services" %>
 
<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
        <script language="javascript">
            function CallPageMethod()
            {
                PageMethods.MyFirstPageMethod(onSucceeded,onFailed);
            }
 
            function CallParametersPageMethod()
            {
               PageMethods.MyFirstParameterPageMethod("This is a Demo",onSucceeded,onFailed);
            }
            function onSucceeded(result,userContext,methodName)
            {
              $get('div1').innerHTML=result;
            }
 
            function onFailed(error,userContext,methodName)
            {
              alert("An error occurred")
            }
        </script>
    
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager2" runat="server" EnablePageMethods="true"></asp:ScriptManager>    
        <div id="div1">
        </div>
        <input type="button" value="Call Page Method With No Parameters" onclick="CallPageMethod()" />
        <input type="button" value="Call Page Method With Parameters" onclick="CallParametersPageMethod()" />
    </div>
    </form>
</body>
</html>
 
 
Pages.aspx.cs
----------------------
    [System.Web.Services.WebMethod()]
    public static string MyFirstPageMethod()
    {
        return "Welcome to the world of AJAX.NET ";
    }
 
    [System.Web.Services.WebMethod()]
    public static string MyFirstParameterPageMethod(String strVal)
    {
        return "Welcome to the world of AJAX.NET , the value you passed is : " + strVal;
    }

Open in new window

0
 
NazoUKCommented:
onclick on a server control will try to run a server side function and shouldn't have parenthesis. Use OnClientClick to run Javascript.
0
 
CB_ThirumalaiCommented:
You can also use the following in the Page_Load event in the code behind.
           Button1.Attributes.Add("onclick", "UpdateTime();");
and remove the onclick placed in the <asp:Button tag.  This will ensure that the event is fired in the client side and call the necessary method in the server side to fetch values.
         
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
jabcocoCommented:
First you don't need to use a "UpdatePanel" when working with PageMethod.
Second, change the onclick="UpdateTime();" for OnClientClick"UpdateTime();return false" or change your asp:button for a <input type="button". And this should work.

Check out those link.
http://www.asp.net/AJAX/Documentation/Live/ViewSample.aspx?sref=Sys.Net.PageMethod/cs/PageMethod.aspx

http://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx
0
 
brettrAuthor Commented:
I changed it to use OnClientClick and that error went away.  After clicking the button, I get an undefined on "OnSucceeded" in

function UpdateTime() {
      PageMethods.GetCurrentDate(OnSuceeded, OnFailed);
      }


I looked at the dynamic page output and "onSuccess" is used.  I tried that but get an undefined there too.
0
 
brettrAuthor Commented:
Thanks.  That works.  I'll have to go through and see what the difference is.  BTW, you don't need

<%@ Import Namespace="System.Web.Services" %>
0
 
jabcocoCommented:
Effectively you don't need to import the services class in your context. You will only need that if you directly put the server side code in the aspx page instead of the code behind.

Hope i've help.
0
 
brettrAuthor Commented:
By the way, is the javascript $get() part of asp.net or jquery.  Where is it coming from?  I notice there are several similar functions showing up in intellisense.
0
 
brettrAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.