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();"/>