About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2024

to call server side function from Javascript function?

Some times we need to call server side function from Javascript function.For example,callig a PoupWindow(with value from code behind) on HyperLink click. 

This must be implemented by creating a additional request to the web server and that’s the only way to invoke methods on the server. Page methods are used for this.

What is PageMethod?

A PageMethod is a public static method that is exposed in the code-behind of a page and can be called from the client script. PageMethods are annotated with [System.Web.Services.WebMethod] attribute.

The code is simple..

Server side function

[code:c#]

[System.Web.Services.WebMethod] public static void Score(string ID, string name, string Role) {
         try {
                //Some data manipulations 
                  return finalScore ; 
              }
         catch (Exception ex) { 
               //Response.Write(ex.ToString());
                                  } 
            } 

[/code]

//Client Side function 

[code:c#]

function score()
              {               
                  PageMethods.Score(txtID.value,txtName.value,txtRole.value);            
               } 

[/code]

<asp:HyperLink ID="btn_Test" runat="server" Text="Calculate" OnClientClick="javascript:return score();"/> 

Tags:
Categories: AJAX | ASP.NET | C#
Posted by vijay on Friday, August 24, 2007 12:23 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading