Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Why doesn't PageMethod work?

Posted on 2009-02-08
9
Medium Priority
?
500 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

0
Comment
Question by:brettr
9 Comments
 
LVL 15

Assisted Solution

by:NazoUK
NazoUK earned 200 total points
ID: 23582790
onclick on a server control will try to run a server side function and shouldn't have parenthesis. Use OnClientClick to run Javascript.
0
 
LVL 12

Assisted Solution

by:CB_Thirumalai
CB_Thirumalai earned 200 total points
ID: 23583527
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
 
LVL 10

Expert Comment

by:jabcoco
ID: 23583698
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:brettr
ID: 23584989
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
 
LVL 10

Accepted Solution

by:
jabcoco earned 1600 total points
ID: 23585561
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
 

Author Comment

by:brettr
ID: 23585751
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
 
LVL 10

Expert Comment

by:jabcoco
ID: 23585770
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
 

Author Comment

by:brettr
ID: 23585855
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
 

Author Comment

by:brettr
ID: 23586015
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question