Tips and Tricks: ASP.NET AJAX 1.0 and User Controls
page 5 of 10
by Bilal Haidar
Average Rating: 
Views (Total / Last 10 Days): 51555/ 107

UserControl inside an UpdatePanel

In this scenario we will use the original UserControls and place them inside UpdatePanels on the host page. This means that no additional work of adding any UpdatePanel is needed on the UserControls’ side.

We will get the same behavior as in the above scenario where clicking an Employee record in the EmployeeList UserControl will cause an asynchronous postback and a partial update to the OrderList UserControl.

The code inside the host page is as follows:

Listing 7

<%@ Page Language="C#"
 AutoEventWireup="true" StylesheetTheme="Default"
 Inherits="UserControlsInsideUpdatePanels" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                        <bhaidar:employeelist ID="EmployeeList1" runat="server" />
            <br />
                <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                        <bhaidar:orderlist id="OrderList1" runat="server" />
 ControlID="EmployeeList1" EventName="RowSelected" />

What you will notice in the code snippet above is the presence of the Triggers property inside the UpdatePanel. This property tells the UpdatePanel to update its content when the control listed in the Triggers section fires its event. In this context, when the EmployeeList event RowSelected is fired, the second UpdatePanel that contains the OrderList UserControl shall be updated in a partial-update fashion.

We could have also not used the Triggers property and instead caused a refresh to the UpdatePanel programmatically as explained in the above scenario.

The only difference between the two scenarios is that the UserControls are left untouched and the UpdatePanels were added on the host page. This has an advantage over the first scenario especially when it comes to adding AJAX to already existing applications. With this scenario in hand, you do not have to do any extra work on your existing UserControls. You only have to add UpdatePanels into the host page. This leaves the UserControl free and usable in any situation, even where no AJAX is required!

View Entire Article

User Comments

Title: very nice   
Name: Serif Emek
Date: 2009-07-28 5:32:09 PM
That is just what I was looking for.
Title: Re: Vbman   
Name: Bilal Haidar [MVP]
Date: 2008-03-27 10:38:17 AM
Try to show me the code behind of the usercontrol and how you are setting it on the page!

Title: Object reference not set to an instance of an object.   
Name: Vbman
Date: 2008-03-27 10:05:40 AM
Great Article!
I have the update panel on the page and the user controls inside the update panel.
I create a property inside the usercontrol that accesses a control, Say the gridviews Tooltip or Pagesize or ...
When I try to set the property on page declaratively I get an error:
Object reference not set to an instance of an object.

I've seen this problem posted in other places but no solution.
Have you run into this and do you have a work around.

Title: Re:   
Name: Bilal Haidar
Date: 2007-10-30 10:43:12 AM
You can use this converter:

Title: VB.Net   
Name: Jo
Date: 2007-10-30 5:51:05 AM
Article is so good. If it is available in also it will be helpful for the beginners.
Title: Well explained   
Name: Jose
Date: 2007-08-09 4:44:16 AM
As usual, you did a great job!

I loved the fact that you showed all the possible combinations you can have with a gridview user control and AJAX. And the sample works really fine!

Title: Great   
Name: Mark W
Date: 2007-06-25 9:24:07 PM
Very well summarized, thanks. I agree, good use of events and generics.

Also, there's a small typo in Listing 5: OrderList UC's UpdatePanel ID should be 'AjaxPanel'.
Title: Nice one   
Name: Wesley Bakker
Date: 2007-06-24 4:32:51 AM
Nice article. Decent use of events, AJAX, generics, datasourcecontrols etc. etc.


Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-28 10:37:17 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search