Web Controls Past and Present
By Andrew Flick and Anthony Lombardo
The temperatures are soaring and summer vacations are in full swing. This month s summer heat is bringing us back to review an older topic with a new flare. Although creating user controls for the Web has been a concept that s been around for some time, the point of this month s tip is to review user controls, how to load them dynamically, and how the modern Web affects their use.
User controls can be used in any scenario where loaded content needs to be componentized. The concept is popular in the development of portal-type sites that have reusable parts and pieces that can be loaded on demand. Of course, in today s world, the concept of smart parts has a more popular approach to this concept.
A user control is essentially a piece of ASP.NET code that can be inserted directly into ASP.NET pages. The design process of creating a user control is simply to add all the functionality to what looks like a page in Visual Studio, except typically ends with the .ascx file extension and lacks some of the HTML of a full Web page. An empty user control looks something like this in code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" Inherits="MyControl" %>
The designer interaction inside Visual Studio to consume user controls is equally as simple. You simply drag the user control onto the form and, just like that, your reusable piece is put to good use. Essentially, what this process does is to include the following line of code in your page s HTML:
<%@ Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>
This will get you on the way to developing componentized Web sites; however, you may actually decide that you want to load different user controls based on requests or actions from your end user. To accomplish this you would use the following simple C# or VB code to load the appropriate user control at a given time:
Control MyDynamicUserControl = LoadControl("MyControl.ascx");
Control MyDynamicUserControl = LoadControl("MyControl.ascx")
This method takes a simple virtual path of the user control and loads the control, but doesn t actually display it. To display it, you need to add it to the form. To accomplish this, simply use the following line of code:
To gain access to positioning the control, add a placeholder or panel to the page that defines the location where you want to add the control. After you add one of these placeholder controls, it s fairly simple to replace it with the user control that you want to dynamically load. The following code will accomplish just that:
You ll notice if you use a button click to add this control to the form that, after a postback, the control will appear you ve successfully positioned and dynamically loaded a control.
Now, the thing is, in today s day and age, that postback you saw is not acceptable (or so every AJAX fanatic would have you believe). What is the solution, then, if you have a scenario in which you want to load this user control dynamically, but also want to do it asynchronously?
Next, add a trigger to your update panel that will monitor either a property change or some event. This hooks up the proper code to load data asynchronously. Your update panel may look something like this:
<atlas:UpdatePanel ID="MyUpdatePanel" runat="server">
<atlas:ControlEventTrigger ControlID="btnSubmit" EventName="Click" />
That s it; now you can asynchronously and dynamically load user controls to your Web pages.
This opens up many possibilities for keeping end users happy, but also for bringing up relevant information as it makes sense to the end user. Enjoy this simple trick and keep cool in these hot months!
Andrew Flick is Product Manager - NetAdvantage Windows Forms Technologies & TestAdvantage for Infragistics, Inc. Prior to joining Infragistics, Andrew played an avid role in presenting on emerging .NET technologies throughout the Midwest to a variety of User Groups as well as numerous Student Groups. As an active member of the INETA Academic Committee, Andrew has authored content on building successful User Groups, as well as numerous white papers on building an effective community. A Microsoft MVP, Andrew joined Infragistics in July of 2004 as a Technology Evangelist, where he was responsible for the creation of reference applications and authoring .NET technology articles for industry leading publications, as well as the world wide delivery of Infragistics Technology demonstrations. Andrew currently serves as Infragistics Product Manager for NetAdvantage Windows Forms Technologies & TestAdvantage. As product manager, he spearheads the product management and strategies of Infragistics Windows Forms Product Lines from establishing the direction through delivery. Working directly with the Director of Development, he sets the direction, plans, and manages product development. Contact Andrew at mailto:[email protected].
Anthony Lombardo is Product Manager of NetAdvantage - ASP.NET Technologies for Infragistics, Inc., and is responsible for spearheading product management and strategies for Infragistics ASP.NET product line, working directly with the Director of Engineering to set the direction, plan, and manage product development. Prior to joining Infragistics, Anthony served as a Network Administrator for North Brunswick Board of Education and worked for Rutgers University in their technical support division. Since beginning his career with Infragistics in 2000, Anthony has been involved with every aspect of the development cycle, including playing a key role in the creation of the Presentation Layer Framework for ASP.NET, assisting in the creation and implementation of Infragistics Visual Studio 2005 project plan, and determining the product feature set for NetAdvantage 2005 for Visual Studio 2005. Contact Anthony at mailto:[email protected].