Click here to Skip to main content
15,881,852 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
2.32/5 (13 votes)
7 Oct 20072 min read 77.8K   1K   22   9
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

Introduction

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
//[System.Web.Services.WebMethod]
//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.

History

none

License

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


Written By
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 Pin
Aravind00kumar23-May-11 1:48
Aravind00kumar23-May-11 1:48 
Hi,

This is not working when we use Forms Authentication..

Any solution....

Regards,
Aravind kUmar
GeneralMy vote of 2 Pin
thinkpad_r5004-Apr-11 16:43
thinkpad_r5004-Apr-11 16:43 
GeneralMy vote of 3 Pin
rhjiv27-Jan-11 13:30
rhjiv27-Jan-11 13:30 
GeneralMy vote of 1 Pin
Michael Ulmann14-Mar-10 13:58
Michael Ulmann14-Mar-10 13:58 
Generaltry this solution also hope be useful Pin
Ledo_moon15-Dec-09 1:30
Ledo_moon15-Dec-09 1:30 
GeneralMy vote of 1 Pin
HarishBhattbhatt13-Oct-09 18:41
HarishBhattbhatt13-Oct-09 18:41 
Questionstrange result Pin
kcw9614-May-08 0:00
kcw9614-May-08 0:00 
Generalimmm... Pin
Michael Sync8-Oct-07 20:22
Michael Sync8-Oct-07 20:22 
QuestionRe: immm... Pin
Suresh Thakur9-Oct-07 21:30
Suresh 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.