Views
3 years ago

DNDJ 3-6 June.indd - sys-con.com's archive of magazines - SYS ...

DNDJ 3-6 June.indd - sys-con.com's archive of magazines - SYS ...

ASP.NET Web FormsAUTHOR

ASP.NET Web FormsAUTHOR BIOs:By: Tommy Newcomb: Tommy Newcomb has been working inIT consulting since 1998. His main focushas been Web application/E-commercework using Microsoft tools. Tommy livesin Plainfield, IL with his wife, pugglepuppy, and soon-to-be new baby.4zoomonkey@gmail.comNext, let’s look at the threeobjRequest lines. First, theobjRequest.onreadystatechangeis called. The onreadystatechangeproperty helps us set up a callbackfunction. This callback will becalled only when the readyStateproperty changes; that is, when weget data back from the Web server.The callback function will handleit at that time.The objRequest.open requiresthree parameters: a GET or POST,a string for the “url” we definedearlier, and a Boolean valuethat defines whether this call isasynchronous or not. Note that ifthis Boolean value is set to true,as it is here, a callback function isrequired.The objRequest.send(null)line actually calls the aspx pagedefined in the “url” variable.However, before we go on to theaspx page, notice the callbackfunction (Process()). Remember,this is the function that will becalled (back) after the Web pagecode has processed our request;it’s our reentry point. Here, wesimply take the values returnedfrom our aspx page, and putthem into the second text box(txtEchoOutPut).For our aspx page, I made thingsas simple as possible. It receivesthe keystroke as a querystring valueand I add some text to the string(“You typed:”) to prove that we hitthe server, and then we echo backthe same text. After this aspx page ishit, the callback function (Process())is fired within the JavaScript, asdescribed earlier.From the user’s perspective,this is all done very quickly. Whena user types in the first text box,their letters appear within thesecond text box immediately. Thetyped text is actually making around trip to the server and back.4Ajax Is Not NewIt should be noted that Ajaxis not new. The methodologyhas been around for years. Websites like Google are now provingAjax’s usefulness, stability, andthe ability to make the Webclosely resemble that of a desktopapplication: the holy grail of Webdevelopment. And what’s specialabout Ajax is that it can do allof this using proven, existingtechnology. In other words, anystandard browser (that can handleJavaScript and the DOM) willwork. You don’t need to installsomething separate, like a plug-in.Here at Magenic, we’re takinga look at how this methodologycan benefit our clients. Ajax is notsomething that will replace everyWeb site, as we know it, but it hasa place and it’s a skill we want tohave in our repertoire.4ASP.Net 2.0ASP.Net 2.0 significantlyenhances the scripting model toincorporate this methodology.They call it script callbacks insteadof Ajax. It works essentially thesame as I described earlier, butASP.Net 2.0 takes it a step furtherby providing tools and support.For a comprehensive explanationof script callbacks in ASP.Net 2.0,take a look at this article: http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/.4Considerations1. Ajax blurs the line between correcttiering techniques since muchof the work has been moved tothe client. Consideration shouldbe given when designing suchapplications using an emergingmethodology. The client (browser)is doing more of the processingwork, and the JavaScript toaccomplish this is fairly complex.It’s handling keystrokes, mouseclicks, interaction with the DOM,processing of these events, anddata coordination with the server.2. It should also be noted that manyusers might not want to runJavaScript on their browsers. YourWeb site audience is a consideration.3. The name Ajax is not official. Thefolks at Adaptive Path are givencredit for this catchy name. In ASP.Net 2.0, it is referred to as “scriptcallbacks.”4References• Adaptive Path. I believe JesseJames Garrett actually gave it theAjax name:– www.adaptivepath.com/publications/essays/archives/000385.php– www.codeproject.com/aspnet/GoogleSuggestDictionary.asp• ASP 2.0 Script Callbacks:– http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/– www.omnytex.com/articles/xhrstruts/– www.quirksmode.org/blog/archives/2005/03/ajax_promise_or.html– www.devarticles.com/c/a/XML/XML-in-the-Browser-Submitting-forms-using-AJAX/– http://indicthreads.com/blogs/189/adopting_ajax.html– http://sleepyhead81.blogspot.com/2005/01/web-applications-in-2005-case-study.html– http://caseyporn.com/blog/archives/000675.htmlListing 1: HTMLStartPage.htmvar objRequest;function SendValue(val){//Initialize *************************************try{ //MS IE BrowsersobjRequest=new ActiveXObject(“Msxml2.XMLHTTP”);}catch(e){try{//All Non-IE BrowsersobjRequest=new ActiveXObject(“Microsoft.XMLHTTP”);}catch(oc){objRequest=null;}}32June 2005 • dotnetdevelopersjournal.com

