Click here to Skip to main content
13,459,040 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 26 Nov 2012

ASP.Net AJAX Begineers Guide

, 26 Nov 2012
Rate this:
Please Sign up or sign in to vote.


Hello readers  this is my first effort to write here   it is focused  to help the beginners who are looking to start working with ASP.Net AJAX  this article will help you to take a overview of how ASP.Net AJAX works  later i will show you working example  .  As its my first effort  i appreciate to point out my mistakes.  This article assume that you already have basic knowledge about web applications and client server programming model .  Last but not least i will post further articles on How to use AJAX with Rest Web services .  

What is AJAX   

AJAX are  client side  UI patterns  . What does it Means ?before that  lets think on simple workflow where client requests  for a resource on server  and server receives the request and response  back to client with resource all this communication is done via HTTP and all page is rendered in HTML .  Now Lets think if you have a information page where user selects a Region and on the basics of selected region list of cities is displayed on dropdownlist   well thats fine it works perfect but here we have one problem for every time when user select a different region then page is posted back to server com-on this increase the network traffic and server load its not a good performance practice .AJAX resolves this problem by rendering a partial page instead of full page post back to server   . So AJAX is nothing its client side  UI pattern and now its standard for developing robust  web applications  .  

How AJAX Work?  

Technically speaking AJAX combines the XMLHTTP components with Java Script library  it works via  XMLHTTP request to achieve the partial rendering to server instead of full page postback. So What happens when user tries to update or get the information it generates the asynchronous call to server and then server respond with updated data no page refresh will happen.  For example you have to update the total products sold on your grid view  which implements the AJAX so it generate a partial asynchronous call to server and server will respond with updated total . I hope its clear .   

 How to Install AJAX With Visual Studio 2008 AND 2010  

I assume that you are starter and you dont know about the AJAX toolkit its basic requirement to use AJAX with you web page .

Installing AJAX Toolkit Visual on Studio 2008 

Go to following URL and download the AJAX toolkit for visual studio 2008  i am not going to explain how to install it because this site explain it very well ): 

Installing AJAX Toolkit Visual on Studio 2010 

For visual studio 2010 please see this site .<o:p />

Code Example :

Well now we are done with installing the tool kit its time to see the practical example .


Lets take the above discussed example of Regions user will select a region and on that selected region second dropdownlist will show the cities so i will use the partial postback approach .

Creating AJAX Enable Web Page Using AJAX Update Pannel and Script Manager  

  1. Add a scriptManager on your markup page
  2. Add a Update Pannel
  3. Add content Template
  4. Inside your contant Template add dropdownlists
  5. Then create a Trigger  which actually register the Regions SelectedIndexChanged Event  its actually a caller which will generate a asynchronous call to server .                                                                        

What is Script Manager ?

 scriptManager  its part of server components it manages the client script for AJAX enable web pages it registers the script for Microsoft AJAX library .

What is Update Panel? 

Well Update panel is most important control it enable the partial page call instead of full page post back Update Panel   has two child tags <contentTemplate>  and< trigger >  content Template is responsible for holding the contents of panel you can put simple test or a control and Trigger tag trigger the call on a particular event so Here you can see markup portion of my code  .

Markup Code Description :

So here i took a ScriptManager  and Update Panel   I am putting my dropdown lists inside the Content Template as i already explained that content template is used to put code and text area here i am putting the dropdownlist  and a small text with cities  then on Triggers Tag  on asp:AsyncTrigger  i registered the Regions selectedItemChanged Event  every time when user will select the region it will trigger partial asynchronous call to server and cities will be update . This is simple beauty of AJAX .  

<div id="DropDownRegions"> 
<asp:ScriptManager ID="Sm" runat="server" />
<asp:UpdatePanel runat="server" >
  <asp:DropDownList ID="drpRegions" runat="server" AutoPostBack="True" 

        Height="37px" onselectedindexchanged="drpRegions_SelectedIndexChanged" 

   <br /> 
    Check Cities <br />
    <asp:DropDownList ID="Cities" runat="server" Height="19px" Width="168px">
 <Triggers >
<asp:AsyncPostBackTrigger ControlID="drpRegions" EventName = "SelectedIndexChanged" />
</asp:UpdatePanel>     <span style="white-space: normal;"> </span>

C# code :

On C# code Behid please do the following steps  

  1. Create a List of Regions using List instead of Getting data from database i am creating a List of Regions which will add regions on Page_Load Event  .
  2. Register a event drpRegions  its SelectedItemChanged Event so whenever dropdownlist selected value will be changed it will raise a event on that event we can check which region is selected and we can take decisions which City list to be added to City dropdownlist . We are done Please see the C# Code below . 

 Now on Code Behind i am populating the Regions with List instead of database table .
// Binding List with Region Dropdownlist at PageLoad Event.
       page_Load {

            if (!IsPostBack)
                List<string> Regions = new List<string>();
                drpRegions.DataSource = Regions;
}                                                                                    Now on Selected Index Change Event it will get the region name and see which region match with cities list .  
// adding city list according to region selected
protected void drpRegions_SelectedIndexChanged(object sender, EventArgs e)
                           List<string> Punjab = new List<string>();
                List<string> sindh = new List<string>(); 
                List<string> Buloschistan = new List<string>();
                List<string> Serhad = new List<string>();
                List<string> Kashmir = new List<string>();
            if(drpRegions.SelectedValue==" South ")
                this.Cities.DataSource = Punjab;
            else if (drpRegions.SelectedValue == " East ")
                this.Cities.DataSource = sindh;
                this.Cities.DataBind();                                                              }
            else if (drpRegions.SelectedValue == " West ")
                this.Cities.DataSource = Buloschistan;
            else if (drpRegions.SelectedValue == " North ")
                this.Cities.DataSource = Serhad;
            else if (drpRegions.SelectedValue == " EastWest ")
                this.Cities.DataSource = Kashmir;


  1. Its Increase the performance . 
  2. Reduce Network Traffic .
  3. Reduce Server Load  
  4. Easy Nevigation

Well when you try to hit the back or refresh button it shows a different behavior )  it could be fixed by good programming practices . 

Who Uses AJAX 

Google search , google maps , Facebook , are very common examples of AJAX. 



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

tahir irshad abbasi
Software Developer (Senior) EIC
Italy Italy
I have 4 years experience in system administration IT technical support later i switched to software development i have 3 years hands on experience in different projects using microsoft technologies ASP.Net AJAX, Webservices , WCF, Windows Forms , windows mobile application development.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Monjurul Habib26-Nov-12 18:57
memberMonjurul Habib26-Nov-12 18:57 
GeneralRe: My vote of 1 Pin
tahir irshad abbasi26-Nov-12 21:32
membertahir irshad abbasi26-Nov-12 21:32 
Question[My vote of 1] Lack of explanation Pin
Dave Cross26-Nov-12 3:28
memberDave Cross26-Nov-12 3:28 
AnswerRe: [My vote of 1] Lack of explanation Pin
tahir irshad abbasi26-Nov-12 3:39
membertahir irshad abbasi26-Nov-12 3:39 
GeneralMy vote of 5 Pin
Christian Amado26-Nov-12 2:58
memberChristian Amado26-Nov-12 2:58 
GeneralRe: My vote of 5 Pin
tahir irshad abbasi26-Nov-12 3:28
membertahir irshad abbasi26-Nov-12 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180323.1 | Last Updated 26 Nov 2012
Article Copyright 2012 by tahir irshad abbasi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid