Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
8 Oct 2007 1  
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



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