if(!objRequest&&typeof XMLHttpRequest!=”undefined”){ objRequest=new XMLHttpRequest();}//*********************************************//Web page that handles requestsvar url=”http://localhost/VBNETSample/HandleAjaxRequests.aspx?sStringIn=” + val;}if(objRequest!=null){ objRequest.onreadystatechange = Process;objRequest.open(“GET”, url, true);objRequest.send(null);}function Process(){if (objRequest.readyState == 4)//A “4” value means we can now use theXMLHttpRequest returned data{ if (objRequest.status == 200){ document.getElementById(“txtEchoOutPut”).innerText = objRequest.responseText; //IE//document.getElementById(“txtEchoOutPut”).innerHTML = objRequest.responseText; //Other browsers}else{ document.getElementById(“txtEchoOutPut”).innerHT-ML= “There was a problem retrieving data:” +objRequest.statusText;}}}Type in this text box:Output will appear here:Listing 2: HandleAjaxRequests.aspxPublic Class HandleAjaxRequestsInherits System.Web.UI.PagePrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.LoadDim sStringOut As String ʻCreate a stringvariable to echo back to the browsersStringOut = “You typed: “ + Request.QueryString(“sStringIn”).ToString()Response.Write(sStringOut) //Echo back thetext that we receivedEnd SubEnd Classwww.vni.comNothing But .NETIMSLC# Numerical Library for Microsoft® .NET Applications• Sheer Innovation – the onlycomprehensive numerical libraryavailable for the C# and VisualBasic .NET languages• Pure Code – written in 100% C#• Complete Ease – delivers seamlessoperability from within the VisualStudio Environment• Absolute Dependability – accurate andreliable numerical analysis based onover three decades of expertiseS P E C I A L P R O M O T I O NBuy One, Get One Free Plus 50% Off Servers* or RuntimesBuy one IMSL C# Library development seat and get one free, PLUS receive50% off servers* or runtimes. Offer valid through June 30, 2005. Takeadvantage of this special offer today: www.vni.com/nothingbutNET/ndj* Visual Numerics sells server and runtime software licenses. The company does not sell hardware.dotnetdevelopersjournal.com • June 200533

DNDJ 6-3 web.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 6-3WEB.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 3-11 Nov.indd - sys-con.com's archive of magazines - SYS ...
DNDJ 6-4 web.indd - sys-con.com's archive of magazines - SYS ...
DNDJ February 3-2.indd - sys-con.com's archive of magazines ...
DNDJ 4-9.indd - sys-con.com's archive of magazines - SYS-CON ...
DNDJ 3-7 July.indd - sys-con.com's archive of magazines - SYS ...
JAVA Vol 3 Issue4 - sys-con.com's archive of magazines
DNDJ 4-8.indd - sys-con.com's archive of magazines - SYS-CON ...
JDJ 10-10.indd - sys-con.com's archive of magazines - SYS-CON ...
LWM 3-12.indd - sys-con.com's archive of magazines - SYS-CON ...
issue 6 - sys-con.com's archive of magazines - SYS-CON Media
in PowerBuilder - sys-con.com's archive of magazines - SYS-CON ...
JDJ 10-12 Dec.indd - sys-con.com's archive of magazines
JDJ 12-1 Jan.indd - sys-con.com's archive of magazines - SYS-CON ...
Conclusion - sys-con.com's archive of magazines
LinuxWorld.com - sys-con.com's archive of magazines - SYS-CON ...
DNDJ 2-8.qxd - sys-con.com's archive of magazines - SYS-CON ...
WebSphere vs WebLogic: - sys-con.com's archive of magazines ...
jdjedge conference & expo - sys-con.com's archive of magazines ...
JDJ 10-6.indd - sys-con.com's archive of magazines - SYS-CON Media
issue 10 - sys-con.com's archive of magazines - SYS-CON Media
MX - sys-con.com's archive of magazines - SYS-CON Media
!"0&$+ ,!" - sys-con.com's archive of magazines - SYS-CON Media
issue 9 - sys-con.com's archive of magazines - SYS-CON Media
SCOTT McNEALY - sys-con.com's archive of magazines - SYS-CON ...
Figure 1 - sys-con.com's archive of magazines - SYS-CON Media
Bulletproof Web Services - sys-con.com's archive of magazines ...
Web Services Edge 2003 - sys-con.com's archive of magazines ...
NET - sys-con.com's archive of magazines - SYS-CON Media