Click here to Skip to main content
6,595,444 members and growing! (21,061 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Intermediate License: The Code Project Open License (CPOL)

Call Server Side Code by JavaScript using Ajax.NET Framework

By PareshDehadray1

This article discusses how to call Server Side Code by JavaScript using Ajax.NET Framework
C#, Javascript, Windows, .NET, ASP.NET, Visual Studio, WebForms, Ajax, Dev
Version:3 (See All)
Posted:27 May 2007
Updated:15 Jul 2007
Views:64,579
Bookmarked:30 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 3.43 Rating: 2.68 out of 5
7 votes, 36.8%
1

2
4 votes, 21.1%
3

4
8 votes, 42.1%
5

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>
 
Server Side Code:
 
/// <summary>
/// Server side function Sum
/// </summary>
/// <param name="arg1">arg1</param>
/// <param name="arg2">arg2</param>
/// <returns>result(sum)</returns>
[System.Web.Services.WebMethod]
public static int Sum(int arg1, int arg2)
{
//On server side we can do anything, like we can access the Session.
//We can do database access operation. Without postback.
try
{
return arg1 + arg2;
}
catch(Exception ex)
{
throw ex;
}
}

License

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

About the Author

PareshDehadray1


Member

Occupation: Web Developer
Location: India India

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralPageMethods is undefined PinmemberT.EDY18:33 6 Jul '09  
GeneralMy vote of 1 PinmemberBalamurugan R A20:21 6 Jan '09  
Questionhow we can do this in asp.net 1.1 Pinmemberkashish321:33 29 Apr '08  
GeneralPlease Post The VB Code Pinmembercijothomas21:35 29 Feb '08  
GeneralRe: Please Post The VB Code PinmemberBrooks Reese22:34 30 Mar '09  
QuestionWhy WEBMETHOD attribute tag ? Pinmembernitinjd@rediffmail.com21:08 27 Sep '07  
AnswerRe: Why WEBMETHOD attribute tag ? PinmemberPareshDehadray121:13 27 Sep '07  
QuestionNot calling server side function Pinmemberimtiyaz_alamshah23:17 15 Jul '07  
AnswerRe: Not calling server side function PinmemberPareshDehadray123:32 15 Jul '07  
GeneralRe: Not calling server side function Pinmemberimtiyaz_alamshah1:38 16 Jul '07  
GeneralRe: Not calling server side function PinmemberPareshDehadray15:51 17 Jul '07  
GeneralRe: Not calling server side function Pinmemberxsoftdev13:53 16 Aug '07  
GeneralRe: Not calling server side function PinmemberPareshDehadray121:51 19 Aug '07  
QuestionCalling non static methods. Pinmemberastrobolidos6:19 22 Jun '07  
AnswerRe: Calling non static methods. PinmemberPareshDehadray122:32 22 Jun '07  
Generalcan we use this in usercontrol ? Pinmemberfrumiweb4:11 9 Jun '07  
GeneralRe: can we use this in usercontrol ? PinmemberPareshDehadray14:56 11 Jun '07  
GeneralCode snippet, not an article PinsupporterMark Nischalke5:26 28 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Jul 2007
Editor: Deeksha Shenoy
Copyright 2007 by PareshDehadray1
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project