ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Whenever you start working on ASP.NET AJAX Page, the first thing you would notice is the Script Manager tag.  The Script Manager tag has to be present in all pages where you try to implement Partial Page Updates.  It can be present only once in a page.  The second thing you would notice is that the <asp:UpdatePanel> is THE Control for getting started or rather ajax enabling your web pages.  It takes care of most of your common needs in implementing Partial Updates.

The UpdatePanel control is a server side control that comes as a part of the Toolbox when you install ASP.NET AJAX.  You can download ASP.NET AJAX for free from http://ajax.asp.net and install for using with your existing Visual Studio 2005 and ASP.NET 2.0 Web Applications.

So here goes a typical ASP.NET AJAX Page.

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
  <div>
  // Page Contents Go Here
  </div>
</form>
 

Now, to understand the working of UpdatePanel, we need to put some labels and a  Button Control which invokes a method in the code behind and retrieves the Current Date and Time

So lets start expanding our Page with those Controls

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:Label ID="Label1" runat="Server"></asp:Label>
            <br />
            <asp:Label ID="Label2" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" Text="Click Me Again" OnClick="Button2_Click"/>
       </div>
</form>

Now in the Code Behind lets define the Page_Load and Button2_Click Methods

 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "The time now is " + System.DateTime.Now.ToString();
       
    }

protected void Button2_Click(object sender, EventArgs e)
    {
        Label2.Text = "The time now is " + System.DateTime.Now.ToString();
    }

When you run this page, you will notice that the Label1 displays the Current Date and Time and when you click ont he button "Click Me Again" the Label 2 also displays the Current Date and Time.

Hang on, also did you notice that the Label1 also changed the Date Time though you didnt intend to do it.  The reason is, when you clicked on the button, it invoked the Page_Load again and subsequently refreshed the page and hence Label1 also got the latest Date Time information.

What is important here is that, the whole page gets refreshed and this would be significantly heavy if the page contains a lot of controls, sections which retrieve data from different sources.  Unfortunately, this has been the way we have been writing web pages where a refresh is inevitable to make a server side request.

Now, let us slighly modify the above ASPX Code by adding the UpdatePanel Control

     <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:Label ID="Label1" runat="Server"></asp:Label>
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="Server">
           
<ContentTemplate>
            <asp:Label ID="Label2" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" Text="Click Me Again" OnClick="Button2_Click"/>
        
    </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
 

We have added Update Panel control and a Content Template which holds the contents which need to implement Partial Refresh.

When we run this page, you will notice that initially when the page loads the Label1 displays the Current Date and Time.

Subsequently, when you click on "Click Me Again" Label2 also appears with the Current Date, but Label1 remains unchanged.

Keep clicking on the "Click Me Again" Button and you will notice that the Label1 Data remains the same whereas the DateTime displayed in Label 2 gets updated.

This is result of the Update Panel which takes care of partially updating the contents of the page without having to reload the whole page.

However, when you do a manual refresh of the page (F5) you will notice that both the Labels get updated with the latest Date Time information.

Any server control that is placed within the Update Panel and Content Template invokes a partial page refresh and only the relevant section gets updated without the whole page getting refreshed.

Though this article was a little elaborate, the idea was to make it in simple words for people who begin with ASP.NET AJAX.

Let us explore Update Progress and other ASP.NET AJAX Controls in the next articles.

Cheers !!! 

posted @ Friday, May 11, 2007 2:48 AM

Print

Comments on this entry:

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Robert Werner at 5/25/2007 6:47 PM
Gravatar
Harish,

Greetings from Vancouver, BC. Do you know if this Ajax Panel can be used to hold a Tree control and have just the Tree control updated when its nodes are altered?

