Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Javascript
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:
<script type="text/javascript" language="javascript">
        function servermethod() {
            var name = $get("txtname").value;
            PageMethods.CallingserverMethod(name);
 
            alert(name);
        }
 

Code Behind:
[WebMethod]
      public static string CallingserverMethod(string name)
      {
 
          return "Hi" + name + Environment.NewLine + "This is an example";
      }
Posted 30-Jan-13 23:45pm
Comments
Ankur\m/ at 31-Jan-13 5:08am
   
Is the page getting posted back?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

.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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 329
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100