Vous êtes sur la page 1sur 21

ASP.net ajax basics, Ajax Formworks basics ,ASP.

net with Ajax Frameworks

1. Introduction 2. History 3. Architecture 4. First Application with Ajax 5. Ajax Controls 6. ScriptManager Control 7. ScriptManagerProxy Control 8. Timer control 9. UpdatePanel control 10. UpdateProgress Control 11. Ajax Control Toolkit Download and Install 12. AjaxToolkitExtender Controls
ASP.net ajax basics, Ajax Formworks basics ,ASP.net with Ajax Frameworks

What is AJAX?
Ajax (Asynchronous JavaScript and XML) is a new paradigm introduced in 2005. To use Ajax, able to trade data, with a web server, without having to load a new page. The purpose of Ajax is that of giving the illusion that websites are responsive. It achieves this by processing requests which involve the sending and receiving of small packets of data without refreshing the web browser. Ajax is a technique for creating fast and dynamic web pages. In other words, Ajax allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.

Why Use AJAX?


In traditional JavaScript coding to get any information from a database or a file on the server, or send user information to a server, its important to make an HTML form and GET or POST data to the server. Wait for the server to respond, then a new page will load with the results when clicking Ok button to send the information. Because the server returns a new page each time . With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object With an HTTP request, a web page can make a request to, and get a response from a web server - without reloading the page. The user will stay on the same page, and he will not notice that scripts request pages, or send data to a server in the background. By using the XMLHttpRequest object, a web developer can update a page with data from the server after the page has loaded. TheXMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.

How Ajax Work?

History Of Ajax
During browser innovation, Netscape added a feature known as LiveScript (LiveScript became JavaScript and grew more powerful,), allowed people to put small scripts in web pages so that they could continue to do things after youd downloaded them. Ajax came when the Internet Explorer introduced the concept of IFrame element. Microsoft introduced another technique, called as Microsofts Remote Scripting that technique involved the pulling of data through means of Java Applet which communicated with the client side using scripting language like JavaScript. As soon Microsoft introduced new XMLHttpRequest object that acts as an ActiveX control in the Internet Explorer. Finally, in the year 2006, the W3C (World Wide Web Consortium) announced the release of the first draft that included the specification for the object (XMLHttpRequest) and made it an official web standard. AJAX (first coined in the year 2005) is not a new programming language but a relatively new technique for creating faster, better and dynamic web applications. If you have the basic understanding of HTML, XHTML, XML and JavaScript languages then you can have a go with AJAX AJAX is basically based on the following web standard, XML HTML JavaScript CSS.

Ajax Architecture in ASP. Net

Server Script, A script is a program or set of instructions that is interpreted or carried out by another program rather than by the processor. A CGI script is an example of a serverside script . Microsoft Ajax Library, There are different library and applications: 1. 2. 3. 4. 5. Base Class Library Script Code Library Asynchronous Communications, transfer data asynchronously. Browser Capability Browsers (IE, Firefox etc.)

ASP.Net Ajax Extensions, Ajax Extensions provide controls in build with .Net framework. 1. Ajax Server Control 2. Application 3. Asynchronous Communication

First Program
1. Open new ASP.NET Web Site then click OK.

2. Open Tool Box and drag ScriptManager and UpdatePanel from AJAX Extensions. Then drag label and Button, named UsingAjax. Open properties window of UpdatePanel. Go to Triggers property and click on collection then add the controlID (Button name, which you want to make a AJAX part) and select Event (Click). Added one another button outside the UpdatePannel ,named WithoutAjax and a added another Label.

3. Paste the code in .cs file of the application

protected void with_ajax_Click1(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } protected void without_ajax_Click(object sender, EventArgs e) { Label2.Text = DateTime.Now.ToString(); }

4. Debug the application

Clicking on "Using Ajax Button" ,only first time (label) will be updated without reloading whole page.

Clicking on "Without Ajax Button", whole page Reloaded.

Ajax Controls
You drop the controls to your page (Default.aspx) from the Tab Ajax Extensions.

1. ScriptManager Control Client script for AJAX-enable ASP.NET Web pages managed by ScriptManager control . ScriptManager control registered the client script for the Microsoft Ajax Library with the page. Script manager support features such as 1. Partial-page rendering to the browser 2. Web-service calls. 2. ScriptManagerProxy Control When a ScriptManager control is already defined in a parent element (or master page or host page already contains a ScriptManager control) then used ScriptManagerProxy control to enables nested components and user control to add service and script.

3. Timer control The ASP. NET AJAX Timer control 1. Performed Postbacks of pages at defined intervals. 2. Timer control with an UpdatePanel control, enable partial-page updates at a defined interval. 3. If you want to post the whole page, used Timer Control. 4. The Timer control requires specific settings in a web.config file in order to function correctly. 5. If your application does not contain the required web.config file, errors appear in the Design view of the page where the control would have appeared. 4. UpdatePanel control Which area on web page you want to partial update, used under the UpdatePanel control. Don't wrap your entire page within an UpdatePanel. You may use several panels on your page. UpdatePanel control is the most important control in the ASP.NET AJAX package. It will AJAX controls contained within it, allowed to partial rendering of the area on the web page.

5. UpdateProgress Control

