Click here to Skip to main content
Sign Up to vote bad
good
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 - 22:45

Comments
Ankur\m/ - 31 Jan '13 - 5:08
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 355
1 OriginalGriff 330
2 Arun Vasu 264
3 Tadit Dash 221
4 CPallini 203
0 Sergey Alexandrovich Kryukov 10,005
1 OriginalGriff 7,654
2 CPallini 4,171
3 Rohan Leuva 3,447
4 Maciej Los 2,974


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid