Click here to Skip to main content
Click here to Skip to main content

How to call code behind methods / Server side script / Server side static methods / Non static Methods using java script / .Net

, 8 Oct 2007
Rate this:
Please Sign up or sign in to vote.
The basic idea of designing this module is to give an idea to call server side methods or sever side script using java script and Asp.Net. In this example I am calling Static methods and non static methods using two different techniques. Ajax.Net , Client Callback


The basic idea of designing this module is to give an idea to call server side methods or sever side script using java script and Asp.Net. In this example I am calling Static methods and non static methods using two different techniques. Most of programmer are aware of these techniques, here by demonstrating it, I am trying to simplify things.

1) Using Ajax.Net Script manager to call static methods
2) Client call back to call non static methods

Using Ajax.Net Script manager to call static methods:

Important points to note down while using this technique:- .aspx page

Step 1: Make property EnablePageMethods="true" Of ScriptManger

//<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true"  runat="server"/>

Step 2: To call server side method in client side script use 'PageMethods' object which is created by Script manger implicitly.

//PageMethods.ServerSideStaticMethod(ServerParams,Callback Client side mehod to receive resulty

<pre>//    , Callback method to receive any error during call)

Step 3:To get the result back on client side use the following method

//JS function
//function OnSucceeded(result, userContext, methodName)

Step 4:To catch any error if happened durin processing use

//JS function
//function OnFailed(error, userContext, methodName) 

Use .cs file:-Step 5::Mark your public method in code behind with System.Web.Services.WebMethod and with static modifyier

//Code behind
//public static String CallServerForDate()

Using Client call back to call non static methods:

Important point to node down while using this technique:-

User .cs file:-Step 1 : First of all your page class should implement ICallbackEventhandler and its two methods GetCallbackResult() and RaiseCallbackEvent()

Step 2:Register Client side call back method signature from code behind using GetCallbackEventReference method of ClientScriptManger class

//Code behind
//Page.ClientScript.GetCallbackEventReference(this, "arg",
//     "CallbackOnSucceeded", "context", "CallbackOnFailed", true);

Step 3 : Register one more client side function which actualy call the RaisCallbackEvent mehotd in server side script , using RegisterClientScriptBlock method of ClientScriptManger class as CallServer() this method implicitly call Client side callback methods in order to make some kind of data communication with Server.

//Code behind
//String clientFunction = "function CallServer(arg, context){ "
//                       + callBack + "; }";
//       Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
//                "Call To Server", clientFunction, true);


Step 4:Define server side method and call it inside RaiseCallbackEvent() method.

//Code behind
//void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
//    {
//        returnValue = ServerSideMethod(eventArgument); 
//    }


Step 5: And result will be send back by GetCallBackResult() methods from the code behind

//Code behind
//string ICallbackEventHandler.GetCallbackResult()
//    {
//        return returnValue;
//    }


Step 6: Define the method signature using java script which were declared in step 2

//JS function
//function CallbackOnSucceeded(result,context){}
//function CallbackOnFailed(result,context){}


Step 7: And make a call to Client side method CallServer() using java script, which were defined in step 3.

Points of Interest

Both of these techniques are very powerful while developer want to pass very small information between client and server.





This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Suresh Thakur
Web Developer FCS Software Solutions Ltd., Noida ( INDIA )
India India
Love to develope web based applications using Microsoft technologies.

Comments and Discussions

QuestionThis is not working PinmemberAravind00kumar23-May-11 2:48 
GeneralMy vote of 2 Pinmemberthinkpad_r5004-Apr-11 17:43 
GeneralMy vote of 3 Pingrouprhjiv27-Jan-11 14:30 
GeneralMy vote of 1 PinmemberMichael Ulmann14-Mar-10 14:58 
Generaltry this solution also hope be useful PinmemberLedo_moon15-Dec-09 2:30 
GeneralMy vote of 1 PinmemberHarishBhattbhatt13-Oct-09 19:41 
Questionstrange result Pinmemberkcw9614-May-08 1:00 
Generalimmm... PinmemberMichael Sync8-Oct-07 21:22 
When I saw the title of your article, I thought it is interesting.. because you said that you are gonna show us the way how to call the server-side function from client-side...
but when I read the introduction, I noticed that you are gonna use Ajax.Net to call those server-side function. (I think the most of ppl know about Ajax framework so... )
another thing is that we can call the web service function from Javascript without using any Ajax framework..
Thanks and Regards,
Michael Sync ( Blog:

QuestionRe: immm... PinmemberSuresh Thakur9-Oct-07 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 8 Oct 2007
Article Copyright 2007 by Suresh Thakur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid