- Initializing User Controls
- Building User Controls
- Building Web Forms with User Controls
- Summary
Building Web Forms with User Controls
With user controls, you can build web pages quickly, leaving you time to concentrate on other content that's unique to a specific page or that changes dynamically over time. This example demonstrates how to create a web page with user controls.
The first page we'll work with is the home page:
There's probably a web page in your project named WebForm1.aspx, which is the default name assigned when you run the wizard. Change the filename to Default.aspx and open the file in the designer.
Next, you'll need to do some initial layout of the page. Select the Document element from the drop-down list in the Object Inspector and change the PageLayout property from GridLayout to FlowLayout.
Add a table to Default.aspx with one row and two columns. Set the width to 100% and change the border width to 0. After creating the table, click the left cell of the new table, change its width property to 125, and change its valign property to "top" in the Object Inspector. Now the layout and structure of the page is set.
After initial layout, you can add controls to the web page. One thing that's a real bother with C#Builder is that you can't do drag-and-drop with user controls. Once the control is on the page, you can select it and delete it, but the fact that Borland left out such an obvious necessity on a GUI designer is difficult to believe. Anyway, a workaround is available, and fortunately, C#Builder has a nice HTML editor to work with. Switch to the HTML page of Default.aspx to continue designing with user controls.
When you put a user control on a web page, you need to tell ASP.NET how to identify and associate a page element that represents the control with the control itself. To accomplish this, another directive called Register is placed on each web page where the control is added. Each web page in this example needs three Register directives, as shown in Listing 2.
Listing 2 Register Directives
<%@ Register TagPrefix="InformIT" TagName="Header" Src="HeaderControl.ascx" %> <%@ Register TagPrefix="InformIT" TagName="Footer" Src="FooterControl.ascx" %> <%@ Register TagPrefix="InformIT" TagName="Menu" Src="MenuControl.ascx" %>
Each Register directive in Listing 2 contains the same three attributes:
The TagPrefix attribute identifies the namespace (think XML) to which elements of this control are assigned. This is similar to the asp: prefix on ASP.NET web controls that ship with .NET, which keeps the names of controls in different libraries from clashing. For instance, I could add a control to my library named Button; it wouldn't conflict with asp:Button because it would be referred to on the page as InformIT:Button as defined by a Register directive.
The TagName attribute identifies the name of the control as it will be used on the web page. You can essentially give the TagPrefix and TagName attributes any value you want, as long as they didn't conflict with the naming conventions of other controls on the web page.
The Src attribute specifies the filename of a user control and is the mechanism that associates the actual control with how it's identified on the web page.
Once Register directives have been added, you can insert elements in the HTML where you want each control to appear. Here's the syntax for coding a user control:
<InformIT:Header runat="server" />
To save yourself some typing, you can specify the user control as shown in Listing 3. However, as soon as you click the Save button or move to the Design window, C#Builder transforms your HTML into the format shown below:
<informit:header id=userControl1 runat="server"> </informit:header>
Of course, the code above still works, or else C#Builder wouldn't have changed it. Feel free to change the value of the id attribute to something more meaningful for this controlespecially if you want to access the control from code. If you do reference this control from code, be sure to add a variable of the proper type with an identifier that matches the id attribute. Controls are objects, just like any other object, and you can add any event, method, or property you need to make the control work the way you want. Public fields and properties can be set as attributes in HTML or via entries in the Object Inspector. For example, the declaration below references the HeaderControl control shown above:
HeaderControl userControl1;
The type here is the same type declared in the code-behind of the HeaderControl user control. Notice that the identifier userControl1 matches the id attribute of the InformIT:Header control element in the HTML of Default.aspx. The identifier is not very intuitive and should be changed in both the HTML and code-behind of Default.aspx. Because program access (events, methods, and properties) to user controls is the subject of another article, I'll leave that part of the discussion and move forward with showing you how to add user controls to a web page.
Now that you know how to add a control, the next step is to add each of the user controls to the web page. HeaderControl goes just before the table, FooterControl goes at the bottom of the table, and MenuControl goes in the first column. Listing 3 contains the HTML, showing where each of the controls goes.
Listing 3 Web Page HTML with User Controls Added
<%@ Page language="c#" Debug="true" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="CraftingUserControls.WebForm1" %> <%@ Register TagPrefix="InformIT" TagName="Header" Src="HeaderControl.ascx" %> <%@ Register TagPrefix="InformIT" TagName="Footer" Src="FooterControl.ascx" %> <%@ Register TagPrefix="InformIT" TagName="Menu" Src="MenuControl.ascx" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <meta name="GENERATOR" content="Borland ASP.NET Designer for c# Package Library 7.1"> </head> <body ms_positioning="FlowLayout"> <form runat="server"> <informit:header id=userControl1 runat="server"> </informit:header> <table cellspacing=0 cellpadding=1 width="100%" border=0> <tbody> <tr> <td valign=top width=125> <informit:menu id=userControl2 runat="server"> </informit:menu> </td> <td> </td> </tr> </tbody> </table> <informit:footer id=userControl3 runat="server"> </informit:footer> </form> </body> </html>
Each control added to the HTML appears in bold in Listing 3. Each control element will be replaced with the contents of the control when the page is rendered. The only step left for this page is content, which is easier to add via the visual designer. Figure 6 shows how this page looks in the visual designer with content added.
Figure 6 The Visual Designer displays user controls as gray boxes with identifiers. You can't see how the page looks, but you can see where the controls are located.
The visual designer in Figure 6 shows what a Web page looks like with controls and content added. The controls are gray boxes and simply help show where they should reside, giving you an idea of where they will appear when the page is rendered. When launched, the web page will actually look like Figure 7.
Figure 7 When the page runs, all controls will be rendered as part of the original web page. The page is responsive and the user experience hasn't changed at all.
As an exercise, add a new web form to this project and name it About.aspx. Add the tables and controls as described for Default.aspx. Add any content that you think an About page should have. When running the application, you can switch between pages via the menu, and both pages have the same header, menu, and footer elements.
To see the real value of user controls, change the contents on one of the controls; for example, add a copyright notice to the footer. Notice that the change appears on both pages. When considering the impact of that one change across a large site with hundreds of pages, you can easily see the huge boost in productivity that user controls deliver.