UpdateProgress control provides status information about partial-page updates in UpdatePanel controls. UpdateProgress control provides, to customize the content. When a partial-page update is very fast, you can specify a delay before the UpdateProgress control is displayed.

ScriptManager Control
Client script for AJAX-enable ASP.NET Web pages managed by ScriptManager control . ScriptManager control registered the client script for the Microsoft Ajax Library with the page. Script manager support features such as 1. Partial-page rendering to the browser 2. Web-service calls. Dragging ScriptManager from Ajax extension.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </form> </body> </html>

The ASP. NET controls UpdatePanel, UpdateProgress, and Timer needed a ScriptManager control in order to support partial-page rendering. To view the property window, right click to the ScriptManager control and select Properties. Several property opened.

Some Important Properties of SCripManager Controls are: The EnablePartialRendering property is enabled the partial-page rendering. EnablePartialRendering property is true by default. Hence partial-page rendering is enabled when you add a ScriptManager control to the page. The AsyncPostBackErrorMessage, When an unhandled server exception occurs during postback, gets or sets the error message that is sent to the client. At that time AsyncPostBackError Event occur. The AllowCustomErrorsRedirect Property, Custom errors section of the Web.config file is used during an error in an asynchronous postback, gets or sets a value.s

Timer control
The ASP. NET AJAX Timer control 1. Performed Postbacks of pages at defined intervals. 2. Timer control with an UpdatePanel control, enable partial-page updates at a defined interval. 3. If you want to post the whole page, used Timer Control.

4. The Timer control requires specific settings in a web.config file in order to function correctly. 5. If your application does not contain the required web.config file, errors appear in the Design view of the page where the control would have appeared. How to use Timer Control 1. Drag ScriptManager Control, UpdatePanel and label from toolbox. After this add a timer control.

2. Open the properties of Update panel. Click on collection.

3.Select controlID and give the name of your Timer control and choose the event.

Click OK

4.Paste the code in the Default.aspx.cs file on your Website

using using using using using using using using using using using

System; System.Configuration; System.Data; System.Linq; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.HtmlControls; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Xml.Linq;

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Timer1_Tick1(object sender, EventArgs { } } Label1.Text = System.DateTime.Now.ToString();

e)

5. The design view of your Website

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server"></asp:Label> </ContentTemplate> <Triggers>

<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> </asp:UpdatePanel> <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick1" Interval="60"> </asp:Timer> </form> </body> </html>

6. Debug the Application

UpdatePanel control
Which area on web page you want to partial update, used under the UpdatePanel control. Don't wrap your entire page within an UpdatePanel. You may use several panels on your page. UpdatePanel control is the most important control in the ASP.NET AJAX package. It will AJAX controls contained within it, allowed to partial rendering of the area on the web page. The <asp:UpdatePanel> tag has two childtags: 1. ContentTemplate, The ContentTemplate tag holds the content of the panel. The content are anything that you would normally put on your page, from web controls 2. Triggers tags, The Triggers tag allows you to define certain triggers which will make the panel update there contents. The following example will show the use of both childtags. Go to properties of UpdatePanel control, click on Triggers a new window open. Add controls, which you want to make a part of web page for partial rendering.

UpdateProgress Control
UpdateProgress control provides status information about partial-page updates in UpdatePanel controls. UpdateProgress control provides, to customize the content. When a partial-page update is very fast, you can specify a delay before the UpdateProgress control

is displayed. Example 1.Open a new Website. Add ScriptManager, updatePanel and UpdateProgress in order. Drag a button (named Update) control on your webpage.

2. Go to UpdatePanel Control Properties and add the Update Button to UpdatePanelTrigger Collection.

3. Design Code (Default.aspx)

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>UpdateProgress Control</title> </head> <body> <form id="UpdateProgress" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="update_bt" EventName="Click"> </asp:AsyncPostBackTrigger> </Triggers> </asp:UpdatePanel> <asp:Button ID="update_bt" runat="server" Text="Update" /> </div> <p>&nbsp;</p> <asp:UpdateProgress ID="UpdateProgress1" runat="server"> <ProgressTemplate><img src="face.jpg" width="300" height="300" /></ProgressTemplate> </asp:UpdateProgress> </form> </body>

</html>

<ProgressTemplate> property used to gets or sets the template, which defined the content of the UpdateProgress control. On Update Button Click (.cs Code)

protected void update_bt_Click(object sender, EventArgs e) { System.Threading.Thread.Sleep(5000); } 4. Debug the Application

Note: When Click on Update Button, the image will be appear for 5 sec. You can increase the time to increment the value in sleep method (System.Threading.Thread.Sleep(5000)).

Ajax Control Toolkit Download and Install


1. Download Ajax Control Toolkit Click Here

2. How to Install Ajax Controls in .Net Framework Step 1. Open new Website and right click on Tool Box, choose Add tab option as shown in below Image.

Step 2. Written the name of Your Ajax control tab.

Step 3. Right click on Ajax control tab Click on the Choose Items option, a new window of Choose Toolbox Items be open.

Step 4. Click on Browse.. button , and find Ajax Control Toolkit Folder (Which you downloaded) and select AjaxControlToolkit.dll file. Click on Open button then click OK.

Step 5. Controls are Downloaded. You can see in your Ajax control Tab.