You are on page 1of 3

Step by Step:How to use Web Services in ASP.NET AJAX In my Article Preventing Duplicate Date With ASP.

NET AJAX Ive used ASP.NET AJAX With We Service Techn!l!gy" There#!re I add this t!pic as an intr!ducti!n h!$ t! access We services #r!m client script in AJAX%ena led ASP.NET We pages. As $ell I $rite this t!pic t! ans$er the c!mm!n &uesti!ns $hich m!st !# the devel!pers #ace $hile $!r'ing $ith ASP.NET A(a) We Services especially in *icr!s!#t ASP.NET !##icial #!rum http+,,#!,. ASP.NET ena les y!u t! create We services can e accessed #r!m client script in We pages y using AJAX techn!l!gy t! ma'e We service calls. Data is e)changed asynchr!n!usly et$een client and server" typically in JS-N #!rmat. .ets g! a head $ith the steps + /%0reate a ne$ pr!(ect " i# y!u are using 1S 2334 y!u have t! create ASP.NET A(a) Ena led We site. 2%Add ne$ Item " 0h!!se We Service #ile .

5%T! ma'e y!ur We Services accessi le #r!m the script" #irst it must e an .asm) We service $h!se We service class is &uali#ied $ith the ScriptServiceAttri ute attri ute and every meth!d y!u are using t! e called #r!m 0lient script must e &uali#ied $ith the We *eth!dAttri ute attri ute. -n the !ther hand y!u can use y!ur We page6 0S !r 17 #iles8 t! add static meth!ds accessi le #r!m 0lient Script " (ust y!u need t! add We *eth!d Attri ute and set the Ena lePage*eth!ds attri ute !# the Script*anager c!ntr!l t! true.. The !ther c!nditi!n is t! register the Script9andler:act!ry 9TTP handler" $hich pr!cesses calls made #r!m script t! .asm) We services + ;system.$e < ;http9andlers< ;rem!ve ver =>?> path=>?.asm)>,< ;add ver =>?> path=>?.asm)> type=>System.We .Script.Services.Script9andler:act!ry> validate=>#alse>,< ;,http9andlers<

;system.$e < ut this already added aut!matically #!r any We .c!n#ig #ile !# any ASP.NET AJAX Ena led We Site !r Pr!(ect" S! y!u d!nt need t! add it. @%Av!id the de#ault *eth!d 9ell!W!rld" then add y!ur meth!d in y!ur asm) #ile lets say -urServer-utput " As a c!nse&uence y!ur We service $ill e li'e this + using using using using using SystemA System.0!llecti!ns.BenericA System.We A System.We .ServicesA

CWe Service6Namespace = >http+,,tempuri.!rg,>8D CWe Service7inding60!n#!rmsT! = WsiPr!#iles.7asicPr!#ile/E/8D CSystem.We .Script.Services.ScriptServiceD pu lic class We Service + System.We .Services.We Service F CWe *eth!dD pu lic string -urServer-utput68 F return >The Server Date and Time is + > G DateTime.N!$.T!String68A H H 4%Add Script*anager 0!nt!l t! y!ur asp) #ile then re#erence the We service y adding an asp+ServiceIe#erence child element t! the Script*anager c!ntr!l and setting its path attri ute t! p!int t! the We service" That generate a JavaScript pr!)y class #!r calling the speci#ied We service #r!m client script. ;asp+Script*anager runat=>server> ID=>script*anager>< ;Services< ;asp+ServiceIe#erence Path=>We Service.asm)> ,< ;,Services< ;,asp+Script*anager< 7asically "t! ena le y!ur applicati!n t! call We services6.asm) #iles8 y using client script" the server asynchr!n!us c!mmunicati!n layer aut!matically generates JavaScript pr!)y classes. A pr!)y class is generated #!r each We service #!r $hich an ;asp+ServiceIe#erence< element is included under the ;asp+Script*anager< c!ntr!l in the page. J%0reate ne$ button t! call the JavaSciprt #uncti!n and a label t! display the returned value . ;input id=> tn0allDateTime> type=> utt!n> value=>0all We Service> !nclic'=>0allDateTime68>,< ;asp+.a el ID=>l l-utput> runat=>server> Te)t=>.a el><;,asp+.a el<

K%De#ine the JavaScript c!de t! call the We Service + ;script language=>(avascript> type=>te)t,(avascript>< #uncti!n 0allDateTime68 F We Service.-urServer-utput6-nSucceeded8A H #uncti!n -nSucceeded6result8 F var l l-utput = d!cument.getElement7yId6>l l-utput>8A l l-utput.inner9T*. = resultA H ;,script< CallDateTi e #uncti!n calls the We Service *eth!d -urServer-utputL

!nSuccee"e" #uncti!n Msed as the call ac' #uncti!n that pr!cesses the We Service return value. $hich the result parameter is a simple parameter c!ntain the Server Date Time value returned #r!m the We Service . :inally " $hen y!u c!mplete these steps and run y!ur applicati!n y!u can press the utt!n and retrieve Server Date time $ith!ut p!st ac'. Conclusion: In this t!pic I descri es h!$ t! access We services #r!m client script in AJAX%ena led ASP.NET We pages With a #ull .NET :rame$!r',JS-N serialiNe" direct integrati!n $ith the #amiliar .asm) We services "Msing simple e)ample"Also you can connect wit# t#e "atabase to return value by create Web$et#o" in your Web Service %ile an" t#e sa e steps you can use. Ne)t time I $ill sh!$ y!u m!re c!mple) e)ample $hich returns a c!mple) type li'e ! (ects.