Documente Academic
Documente Profesional
Documente Cultură
Introducing the
By:
Praveen Kumar
Introduction
Client Server Architecture AJAX(Asynchronous JavaScript and XML)
AJAX = JAVASCRIPT+HTML+DHTML+DOM+CSS+XML
What is AJAX, and why do we care? The XmlHttpRequest revolution
Agenda
Understanding AJAX & XmlHttpRequest Understanding ASP.NET AJAX ASP.NET AJAX Architecture ASP.NET AJAX Client Life-Cycle events ASP.NET 2.0 AJAX Extensions ASP.NET AJAX Control Toolkit Anatomy of Toolkit Component
Asynchronous JavaScript And XML = is a web development technique for creating responsive web applications by exchanging small amounts of data with the server behind the scenes
Overview of AJAX
Browser
Browser submits async XMLHTTP request to server; UI remains responsive; page doesn't flash
Web Server
Client making request To JAVASCRIPT Not Server. User may continue to work.
XmlHttpRequest
http://www.w3.org/TR/XMLHttpRequest/
Object Instantiation.
function getobject(){ var reqst = false; try{ reqst = new XMLHttpRequest(); } catch(msnew){ try{ reqst = new ActiveXObject(Msxml2.XMLHTTP); } catch(msother){ try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; } }}}
Methods :
open(); Sets up a new request to a server. send(); Send a request on server. readystate; provides current request State. responseText; The text server send back by server. responseXML; The Server generated XML response.
ReadyState :
0: The request is uninitialized (before call open()). 1: The request is set up, but not sent (before call send()). 2: The request was sent and is in process. 3: The request is in process; 4: The response is complete; you can get the server's response and use it. Call Back method is passed to request object by assigning the onreadystatechange=function()
Open Method :
The request open method takes following Args. request-type: The type of request to send (Get,Post). url: The URL to connect to. asynch: Pass here true/false (true if asynch else false). username: User name, if authentication required; password: Password, if authentication required. For Example : open(GET,/myserver/abc.aspx?p1= + p1,true); open(POST,/myserver/abc.aspx,true);
Send Method :
The parameter for send method is the data in case of data sent true post method
send(vname);
Demo
Create Request
<script language="javascript" type="text/javascript"> var request = false; try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; }}}
if (!request) alert("Error initializing XMLHttpRequest!"); function getCustomerInfo() { var phone = document.getElementById("phone").value; var url = "/cgi-local/lookupCustomer.php?phone=" + escape(phone); } </script>
Open Request
function getCustomerInfo() { var phone = document.getElementById("phone").value; var url = "/local/lookupCustomer.aspx?phone=" + escape(phone); request.open("GET", url, true); }
} } }
200 301 302 305 400 401 403 404 408 500
(OK, Request Processed Successfully and Responded.) (Moved Permanently) (Found and Redirected to some other URL) (the request must use a proxy to access the resource requested) (Bad Request) (Unauthorized) (Forbidden) (Not Found) (Request Time Out) (Internal server Errors)
ASP.NET AJAX
Microsoft AJAX Library (client-side framework) ASP.NET 2.0 AJAX Extensions (server-side framework)
Familiar UI elements such as progress indicators, tool tips, and pop-up windows. A framework that simplifies customization of server controls to include client capabilities.
ASP.NET 2.0
Script Core Browser Compatibility Page, Framework, Server controls Application Services
Server Framework
Client
XHTML/CSS Server Scripts ASPX
Server
ASMX
ASP.NET 2.0
Browser Compatibility Browsers (IE, Firefox, Safari, others) Page Framework and Server Controls Application Services
Object oriented framework build on JavaScript Provides automatic cross-browser compatibility layer Built-in classes for WebService calls, Type system, basic DOM operations Allows structured programming and encapsulation No more sprinkling JavaScript around your site!
Built-in Web services provide bridge to app services ASMX extensions and JSON serializer enable Web services to be called through JavaScript proxies
ScriptManager, UpdatePanel, and others Emit content that leverages the client-side framework Familiar drag-and-drop design paradigm
Script Management
ScriptManager ScriptManagerProxy
Partial Rendering
UpdatePanel
Extenders
AutoCompleteExtender DragOverlayExtender
Service Wrappers
ProfileScript
UpdateProgress
Timer
ScriptManager- One per page to support AJAX features ScriptManagerProxy - Use when a .master contains a ScriptManager already UpdatePanel - Allows partial-page updates Timer - Raises event at regular interval UpdateProgress - Send progress messages back to page
To enable partial page update set EnablePartialRendering=true of ScriptManager control. Only one instance of the ScriptManager control can be added to the page. Syntax: <asp:scriptmanager id=ScMngr runat=server EnablePartialRendering="true"></ asp:scriptmanager>
The "crown jewel" of ASP.NET AJAX Adds partial rendering to pages and controls
Automatically converts postbacks into async callbacks Automatically updates content using callback results Requires no knowledge of JavaScript or XmlHttpRequest Upside: Extremely easy to use, widely applicable Downside: Not as efficient as hand-coded AJAX
Exemplifies value added by AJAX frameworks A control that causes an asynchronous postback is referred to as a trigger
<asp:ScriptManager ID="..." Runat="server" EnablePartialRendering="true" /> . . <asp:UpdatePanel ID="..." Runat="server"> <Triggers> <!-- Define triggers (if any) here --> </Triggers> <ContentTemplate> <!-- Define content here --> </ContentTemplate> </asp:UpdatePanel>
UpdateProgress
Provides visual feedback in the browser when the content of UpdatePanel controls is updated. Associate an UpdateProgress control with an UpdatePanel control by setting the AssociatedUpdatePanelID property of
Timer
The Timer control performs automated postbacks at defined intervals. The JavaScript component initiates the postback from the browser when the interval that is defined in the Interval property has elapsed. The Interval property is defined in milliseconds and has a default value of 60,000 milliseconds, or 60 seconds. When a postback was initiated by the Timer control, the Timer control raises the Tick event on the server. Used with updatepanel control. <asp:Timer ID="Timer1" runat="server">
</asp:Timer>
The two main Microsoft AJAX Library classes that raise events during the client lifecycle of a page are the Application and PageRequestManager classes. The key event for initial requests (GET requests) and synchronous postbacks is the load event of the Application instance. When script in a load event handler runs, all scripts and components have been loaded and are available. When partial-page rendering with UpdatePanel controls is enabled, the key client events are the events of the PageRequestManager class. These events enable you to handle many common scenarios. These include the ability to cancel postbacks, to give precedence to one postback over another, and to animate UpdatePanel controls when their content is refreshed.
PageRequestManager Class
The Microsoft AJAX Library manages partial-page updates in the Browser. Exposes properties, methods, and events that enables customize partial-page updates with client script. Use When Required to control: -
How multiple asynchronous postbacks are processed. Display status messages during asynchronous postbacks and opportunity to cancel the processing. Provide custom error-message handling for partial-page updates. Access the underlying request and response objects that are used for the asynchronous postbacks.
Raised before the request is initialized for an asynchronous postback Raised just before the asynchronous postback is sent to the server Raised after the response to the most recent asynchronous postback has been received but before any updates to the page have been made.
Raised after page regions are updated after the most recent postback Raised when request processing is finished
initializeRequest
beginRequest pageLoading
pageLoaded endRequest
More ASP.NET AJAX server controls, plus SDK for control development Community-owned, community-driven, shared source Most of toolkit controls are extender controls An extender is a control which extends the functionality of another control
Demo: ConfirmButtonExtender
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
Behaviors Add or modify functionality to an element on the page Written in JavaScript Modify the HTML DOM Extenders ASP.NET components Associate an ASP.NET Control with a Behavior Leverage Server-Side and Design Time
Two types of components in the Toolkit Behavior + Extender ASP.NET AJAX-aware Control (ScriptControl) Which to choose? If required HTML shape is very specific, write a control and then attach behaviors to it If HTML shape is simple or common, write an extender
ASP.NET AJAX is
Compact and well factored (minimal download size) Compatible with a wide range of browsers Compatible with PHP, ColdFusion, and other platforms
Resources
www.ajaxwebwidgets.com
http://www.asp.net/AJAX http://forums.asp.net/default.aspx?GroupID=34 http://asp.net/AJAX/Documentation/Live/ClientReference/
Lecture Demos/Workshop
http://cds.fhcrc.org/download.aspx
Thank you!