Saturday, April 27, 2013

Call a code-behind method from javascript using pagemethods in asp.net

Lets see how to call a code-behind method from javascript using pagemethods.

1. Here i will be calling the codebehind method on the button click event.

2. In the form tag add the Scriptmanager and  button element.
On button "onClientClick" property i will call the JavaScript function and in that i will call the codebehind method.

To call a code behind method in the JavaScript function we need to prefix the code behind method name with "pagemethods.<your code behind method name>".

Below is the html markup code. Here CallJS() function will get called on the button click and under onSucceed() function i will display the message returned from the codebehind method and if there is any error while returning the message onError() will get called.



3. Now in the code behind page we will define the required method which has to be called. Just above the method name we will write "[System.Web.Services.WebMethod]" so that the method is available on the client side.

4.Now Run the project to get the expected result.


No comments:

Post a Comment