Click here to Skip to main content
12,456,128 members (53,562 online)
Click here to Skip to main content
Add your own
alternative version

Stats

155.9K views
1.8K downloads
41 bookmarked
Posted

Call Server Side Code by JavaScript using Ajax.NET Framework

, 15 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This article discusses how to call Server Side Code by JavaScript using Ajax.NET Framework

Introduction

Lots of times, we need to call server side code using JavaScript (it means Ajax call) and without postback. There are lots of technologies that are available for that. Some people use Ajax.dll to perform this operation. But now, when Ajax.NET framework is available, there is no need to use a third party DLL for Ajax call.

Background

There is no need to use any third party DLL for Ajax Call. Simply add the reference of System.Web.Extensions.

Using the Code

Set the EnablePageMethods="true" in Script Manager
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
<!--
// Javascript function
function CallSum() 
{
//Get the controls
var txt1 = $get("txt1");
var txt2 = $get("txt2");
var txtresult = $get("txtSum");

//Call server side function
PageMethods.Sum(txt1.value,txt2.value,OnCallSumComplete,OnCallSumError,txtresult);

//Server side function gets the 2 arguments arg1 and arg2. 
//We are passing txt1.value and txt2.value
//for that. OnCallSumComplete is callback function for complete successfully. 
//OnCallSumError is callback
//function on error. txtresult is usercontext parameter.

//OnCallSumComplete,OnCallSumError,txtresult are optional parameters.

//If server side code executed successfully, then OnCallSumComplete will call.
//If server side code do not executed successfully, then OnCallSumError will call.
}

// Callback function on complete
// First argument is always "result" if server side code returns void 
// then this value will be null
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
// In this example the methodName will be "Sum"
function OnCallSumComplete(result,txtresult,methodName)
{
//Show the result in txtresult
txtresult.value = result;
}

// Callback function on error
// Callback function on complete
// First argument is always "error" if server side code throws any exception
// Second argument is usercontext control pass at the time of call
// Third argument is methodName (server side function name) 
// In this example the methodName will be "Sum"
function OnCallSumError(error,userContext,methodName)
{
if(error !== null) 
{
alert(error.get_message());
}
}
// -->
</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PareshDehadray1
Web Developer
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
Amol_Joshi10-Dec-11 18:25
memberAmol_Joshi10-Dec-11 18:25 
QuestionGetting a error ain the javascript Pin
dumpy31dec15-Nov-09 23:43
memberdumpy31dec15-Nov-09 23:43 
GeneralPageMethods is undefined Pin
T.EDY6-Jul-09 17:33
memberT.EDY6-Jul-09 17:33 
GeneralMy vote of 1 Pin
Balamurugan R A6-Jan-09 19:21
memberBalamurugan R A6-Jan-09 19:21 
Questionhow we can do this in asp.net 1.1 Pin
kashish329-Apr-08 20:33
memberkashish329-Apr-08 20:33 
GeneralPlease Post The VB Code Pin
cijothomas29-Feb-08 20:35
membercijothomas29-Feb-08 20:35 
GeneralRe: Please Post The VB Code Pin
Brooks Reese30-Mar-09 21:34
memberBrooks Reese30-Mar-09 21:34 
QuestionWhy WEBMETHOD attribute tag ? Pin
nitinjd@rediffmail.com27-Sep-07 20:08
membernitinjd@rediffmail.com27-Sep-07 20:08 
AnswerRe: Why WEBMETHOD attribute tag ? Pin
PareshDehadray127-Sep-07 20:13
memberPareshDehadray127-Sep-07 20:13 
QuestionNot calling server side function Pin
imtiyaz_alamshah15-Jul-07 22:17
memberimtiyaz_alamshah15-Jul-07 22:17 
AnswerRe: Not calling server side function Pin
PareshDehadray115-Jul-07 22:32
memberPareshDehadray115-Jul-07 22:32 
GeneralRe: Not calling server side function Pin
imtiyaz_alamshah16-Jul-07 0:38
memberimtiyaz_alamshah16-Jul-07 0:38 
GeneralRe: Not calling server side function Pin
PareshDehadray117-Jul-07 4:51
memberPareshDehadray117-Jul-07 4:51 
GeneralRe: Not calling server side function Pin
xsoftdev16-Aug-07 12:53
memberxsoftdev16-Aug-07 12:53 
GeneralRe: Not calling server side function Pin
PareshDehadray119-Aug-07 20:51
memberPareshDehadray119-Aug-07 20:51 
QuestionCalling non static methods. Pin
astrobolidos22-Jun-07 5:19
memberastrobolidos22-Jun-07 5:19 
AnswerRe: Calling non static methods. Pin
PareshDehadray122-Jun-07 21:32
memberPareshDehadray122-Jun-07 21:32 
Questioncan we use this in usercontrol ? Pin
frumiweb9-Jun-07 3:11
memberfrumiweb9-Jun-07 3:11 
AnswerRe: can we use this in usercontrol ? Pin
PareshDehadray111-Jun-07 3:56
memberPareshDehadray111-Jun-07 3:56 
GeneralCode snippet, not an article Pin
Mark Nischalke28-May-07 4:26
mvpMark Nischalke28-May-07 4:26 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 16 Jul 2007
Article Copyright 2007 by PareshDehadray1
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid