We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Why doesn't PageMethod work?

brettr
brettr asked
on
Medium Priority
693 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Commented:
onclick on a server control will try to run a server side function and shouldn't have parenthesis. Use OnClientClick to run Javascript.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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.
         

Commented:
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

Author

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.
Commented:
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

Author

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

Commented:
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.

Author

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.

Author

Commented:
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.