Robert W.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by santhosha k r at 2/21/2008 10:54 PM
Gravatar
Hi Harish Ranganathan,the artcle that you have written is so super... and it helped me lot in understanding the funda behind ajax concept

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Krishna at 3/6/2008 6:09 PM
Gravatar
Excellent article for beginners

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Sus at 4/8/2008 6:40 AM
Gravatar
I am trying to update some controls using ajax. The updating control is a combo box and on selected index change event, i am updating other controls like textboxes and label. But it hangs. I debugged but it goes through the event in code without any error and just hangs on the page. I tried doing it without ajax and i get save changes prompt when i m not even trying to leave the page. any help???

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Brian D. at 4/24/2008 9:09 AM
Gravatar
Anyone know how to get this event to happen without a button and just an onblur event in the textbox?

Thanks.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by harikrishnan at 6/6/2008 1:14 AM
Gravatar
Hi,
Very nice,,am new in ajax.......so that was very helpful
thnks'Hari

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Utsavi Gajjar at 6/27/2008 8:57 PM
Gravatar
Hi
Its a nice article..but i wnated to know that if i want to avoid page refresh in case of calendar how can i do that with ajax..??

If you can guide me regarding that...i ll be really thankful to you..

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Bo at 7/15/2008 3:30 AM
Gravatar
This time I repeated your code exactly in another website and both the labels get updated with the current time...=( any suggestions???

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by umesh daiya at 8/19/2008 5:35 PM
Gravatar
its really to good.its helpful for me. thanks for this.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Vava at 10/16/2008 6:27 AM
Gravatar
Hi,

The informations what you have provided is well and good. so that one person can easily understand the basic things about the Ajax Controls and its advantage.But my problem is something different ie,

I am facing a problem while using Update Panel.Thatz nothing but the tab index is not working and Client Side Script is not running for the contrls what we have placed inside the Update Panel. So that the Page Flickering is still remaining even for a single button click also.

So please look forward my problem and help me out.

Regards,
Vava

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Suvarna at 12/11/2008 3:41 PM
Gravatar
Hi
Nice article.. But trigger is missing

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Suvarna at 12/11/2008 3:42 PM
Gravatar
Hi
Gud article.. But trigger is missing

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Olaf Schreiber at 1/13/2009 4:44 PM
Gravatar
Some say it doesn't work - it does, but you need a lot of entries in the web.config (httpHandlers, httpModules,...).

They are automatically created when you create an ASP.NET AJAX-Enabled WebSite instead of an ordinary one.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by sari at 2/25/2009 12:47 PM
Gravatar
hey
its a very nice article. it helps me in understanding the basic concept of update control very easily..thankss

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Aviinash Pohane at 3/5/2009 3:14 PM
Gravatar
nice article for beginer

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Avinash at 6/18/2009 12:43 AM
Gravatar
thanx harish !!! Really very good article for begineers

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by C#er at 6/22/2009 6:46 PM
Gravatar
Great tutorial, however the problem some of you might be having is that your button is not in the update panel. Try moving the button into that panel and everything should work as described.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Riddhi at 7/8/2009 9:18 AM
Gravatar
Thank you. perfectly explained for beginners!! got an idea of ajax

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by dev at 7/9/2009 5:34 AM
Gravatar
thanks, well explained for beginers

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by sankalp at 7/24/2009 7:00 AM
Gravatar
does update panel also include control for whom we have to send request without sending the entire page eg if i have a drop downlist and image control and the selected value image should be displayed in image control than do we have to include both image and drop downlist or only image as image will be getting refreshed

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by mith at 8/6/2009 1:43 PM
Gravatar
thanx..
really great example..
i think it'l b helpful for my work a lot..
i really appreciate it..

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Peeyush Dwivedi at 8/26/2009 2:21 AM
Gravatar
Thank you for a easy and most helpfull details on Update Panal on Site.

# re: ASP.NET AJAX Update Panel Control, the most useful AJAX Control

Left by Hennie at 9/20/2009 10:32 PM
Gravatar
Hi. Thanks Was looking all over for simple ajax explanation. Really Helpful

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345