What is Ajax in ASP .Net


AJAX is Asynchronous JavaScript and XML, is the technique that can be used to have communication between the client and server without needing a postback. The benefit of avoiding postback is faster response to the user, the page in the browser is not changed/refreshed/posted so the user can continue to use it while data is sent to the server and user actions like keypresses can also be communicated to the server to provide more meaningful results (example: autosuggest), i.e., enhanced overall usability of the web application.
AJAX is a set of W3C complaint technologies that facilitate this asynchronous communication between the client and server. As an ASP.NET developer, we can include AJAX in our web application using:
  • XMLHTTPRequest object in JavaScript - The W3C standard way of having AJAX functionality.
  • Using jQuery AJAX - jQuery AJAX provides a wrapper using XMLHTTPRequest making the life of developers a little easy.
  • Using Microsoft AJAX library - This is a JavaScript framework that makes working with AJAX easy for ASP.NET developers
  • AJAX Control Toolkit - A set of controls that can be used with ASP.NET to incorporate AJAX functionality.
  • ASP.NET AJAX Server controls - These controls provide the basic building blocks to have AJAX functionality on an ASP.NET page.
So as an ASP.NET developer why should one learn to use AJAX? For the simplest of reason that AJAX will increate the overall usability of the website. Major benefits of incorporating AJAX functionality in a website include:
  • Partial page updates - Only the portion of the page that needs updating is posted back and not the whole page.
  • Faster user response - Since only a subset of data moves between the client and server, the results are shown quickly.
  • Enhanced user interface - With AJAX, desktop like user interface with progress indicators and timers can be implemented.
  • Enhanced features - with AJAX features like autocomplete can be implemented.
This article mainly focuses on ASP.NET AJAX Server controls. Other techniques for implementing AJAX deserves a separate article each (which I will post in due time) but for now, our focus will be ASP.NET AJAX server controls.

Ajax Server Controls:

ASP.NET AJAX server controls mainly provide functionality for having partial page updates, update progress indication, and frequent updates based on a timer. Also, it takes care of generating all the JavaScript that is required to perform these functionalities. So with these controls, the developer doesn't have to write any JavaScript to implement AJAX.
The controls provided by ASP.NET for having AJAX functionality are:
  1. ScriptManager
  2. ScriptManagerProxy
  3. UpdatePanel
  4. UpdateProgress
  5. Timer
Let us try to understand these controls one by one and try to work out an example to see how each of them can be used to implement AJAX features.

ScriptManager

The ScriptManager control is a non visual component on the page. This control is required on each page that needs to have AJAX features implemented on it. The main functionality of a ScriptManager control is to push Microsoft AJAX framework code to the client side when the page is being rendered. This control can be thought of as the agent which will write the JavaScript required on the client side to facilitate AJAX functionality.
There should be only one ScriptManager control on the page that needs AJAX functionality. Let us create a webpage and add a ScriptManager control to it:

<asp:ScriptManager ID="ScriptManager1" runat="server"/>

ScriptManagerProxy

We have seen that the ScriptManager control is required on the page that needs AJAX functionality. We also know that there should be only one ScriptManager control on the page. Now consider a situation where there is a master page and content page and both need AJAX functionalities. There is one more scenario, let's say we have a UserControl that needs AJAX and it has to be added on a page where AJAX is already implemented. Since there could be only one ScriptManager on the page, adding a ScriptManager control in these scenarios will result in two ScriptManager controls on the page. So to handle such conditions, theScriptManagerProxy control can be used.
ScriptManagerProxy should be used on content pages that have master pages containing a ScriptManagercontrol. It can also be used inside UserControls when the page containing the UserControl already has theScriptManager control.

UpdatePanel

This is a container control that contains other ASP.NET controls. This control defines a region that is capable of making partial page updates. We can add various server controls inside an UpdatePanel and this controls inside the UpdatePanel will communicate to the server irrespective of the page's postback.

UpdateProgress

The scenario we just handled gave us the results instantly, but imagine a scenario where the server side processing for the asynchronous event takes some time. If the operation is time consuming then we can provide the user feedback by using the UpdateProgress control inside the UpdatePanel.

Timer

There might be some scenarios where we want to update a particular portion of the page after some time duration irrespective of user action. To achieve this, we can use the Timer control. Let us add a timer control to our page and display the server time after every 5 seconds

What is Ajax in ASP .Net What is Ajax in ASP .Net Reviewed by Girraj Prasad Raman on 00:47 Rating: 5
Powered by Blogger.