Click here to Skip to main content
13,191,400 members (78,001 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 7 Oct 2007

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

, 7 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.

You may also be interested in...


Comments and Discussions

QuestionThis is not working Pin
Aravind00kumar23-May-11 1:48
memberAravind00kumar23-May-11 1:48 
GeneralMy vote of 2 Pin
thinkpad_r5004-Apr-11 16:43
memberthinkpad_r5004-Apr-11 16:43 
GeneralMy vote of 3 Pin
rhjiv27-Jan-11 13:30
grouprhjiv27-Jan-11 13:30 
GeneralMy vote of 1 Pin
Michael Ulmann14-Mar-10 13:58
memberMichael Ulmann14-Mar-10 13:58 
Generaltry this solution also hope be useful Pin
Ledo_moon15-Dec-09 1:30
memberLedo_moon15-Dec-09 1:30 
GeneralMy vote of 1 Pin
HarishBhattbhatt13-Oct-09 18:41
memberHarishBhattbhatt13-Oct-09 18:41 
Questionstrange result Pin
kcw9614-May-08 0:00
memberkcw9614-May-08 0:00 
I am using your solution in the following manner, but the returned result is odd. The code is displaying the HTML for the PAGE in the txtState textbox as in the quote below. Do you have any suggestions?

function CallParametersPageMethod()<br />
        {<br />
            var strVal=document.getElementById("txtMailingZip");    <br />
            PageMethods.getCity2(onSucceeded,onFailed);   <br />
            //PageMethods.getCity1(strVal.value,onSucceeded,onFailed);   <br />
        }<br />
<br />
        function onSucceeded(result,userContext,methodName)<br />
        {<br />
            var strVal=document.getElementById("Label1");<br />
            strVal.innerHTML="inSucceed";<br />
            document.form1.txtState.value=result;<br />
        }<br />
<br />
        function onFailed(error,userContext,methodName)<br />
        {<br />
        <br />

[System.Web.Services.WebMethod()]<br />
        public static String getCity1(string strVal)<br />
        {<br />
            return strVal;<br />
        }<br />
       [System.Web.Services.WebMethod()]<br />
        public static String getCity2()<br />
        {<br />
            return "No Parameter";<br />


"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns="" ><head><title> Untitled Page</title></head><body> <form name="form1" method="post" action="test.aspx" id="form1"><div><input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value ....."

Go Beavs!!!

Generalimmm... Pin
Michael Sync8-Oct-07 20:22
memberMichael Sync8-Oct-07 20:22 
QuestionRe: immm... Pin
Suresh Thakur9-Oct-07 21:30
memberSuresh Thakur9-Oct-07 21:30 

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

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

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