Click here to Skip to main content
15,886,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am calling a server method from a java script function but it is getting invoke. So what is the problem it is having. Please advise me.

This is the script:
XML
<script type="text/javascript" language="javascript">
        function servermethod() {
            var name = $get("txtname").value;
            PageMethods.CallingserverMethod(name);

            alert(name);
        }



Code Behind:
C#
[WebMethod]
      public static string CallingserverMethod(string name)
      {

          return "Hi" + name + Environment.NewLine + "This is an example";
      }
Posted
Comments
Ankur\m/ 31-Jan-13 5:08am    
Is the page getting posted back?

1 solution

.cs file
[ScriptMethod, WebMethod]

   public static string CallingserverMethod(string name)
   {
       return "Hi" + name + Environment.NewLine + "This is an example";
   }


.aspx file

<script type="text/javascript">
      function servermethod()
      {
          PageMethods.CallingserverMethod(onSuccess, onFailure);
      }

  function onSuccess(result) {
          alert(result);
      }


      function onFailure(error) {
          alert(error);
      } 

</script>


Then call this servermethod() on click of button in your .aspx code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900