Search the Catalog
Programming ASP.NET

Programming ASP.NET

By Jesse Liberty, Dan Hurwitz
February 2002
0-596-00171-1, Order Number: 1711
960 pages, $49.95 US $74.95 CA £35.50 UK

Chapter 14
Custom and User Controls

Chapter 4 includes a chart of the five types of controls supported in ASP.NET: HTML controls, HTML server controls, web server controls, validation controls, and controls created by the developer. This chapter will discuss this last type of control, known as custom controls, and a subset of them called user controls.

Custom controls are compiled controls that act, from the client's perspective, much like web (ASP) controls. Custom controls can be created in one of three ways:

Of course, all three of these methods, and the three control types that correspond to them, are variations on the same theme. We'll consider these custom controls later in this chapter. The simplest category of custom controls is a subset called user controls. Microsoft distinguishes user controls as a special case because they are quite different from other types of custom controls. In short, user controls are segments of ASP.NET pages that can be reused from within other pages. This is similar to "include files" familiar to ASP developers. However, user controls are far more powerful. User controls support properties and events, and thus provide reusable functionality as well as reusable HTML.

User Controls

User controls allow you to save a part of an existing ASP.NET page and reuse it in many other ASP.NET pages. A user control is almost identical to a normal .aspx page, with two differences: the user control has the .ascx extension rather than .aspx, and it may not have <HTML>, <Body>, or <Form> tags.

The simplest user control is one that displays HTML only. A classic example of a simple user control is an HTML page that displays a copyright notice. Example 14-1 shows the complete listing for copyright.ascx.

Example 14-1: copyright.ascx

<%@ Control %>
<hr>
<table>
   <tr>
      <td align="center">Copyright 2005 Liberty Associates, Inc.</td>
   </tr>
   <tr>
      <td align="center">Support at http://www.LibertyAssociates.com</td>   
   </tr>
</table>

To see this at work, you'll modify Example 8-1, adding just two lines. At the top of the .aspx file, you'll add the registration of your new user control:

<%@Register tagprefix="OReilly" Tagname="copyright" src="copyright.ascx" %> 

This registers the control with the page and establishes both the prefix (OReilly) and the TagName (copyright). In the page you are modifying, there are any number of ASP elements such as <asp:ListItem>. The letters asp before the colon are the tag prefix that identifies this tag as being a web control, and the token ListItem is the TagName.

Your user controls will have a tag prefix as well (in this case, Oreilly), in addition to a specific tag name (in this case, copyright). Interestingly, there is nothing in the .ascx file itself that identifies the tag prefix.

The modified .aspx code is shown in Example 14-2. The output from this page is shown in Figure 14-1.

Figure 14-1. The copyright user control

 

Example 14-2: Modification of Example 8-1

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
AutoEventWireup="false" Inherits="Validation04.WebForm1" %>
 
<%@Register tagprefix="OReilly" Tagname="copyright" src="copyright.ascx" %>
 
<HTML>
  <HEAD>
 
<!-- Demonstrate simple required field validation -->
      <meta name=vs_targetSchema content="Internet Explorer 5.0">
      <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
      <meta name="CODE_LANGUAGE" Content="C#">
  </HEAD>
   <body>
      <h3>
         <font face="Verdana">Bug Report</font>
      </h3>
      <form runat="server" ID="frmBugs">
         <table bgcolor=gainsboro cellpadding=10>
            <tr valign="top">
               <td colspan=3>
                  <!-- Display error messages -->
                  <asp:Label ID="lblMsg" 
                  Text="Please report your bug here" 
                  ForeColor="red" Font-Name="Verdana" 
                  Font-Size="10" runat=server />
                  <br>
               </td>
            </tr>
            <tr>
               <td align=right>
                  <font face=Verdana size=2>Book</font>
               </td>
               <td>
               <!-- Drop down list with the books (must pick one) -->
                  <ASP:DropDownList id=ddlBooks runat=server>
                     <asp:ListItem>-- Please Pick A Book --</asp:ListItem>
                     <asp:ListItem>Programming ASP.NET</asp:ListItem>
                     <asp:ListItem>Programming C#</asp:ListItem>
                     <asp:ListItem>
                        Teach Yourself C++ In 21 Days
                     </asp:ListItem>
                     <asp:ListItem>
                        Teach Yourself C++ In 24 Hours
                     </asp:ListItem>
                     <asp:ListItem>TY C++ In 10 Minutes</asp:ListItem>
                     <asp:ListItem>TY More C++ In 21 Days</asp:ListItem>
                     <asp:ListItem>C++ Unleashed</asp:ListItem>
                     <asp:ListItem>C++ From Scratch</asp:ListItem>
                     <asp:ListItem>XML From Scratch</asp:ListItem>
                     <asp:ListItem>Web Classes FS</asp:ListItem>
                     <asp:ListItem>Beg. OO Analysis & Design</asp:ListItem>
                     <asp:ListItem>Clouds To Code</asp:ListItem>
                     <asp:ListItem>
                        CIG Career Computer Programming
                     </asp:ListItem>
                  </ASP:DropDownList>
               </td>
               <!-- Validator for the drop down -->
               <td align=middle rowspan=1>
                  <asp:RequiredFieldValidator 
                  id="reqFieldBooks" 
                  ControlToValidate="ddlBooks" 
                  Display="Static" 
                  InitialValue="-- Please Pick A Book --" 
                  Width="100%" runat=server>
                     Please choose a book
                  </asp:RequiredFieldValidator>
               </td>
            </tr>
            <tr>
               <td align=right>
               <!-- Radio buttons for the edition -->                  
                  <font face=Verdana size=2>Edition:</font>
               </td>
               <td>
                  <ASP:RadioButtonList id=rblEdition 
                  RepeatLayout="Flow" runat=server>
                     <asp:ListItem>1st</asp:ListItem>
                     <asp:ListItem>2nd</asp:ListItem>
                     <asp:ListItem>3rd</asp:ListItem>
                     <asp:ListItem>4th</asp:ListItem>
                  </ASP:RadioButtonList>
               </td>
               <!-- Validator for editions -->
               <td align=middle rowspan=1>
                  <asp:RequiredFieldValidator 
                  id="reqFieldEdition" 
                  ControlToValidate="rblEdition" 
                  Display="Static" 
                  InitialValue="" 
                  Width="100%" runat=server>
                     Please pick an edition
                  </asp:RequiredFieldValidator>
               </td>
            </tr>
            <tr>
               <td align=right style="HEIGHT: 97px">
                  <font face=Verdana size=2>Bug:</font>
               </td>
               <!-- Multi-line text for the bug entry -->               
               <td style="HEIGHT: 97px">
                  <ASP:TextBox id=txtBug runat=server width="183px" 
                  textmode="MultiLine" height="68px"/>
               </td>
               <!-- Validator for the text box-->               
               <td style="HEIGHT: 97px">
                  <asp:RequiredFieldValidator 
                  id="reqFieldBug" 
                  ControlToValidate="txtBug" 
                  Display="Static" 
                  Width="100%" runat=server>
                     Please provide bug details
                  </asp:RequiredFieldValidator>
               </td>
            </tr>
            <tr>
               <td>
               </td>
               <td>
                  <ASP:Button id=btnSubmit 
                  text="Submit Bug" runat=server />
               </td>
               <td>
               </td>
            </tr>
         </table>
      </form>
<OReilly:copyright runat="server" />
   </body>
</HTML> 

In Figure 14-1 the horizontal rule at the bottom of the page, and the copyright notice below it, comes from the .ascx user control you've created. This control can be reused in many pages. If you update the copyright, you will make that update only in the one .ascx file, and it will be displayed appropriately in all the pages that use that control.

In the next example, you will recreate the book drop-down list itself, this time as a user control. The process of converting part of an existing HTML page into a user control is very simple; you just extract the code that creates the drop-down list into its own HTML file and name that file with the .ascx extension.

Visual Studio .NET provides support for creating user controls. Right-click on the project and choose Add Add New Item. One of the choices is New User Control. This choice opens a new form. The HTML at the top of the form includes the Control directive, which sets the language attributes, etc.

Rename the new item BookList.ascx and copy in the code for creating the book list, as shown in Example 14-3.

Example 14-3: The BookList user control

<!-- Drop down list with the books (must pick one) -->
   <ASP:DropDownList id=ddlBooks runat=server>
      <asp:ListItem>-- Please Pick A Book --</asp:ListItem>
      <asp:ListItem>Programming ASP.NET</asp:ListItem>
      <asp:ListItem>Programming C#</asp:ListItem>
      <asp:ListItem>
         Teach Yourself C++ In 21 Days
      </asp:ListItem>
      <asp:ListItem>
         Teach Yourself C++ In 24 Hours
      </asp:ListItem>
      <asp:ListItem>TY C++ In 10 Minutes</asp:ListItem>
      <asp:ListItem>TY More C++ In 21 Days</asp:ListItem>
      <asp:ListItem>C++ Unleashed</asp:ListItem>
      <asp:ListItem>C++ From Scratch</asp:ListItem>
      <asp:ListItem>XML From Scratch</asp:ListItem>
      <asp:ListItem>Web Classes FS</asp:ListItem>
      <asp:ListItem>Beg. OO Analysis & Design</asp:ListItem>
      <asp:ListItem>Clouds To Code</asp:ListItem>
      <asp:ListItem>
         CIG Career Computer Programming
      </asp:ListItem>
   </ASP:DropDownList>

TIP:   In Example 14-3, you'll strip out the validator code, to keep the focus on working with the user control. The validator can be used with the user control exactly as it was with the ASP control.

To make this work in your page, you'll add a new Register statement:

<%@Register tagprefix="OReilly" Tagname="bookList" src="bookList.ascx" %>

In the body of the page, you'll add the new user control, exactly where you cut the original code:

<OReilly:bookList runat="server" ID="Booklist"/>

Adding Code

So far, all you've put into the user control is straight HTML. This is simple, but also somewhat limited. There is no reason to so severely limit the user control. In the next example, you'll add support for filling the list box with books from a database. To do so, you'll need to add a table to the ProgASPNetBugs database, as shown in Figure 14-2.

Figure 14-2. Design of the Books table

 

You'll also need to populate this table with the values shown in Figure 14-3, or simply download the entire database with data already provided.

Figure 14-3. Contents of the Books table

 

You are ready to fill the list box dynamically, using data binding. Strip out all the code that fills the list box by hand. This reduces Example 14-3 to two lines:

<ASP:DropDownList id=ddlBooks runat=server>
</ASP:DropDownList>

The key to making this work is now in the Control tag:

<%@ Control Language="c#" AutoEventWireup="false" 
Codebehind="BookList.ascx.cs" 
Inherits="UserControl1.WebUserControl1"%>

The Codebehind attribute points to the code-behind page. In that page, you'll add code to the Page_Load method to bind the list box to the appropriate table in the database. That code is shown in Example 14-4 for C# and Example 14-5 for VB.NET.

Example 14-4: C# Page_Load from the code-behind page for the user control

private void Page_Load(object sender, System.EventArgs e)
{
   if (!IsPostBack)
   {
      string connectionString = 
         "server= " + ServerName + 
         "; uid=sa;pwd=" +
         Password + "; database= " + DB;
 
      // get records from the Bugs table
      string commandString = 
         "Select BookName from Books";
 
      // create the data set command object 
      // and the DataSet
      SqlDataAdapter dataAdapter = 
         new SqlDataAdapter(
         commandString, connectionString);
 
      DataSet dataSet = new DataSet(  );
 
      // fill the data set object
      dataAdapter.Fill(dataSet,"Bugs");
 
      // Get the one table from the DataSet
      DataTable dataTable = dataSet.Tables[0];
 
      ddlBooks.DataSource = dataTable.DefaultView;
      ddlBooks.DataTextField = "BookName";
      ddlBooks.DataBind(  );
   }
}

Example 14-5: VB.NET Page_Load from the code-behind page for the user control

Private Sub Page_Load(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles MyBase.Load
   If Not IsPostBack Then
      Dim connectionString As String = _
               "server= " & ServerName & 
               "; uid=sa;pwd=" +
               Password + "; database= " + DB;
 
            ' get records from the Bugs table
            Dim commandString As String = _
               "Select BookName from Books"
 
            ' create the data set command object 
            ' and the DataSet
             Dim dataAdapter as SqlDataAdapter = _
               new SqlDataAdapter( _
               commandString, connectionString);
 
            Dim dataSet As DataSet = New DataSet(  )
 
            ' fill the data set object
            dataAdapter.Fill(dataSet, "Books")
 
            ' Get the one table from the DataSet
            Dim dataTable As DataTable = dataSet.Tables(0)
 
            ddlBooks.DataSource = dataTable.DefaultView
            ddlBooks.DataTextField = "BookName"
            ddlBooks.DataBind(  )
        End If
    End Sub

The host page does not change at all. The updated user control now works as intended, loading the list of books from the database, as shown in Figure 14-4.

@Control Properties

There can be only one @Control directive for each user control. This attribute is used by the ASP.NET page parser and compiler to set attributes for your user control. Possible values are shown in Table 14-1.

Table 14-1: Values for @Control properties

Attribute

Description

Possible values

AutoEventWireup

true (the default) indicates the page automatically posts back to the server. If false, the developer must fire the server event manually.

true or false; default is true.

ClassName

The class name for the page.

Any valid class name.

CompilerOptions

Passed to compiler.

Any valid compiler string indicating options.

Debug

Whether to compile with debug symbols.

true or false; default is false.

Description

Text description of the page.

Any valid text.

EnableViewState

Is view state maintained for the user control?

true or false; default is true.

Explicit

Should page be compiled with VB.NET option explicit

true or false; default is false.

Inherits

Defines a code-behind class.

Any class derived from UserControl.

Language

The language used for inline rendering and server-side script blocks.

Any .NET-supported language.

Strict

Page should be compiled using VB.NET Strict option?

true or false; default is false.

Src

Name of the source file for the code-behind.

Any valid filename.

WarningLevel

Compiler warning level at which compilation will abort.

0-4.

Figure 14-4. Loading the book list from the database in a user control

 

TIP:   The src attribute is not used in Visual Studio .NET. VS.NET uses precompiled code-behind classes with the Inherits attribute.

Adding Properties

You can make your user control far more powerful by adding properties. Properties allow your client (in this case WebForm1) to interact with your control, setting attributes either declaratively (when the user control is added to the form) or programmatically (while the program is running).

You can, for example, give your book list control properties for the server name, the password, and the database to which you will connect. You do this in four steps:

  1. Create a property. You must decide if you will provide a read-write, read-only, or write-only property. For this example, you'll provide read-write properties.
  2. Provide an underlying value for the property. You can do this by computing the property, retrieving it from a database or, as you'll do here, storing the underlying value in a private member variable. You must also decide if you'll provide a default value for your properties.
  3. Integrate the underlying values into the body of the code.
  4. Set the property from the client, either declaratively (as an attribute) or programmatically.

Creating a property

There is nothing special about the property for the user control; you create it as you would any property for a class. In C#, this takes the form:

public string ServerName
{
   get
   {
      return serverName;
   }
   set
   {
      serverName = value;
   }
}
public string Password 
{ 
      get { return password; } set { password = value; } 
} 
 
public string DB 
{ 
   get { return db; } set { db = value; } 
} 

In VB.NET, the code is:

Public Property ServerName As String
   Get
      Return sServerName
   End Get
   Set
      sServerName = Value
   End Set
End Property
 
Public Property Password As String
   Get
      Return sPassword
   End Get
   Set
      sPassword = Value
   End Set
End Property
 
Public Property DB As String 
   Get 
      Return sDB
   End Get
   Set
      sDB = Value
   End Set
End Property

Note that you can take advantage of C#'s case-sensitivity to differentiate the property name (such as ServerName) from the private variable representing the underlying property value (such as serverName). However, because VB.NET is case-insensitive, you must use a property name (such as ServerName) that is clearly distinctive from the private variable holding the underlying property value (such as sServerName).

When coding in C#, we tend to prefer the more extended property declaration style, as shown with ServerName. However, in this book we often use the terser form to save space, as shown for Password and DB.

Providing an underlying value for the property

You certainly can compute the value of a property, or look up the value in a database. In this example, however, you'll simply create member variables to hold the underlying value. In C#, the code is:

private string serverName;
private string password;
private string db = "ProgASPDotNetBugs";

In VB.NET, it's:

Private sServerName, sPassword As String
Private sDB As String = "ProgASPDotNegBugs"

Acting in the role of control designer, you have decided to provide a default value for the db property (the name of the database), but you have not provided a default value for the name of the server or the sa (system administrator) password. This is appropriate; you can safely assume the database is ProgASPDotNetBugs, but you can't possibly know in advance what database server will be used, or what the sa password is.

Integrating the property into your code

Having declared the properties, you must now modify the connection string to use the properties, rather than the hard-coded values. In C#, the code is:

string connectionString = 
   "server= " + serverName + 
   "; uid=sa;pwd=" +
   password + "; database= " + db;

and in VB.NET, it's:

Dim connectionString As String = _
   "server= " & sServerName & _
   "; uid=sa;pwd=" & _
   sPassword & "; database= " & sDB

Here you concatenate hard-coded string values ("server=") with the member variables that will be set through the properties. You could, as an alternative, just use the properties' Get accessors, rather than using the underlying values:

string connectionString = 
   "server= " + ServerName + 
   "; uid=sa;pwd=" +
   Password + "; database= " + DB;

While using the underlying value is trivially more efficient, using the property has the advantage of allowing you to change the implementation of the property without breaking this code.

Setting the property from the client

In the client you must now provide values for the two required attributes, ServerName and Password, and you may provide a value for the DB property. For example, you might write:

<OReilly:bookList runat="server" ID="Booklist"
DB="ProgASPDotNetBugs" Password="yourPassWord"
ServerName="YourServer"

Notice that in the preceding code, you have provided a value for the DB property. This code will continue to work if you leave out this attribute, but adding it makes the code self-documenting.

Handling Events

Event handling with user controls can be a bit confusing. Within a user control (e.g., bookList), you may have other controls (e.g., a list box). If those internal controls fire events, you'll need to handle them within the user control itself. The page the user control is placed in will never see those events.

That said, a user control itself can raise events. You may raise an event in response to events raised by internal controls, in response to user actions or system activity, or for any reason you choose.

Handling events in C#

You declare new events for the user control just as you would for any class. Example 14-6 shows the complete code listing for BookList.ascx.cs.

Example 14-6: BookList.ascx.cs

namespace UserControl2A1
{
   using System;
   using System.Data;
   using System.Data.SqlClient;
   using System.Drawing;
   using System.Web;
   using System.Web.UI.WebControls;
   using System.Web.UI.HtmlControls;
 
   public abstract class BookList : System.Web.UI.UserControl
   {
      protected System.Web.UI.WebControls.DropDownList ddlBooks;
      private string serverName;
      private string password = "oWenmEany";
      private string db = "ProgASPDotNetBugs";
 
      public delegate void 
         ListChangedHandler(object sender, EventArgs e);
      public event ListChangedHandler ListChanged;
 
      protected virtual void OnListChanged(EventArgs e)
      {
         if (ListChanged != null)
            ListChanged(this, e);
      }
 
      public string ServerName
      {
         get
         {
            return serverName;
         }
         set
         {
            serverName = value;
         }
      }
      public string Password 
      { 
         get { return password; } 
         set { password = value; } 
      } 
 
      public string DB 
      { 
         get { return db; } 
         set { db = value; } 
      } 
      
      public BookList(  )
      {
         this.Init += new System.EventHandler(Page_Init);
      }
 
      private void Page_Load(object sender, System.EventArgs e)
      {
         if (!IsPostBack)
         {
            string connectionString = 
               "server= " + ServerName + 
               "; uid=sa;pwd=" +
               Password + "; database= " + DB;
 
            // get records from the Bugs table
            string commandString = 
               "Select BookName from Books";
 
            // create the data set command object 
            // and the DataSet
            SqlDataAdapter dataAdapter = 
               new SqlDataAdapter(
               commandString, connectionString);
 
            DataSet dataSet = new DataSet(  );
 
            // fill the data set object
            dataAdapter.Fill(dataSet,"Bugs");
 
            // Get the one table from the DataSet
            DataTable dataTable = dataSet.Tables[0];
 
            ddlBooks.DataSource = dataTable.DefaultView;
            ddlBooks.DataTextField = "BookName";
            ddlBooks.DataBind(  );
         }
   
      }
 
      private void Page_Init(object sender, EventArgs e)
      {
         InitializeComponent(  );
      }
 
        #region Web Form Designer generated code
      ///        Required method for Designer support - do not modify
      ///        the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent(  )
      {
         this.ddlBooks.SelectedIndexChanged += 
            new System.EventHandler(this.OnSelectedIndexChanged);
         this.Load += new System.EventHandler(this.Page_Load);
 
      }
        #endregion
 
      public class BookListArgs : EventArgs
      {
         public string bookSelected;
      }
 
      private void OnSelectedIndexChanged(
         object sender, System.EventArgs e)
      {
         OnListChanged(e);
      }
   }
}

You start by declaring a delegate that describes the event procedure:

public delegate void ListChangedHandler(object sender, EventArgs e);

You then declare the event itself:

public event ListChangedHandler ListChanged;

You must create a method that begins with the letters "On" followed by the name of the event, as follows:

protected virtual void OnListChanged(EventArgs e)
{
}

This method typically checks that the event has one or more handlers registered, and if so, it raises the event, as the following code shows:

protected virtual void OnListChanged(EventArgs e)
{
   if (ListChanged != null)
      ListChanged(this, e);
}

You are now ready to test the event. For this example, go back to the list box within the book list user control and add an event handler for the selected item being changed:

private void OnSelectedIndexChanged (object sender, System.EventArgs e)
{
   OnListChanged(e);
}

When the item is changed, you call the OnListChanged method, which in turn fires the ListChanged event. More about this shortly.

Your web page can add an event handler for its BookList element. The declaration in the .aspx page is unchanged:

<td><OREILLY:BOOKLIST id=Booklist runat="server" 
    ServerName="yourServer" Password="yourPW" 
    DB="ProgASPDotNetBugs"></OReilly:bookList></TD></TR>

The code-behind changes, however. To register the event, you'll need an instance of a booklist object:

protected UserControl3.BookList Booklist;

You now have only to register the event handler. Within the InitializeComponent method of WebForm1.aspx.cs, add this code:

this.Booklist.ListChanged += 
new UserControl3.BookList.ListChangedHandler(this.Booklist_ListChanged);

The event handler Booklist_ListChanged is thus wired to the ListChanged event of the booklist. When the user chooses a book, the internal list box fires a postback event, the OnSelectedIndexChanged event fires within the .ascx page, and the OnSelectedIndexChanged event handler within the user control responds.

When the ListChanged event is fired, it is caught in the containing page's BookList_ListChanged method, and the label is updated:

public void Booklist_ListChanged(object sender, System.EventArgs e)
{
   lblMsg.Text = "The list changed!!";
}

To the user, it appears just as it should; the list box within the user control appears just to be another control with which the user can interact, as shown in Figure 14-5.

Figure 14-5. The List event fired

 

Handling events in VB.NET

Defining a custom event, trapping the DropDownList control's SelectedIndexChanged event, raising a custom event, and handling it within the .aspx page are very easy and require fewer lines of code in VB.NET than in C#. Within the class definition of the BookList control, you simply declare the event and its signature, as follows:

Public Event ListChanged(ByVal sender As Object, ByVal e As EventArgs)

Since Visual Studio automatically declares the DropDownList control in the code-behind file using the WithEvents keyword, the control's events are automatically trapped by VB.NET, and any event handler, if one is present, is executed. Hence, you simply need to define the following event handler, which raises the custom ListChanged event:

Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs) _
            Handles ddlBooks.SelectedIndexChanged
   RaiseEvent ListChanged(sender, e)
End Sub

That's all the code that's required in the user control's code-behind file. In the ASP.NET application, you have to declare the instance of the Booklist class using the WithEvents keyword:

Protected WithEvents Booklist1 As UserControl1VB.Booklist

The final step is to provide the event handler, as follows:

Private Sub Booklist1_ListChanged(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Booklist1.ListChanged
   lblMsg.Text = "The list changed!!"
End Sub

The complete code-behind page for the Booklist user control is shown in Example 14-7.

Example 14-7: The VB.NET version of the Booklist user control's code-behind file

Imports System.Data.OleDb
 
Public MustInherit Class Booklist
    Inherits System.Web.UI.UserControl
 
      Protected WithEvents ddlBooks As System.Web.UI.WebControls.DropDownList
      Public Event ListChanged(ByVal sender As Object, ByVal e As EventArgs)
 
#Region " Web Form Designer Generated Code "
 
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(  )
 
    End Sub
 
    Private Sub Page_Init(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent(  )
    End Sub
 
#End Region
 
    Private Sub Page_Load(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim connectionString As String = _
               "server= " & ServerName & 
               "; uid=sa;pwd=" &
               Password & "; database= " & DB
 
            ' get records from the Bugs table
            Dim commandString As String = _
               "Select BookName from Books"
 
            ' create the data set command object 
            ' and the DataSet
            Dim dataAdapter as SqlDataAdapter = _
               new SqlDataAdapter( _
               commandString, connectionString);
 
            Dim dataSet As DataSet = New DataSet(  )
 
            ' fill the data set object
            dataAdapter.Fill(dataSet, "Books")
 
            ' Get the one table from the DataSet
            Dim dataTable As DataTable = dataSet.Tables(0)
 
            ddlBooks.DataSource = dataTable.DefaultView
            ddlBooks.DataTextField = "BookName"
            ddlBooks.DataBind(  )
        End If
 
    End Sub
 
   Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object,_
                                     ByVal e As System.EventArgs) _
                                     Handles ddlBooks.SelectedIndexChanged
      RaiseEvent ListChanged(sender, e)
   End Sub
 
End Class

Custom event arguments

It would be even more useful if the control could tell the page what book was chosen. The idiom for doing so is to provide a custom event argument type derived from System.EventArgs. To accomplish this, you'll add a class declaration nested within the Booklist class. In C#, this takes the form:

public class BookListArgs : EventArgs
{
   public string bookSelected;
}

In VB.NET:

Public Class BookListArgs
   Inherits EventArgs
 
   Public bookSelected As String
 
End Class

You can now declare the event to use this new type of Event argument. In C#, you do this by modifying the delegate statement:

public delegate void ListChangedHandler(object sender, BookListArgs e);

In VB.NET, you modify the Event statement:

Public Event ListChanged(ByVal sender As Object, ByVal e As BookListArgs)

In C#, the event handler for the list box change event is now updated to get the selected item's text and add that to the BookListArgs object's bookSelected property:

private void OnSelectedIndexChanged(
   object sender, System.EventArgs e)
{
    BookListArgs bookListArgs = 
       new BookListArgs(  );
    bookListArgs.bookSelected = 
       ddlBooks.SelectedItem.ToString(  );
    OnListChanged(bookListArgs);
}

Remember to update OnListChanged to take the new type of event argument:

protected virtual void OnListChanged(BookListArgs e)
{
   if (ListChanged != null)
      ListChanged(this, e);
}

In VB.NET, you just have to modify the handler for the DropDownList control's SelectedIndexChanged event:

Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object, _
                     ByVal e As System.EventArgs) _
                        Handles ddlBooks.SelectedIndexChanged
 
   Dim bla As New BookListArgs(  )
   bla.bookSelected = ddlBooks.SelectedItem.ToString(  )
   RaiseEvent ListChanged(sender, bla)
 
End Sub

All of the changes noted so far are within the BookList.ascx file. The only change in the page is to the event handler itself. In C#, the code is:

public void Booklist_ListChanged(object sender, UserControl3.BookList.BookListArgs e)
{
   lblMsg.Text = "Selected: " + e.bookSelected;
} 

In VB.NET, it's:

Private Sub Booklist1_ListChanged(ByVal sender As System.Object, _
       ByVal e As UserControl1VB.Booklist.BookListArgs) _
       Handles Booklist1.ListChanged
   lblMsg.Text = "Selected: " & e.bookSelected
End Sub

When you view the web page, it is now able to display the text of the selection, even though the selection event occurs within the user control, as shown in Figure 14-6.

Figure 14-6. Passing text from the list box within the user control to the page

 

Custom Controls

In addition to creating user controls, which are essentially reusable small web pages, you can also create your own compiled custom controls. There are three ways to create custom controls:

Composite controls are most similar to user controls. The key difference is that composite controls are compiled into a DLL and used as you would any server control.

To get started, you'll create a Web Control Library in which you'll create the various custom controls for this chapter. Open Visual Studio .NET and choose New Project. In the New Project Window, select either Visual C# Projects or Visual Basic Projects and create a Web Control Library called CustomControls, as shown in Figure 14-7.

Figure 14-7. Custom control New Project window

 

You'll notice that Visual Studio has created a complete custom control named WebCustomControl1. Before examining this control, create a Web Application to test it. From the File menu choose New Project (Ctrl-Shift-N) and create a project named CustomControlWebPage in the same directory. Be sure to choose the "Add to Solution" radio button, as shown in Figure 14-8.

Figure 14-8. Add custom control web page

 

You'll create a series of custom controls and test them from this application. Right-click on the CustomControls project to bring up the context menu, and choose Properties, as shown in Figure 14-9.

Figure 14-9. Choosing project properties

 

Choose the configuration properties and set the output path to the same directory as the test page, as shown in Figure 14-10.

Figure 14-10. Setting the output path

 

Normally, when you build a custom control you will copy the .DLL file to the \bin directory of the page that will test it. By setting the output to the \bin directory of your test page you will save that step and thus be able to test the control quickly.

The Default (Full) Custom Control

Visual Studio .NET has provided a custom control named WebCustomControl1, as we saw. This is a full custom control, derived from System.Web.UI.WebControls.WebControl. Even before you fully understand how this code works, you can test it in the test page you created. Open WebForm1.aspx and add a statement to register the new control:

<%@Register TagPrefix="OReilly" 
Namespace="CustomControls" 
Assembly="CustomControls" %>

This registers the custom control with the web page, similar to how you registered the user control in Example 14-2. Once again you use the @Register tag and provide a tag prefix (OReilly). Rather than providing a Tagname and src, however, you provide a Namespace and Assembly, which uniquely identify the control and the DLL that the page must use.

You now add the control to the page. The two attributes you must set are the Runat attribute, which is needed for all server-side controls, and the Text attribute, which dictates how the control is displayed at runtime. The tag should appear as follows:

<OReilly:WebCustomControl1 Runat="Server" Text="Hello World!" Id="WC1" />

When you view this page, the text you passed in is displayed, as shown in Figure 14-11.

Figure 14-11. Viewing the default custom control

 

Example 14-8 shows the C# version of the complete custom control provided by Visual Studio .NET, while Example 14-9 shows the VB.NET version.

Example 14-8: VS.NET default custom control (C#)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
 
namespace CustomControls
{
   [DefaultProperty("Text"),
      ToolboxData("<{0}:WebCustomControl1 
           runat=server></{0}:WebCustomControl1>")]
   public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
   {
      private string text;
 
      [Bindable(true),
         Category("Appearance"),
         DefaultValue("")]
      public string Text
      {
         get
         {
            return text;
         }
 
         set
         {
            text = value;
         }
      }
 
      protected override void Render(HtmlTextWriter output)
      {
         output.Write(Text);
      }
   }
}

Example 14-9: VB.NET default custom control

Imports System.ComponentModel
Imports System.Web.UI
 
<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:
WebCustomControl1>")> Public Class WebCustomControl1
   Inherits System.Web.UI.WebControls.WebControl
 
   Dim text As String
 
   <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text](  ) As String
      Get
         Return text
      End Get
 
      Set(ByVal Value As String)
         text = Value
      End Set
   End Property
 
   Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
      output.Write([Text])
   End Sub
 
End Class

This control contains a single property, Text, backed by a private string variable, text.

Note that there are attributes provided both for the property and for the class. These attributes are used by Visual Studio .NET and are not required when creating custom controls. The most common attributes for custom controls are shown in Table 14-2.

Table 14-2: Common attributes for custom controls

Attribute

Description

Bindable

Boolean. true indicates that VS .NET will display this control in the databindings dialog box.

Browsable

Boolean. Is the property displayed in the designer?

Category

Determines in which category this control will be displayed when the Properties dialog is sorted by category.

DefaultValue

The default value.

Description

The text you provide is displayed in the description box in the Properties panel.

Properties

Custom controls can expose properties just as any other class can. You access these properties either programmatically (e.g., in code-behind) or declaratively, by setting attributes of the custom control, as you did in the text page, and as shown here:

<OReilly:WebCustomControl1 Runat="Server" Text="Hello World!" />

The Text property of the control is accessed through the Text attribute in the web page.

In the case of the Text property and the Text attribute, the mapping between the attribute and the underlying property is straightforward because both are strings. ASP.NET will provide intelligent conversion of other types, however. For example, if the underlying type is an integer or a long, the attribute will be converted to the appropriate value type. If the value is an enumeration, ASP.NET matches the string value against the evaluation name and sets the correct enumeration value. If the value is a Boolean, ASP.NET matches the string value against the Boolean value; that is, it will match the string "True" to the Boolean value true.

The Render method

The key method of the custom control is Render. This method is declared in the base class, and must be overridden in your derived class if you wish to take control of rendering to the page. In Examples 14-8 and 14-9, the Render method uses the HtmlTextWriter object passed in as a parameter to write the string held in the Text property.

The HtmlTextWriter class derives from TextWriter and provides rich formatting capabilities. HtmlTextWriter will ensure that the elements produced are well-formed, and it will manage the attributes, including style attributes. Thus, if you want to set the text to red, you can add a color attribute, passing in an enumerated color object that you've translated to HTML, as shown here:

output.AddStyleAttribute("color", ColorTranslator.ToHtml(Color.Red));

You can set the text to be within header (<h2>) tags with the HtmlTextWriter's RenderBeginTag and RenderEndTag methods:

output.RenderBeginTag("h2");
output.Write(Text);
output.RenderEndTag(  );

The result is that when the text is output, the correct tags are created, as shown in Figure 14-12. (The source output that illustrates the HTML rendered by the HtmlTextWriter is circled and highlighted.)

Figure 14-12. The output and its source

 

Maintaining state

In the next example, you'll add a button to increase the size of the text. To accomplish this, you'll eschew the rendering support of the HtmlTextWriter, instead writing the text yourself, using a new Size property (to set the size of the output text). The C# code for the Render method should appear as follows:

protected override void Render(HtmlTextWriter output)
{
   output.Write("<font size = " + Size + ">" + Text + "</font>");
}

While the VB.NET code should appear as:

Protected Overrides Sub Render(ByVal output As _
                               System.Web.UI.HtmlTextWriter)
   output.Write("<font size = " & Size & ">" & [Text] & "</font>")
End Sub

The Size property must maintain its state through the postback fired by pressing the button. This is as simple as writing to and reading from the ViewState collection maintained by the page (see Chapter 6), as shown in the C# property definition of the Size property:

public int Size
{
   get { return Convert.ToInt32((string) ViewState["Size"]);  }
   set { ViewState["Size"] = value.ToString(  ); }
}

In VB.NET, the Size property is defined as follows:

Public Property Size(  ) As Integer
   Get
      Return Convert.ToInt32(ViewState("Size"))
   End Get
   Set(ByVal Value As Integer)
      ViewState("Size") = Value.ToString(  )
   End Set
End Property

The property Get method retrieves the value from ViewState, casts it to a string in the case of C#, and then converts that string to its integer equivalent. The property Set method stashes a string representing the size into ViewState.

To ensure that a valid value is in ViewState to start with, you'll also add a constructor to this control. In C#, the constructor is:

public WebCustomControl1(  )
{
   ViewState["Size"] = "1";
}

In VB.NET, it is:

Public Sub New(  )
   ViewState("Size") = "1"
End Sub

The constructor initializes the value held in ViewState to 1. Each press of the button will update the Size property. To make this work, you'll add a button declaration in the test page:

<asp:Button 
   Runat="server" 
   Text="Increase Size" 
   OnClick="Button1_Click" 
   id="Button1" />

The important changes here are that you've added an ID attribute (Button1) and defined an event handler for the button. You will also need to create an event handler in the code-behind page.

Be sure to add a reference to the CustomControls DLL file to the web page. That will allow Intellisense to see your object, and you'll be able to declare the control in the code-behind page. In C#, this takes the form:

public class WebForm1 : System.Web.UI.Page
{
   protected System.Web.UI.WebControls.Button Button1;
   protected CustomControls.WebCustomControl1 WC1;

In VB.NET, it takes the form:

Public Class WebForm1
   Inherits System.Web.UI.Page
 
   Protected WithEvents Button1 As System.Web.UI.WebControls.Button
   Protected WC1 As VBCustomControls.WebCustomControl1

You can then use that declaration to set the Size property in the event handler in C# for the button click:

public void Button1_Click(object sender, System.EventArgs e)
{
   WC1.Size += 1;
}

The VB.NET code is nearly identical:

Public Sub Button1_Click(ByVal sender As Object, _
                   ByVal e As System.EventArgs) Handles Button1.Click
     WC1.Size += 1
End Sub

Example 14-10 is the complete .aspx page for testing, Example 14-11 is the complete C# code-behind page (with the Visual Studio .NET generated code removed to save space), and Example 14-12 is the complete C# source for the custom control. Example 14-13 is the complete VB.NET code-behind page (again, with the Visual Studio .NET-generated code removed to save space), and Example 14-14 provides the complete VB.NET source for the custom control.

Example 14-10: WebForm1.aspx

<%@ Page language="c#" 
Codebehind="WebForm1.aspx.cs" 
AutoEventWireup="false" 
Inherits="CustomControlWebPage.WebForm1" %>
 
<%@ Register TagPrefix="OReilly" 
Namespace="CustomControls" 
Assembly="CustomControls" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 
<HTML>
  <HEAD>
 
  </HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
      
      <asp:Button Runat="server" 
      Text="Increase Size" 
      OnClick="Button1_Click" 
      id="Button1" />  
      
      <OReilly:WebCustomControl1 
      Runat="Server" 
      Text="Hello World!" 
      id="WC1" />
      
</FORM>      
  </body>
</HTML>

Example 14-11: WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace CustomControlWebPage
{
   public class WebForm1 : System.Web.UI.Page
   {
      protected System.Web.UI.WebControls.Button Button1;
      protected CustomControls.WebCustomControl1 WC1;
   
      public WebForm1(  )
      {
         Page.Init += new System.EventHandler(Page_Init);
      }
 
     // ASP.NET generated code elided from listing
 
      private void InitializeComponent(  )
      {    
         this.Button1.Click += new System.EventHandler(this.Button1_Click);
         this.Load += new System.EventHandler(this.Page_Load);
      }
 
      public void Button1_Click(object sender, System.EventArgs e)
      {
         WC1.Size += 1;
      }
   }
}

Example 14-12: WebCustomControl1.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
 
namespace CustomControls
{
   [DefaultProperty("Text"),
      ToolboxData("<{0}:WebCustomControl1 
      runat=server></{0}:WebCustomControl1>")]
   public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
   {
      private string text;
      
      // constructor initializes the value in ViewState
      public WebCustomControl1(  )
      {
         ViewState["Size"] = "1";
      }
 
      // Created by VS.NET
      [Bindable(true),
         Category("Appearance"),
         DefaultValue("")]
      public string Text
      {
         get {   return text; }
         set{   text = value; }
      }
 
      // Your custom attribute to hold the Size in ViewState
      public int Size
      {
         get { return Convert.ToInt32((string) ViewState["Size"]);  }
         set { ViewState["Size"] = value.ToString(  ); }
      }
 
      // Render method hand renders the size
      protected override void Render(HtmlTextWriter output)
      {
         output.Write("<font size = " + Size + ">" + 
                  Text + "</font>");
      }
   }
}

Example 14-13: WebForm1.aspx.vb

Imports CustomControls.WebCustomControl1
 
Public Class WebForm1
    Inherits System.Web.UI.Page
 
   Protected WithEvents Button1 As System.Web.UI.WebControls.Button
   Protected WC1 As VBCustomControls.WebCustomControl1
 
   Public Sub Button1_Click(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Button1.Click
      WC1.Size += 1
   End Sub
End Class

Example 14-14: WebCustomControl1.vb

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Drawing
 
<DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 _
runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
   Inherits System.Web.UI.WebControls.WebControl
 
   Dim _text As String
 
   Public Sub WebCustomControl1(  )
      ViewState("Size") = "1"
   End Sub
 
    <Bindable(True), Category("Appearance"), DefaultValue("")> _
    Property [Text](  ) As String
        Get
            Return _text
        End Get
 
        Set(ByVal Value As String)
            _text = Value
        End Set
    End Property
 
    Protected Overrides Sub Render( _
                        ByVal output As System.Web.UI.HtmlTextWriter)
      output.Write("<font size = " & Size & ">" & [Text] & "</font>")
    End Sub
 
   Public Property Size(  ) As Integer
      Get
         Return Convert.ToInt32(ViewState("Size"))
      End Get
      Set(ByVal Value As Integer)
         ViewState("Size") = Value.ToString(  )
      End Set
   End Property
 
End Class

To illustrate the effect of clicking the button, in Figure 14-13 I created two instances of the program, and in the second instance I pressed the button three times.

Figure 14-13. Maintaining state

 

Each time the button is clicked, the state variable Size is incremented; when the page is drawn, the state variable is retrieved and used to set the size of the text.

Creating Derived Controls

There are times when it is not necessary to create your own control from scratch. You may simply want to extend the behavior of an existing control type. You can derive from an existing control just as you might derive from any class.

Imagine, for example, that you would like a button to maintain a count of the number of times it has been clicked. Such a button might be useful in any number of applications, but unfortunately the web Button control does not provide this functionality.

To overcome this limitation of the button class, you'll derive a new custom control from System.Web.UI.WebControls.Button, as shown in Example 14-15 (for C#) and Example 14-16 (for VB.NET).

Example 14-15: CountedButton implementation in C#

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
 
namespace CustomControls
{
   // custom control derives from button
   public class CountedButton : System.Web.UI.WebControls.Button
   {
 
      // constructor initializes view state value
      public CountedButton(  )
      {
        this.Text = "Click me";
         ViewState["Count"] = 0;
      }
   
      // count as property maintained in view state
      public int Count 
      {
         get
         {
            return (int) ViewState["Count"];
         }
 
         set
         {
            ViewState["Count"] = value;
         }
      }
 
      // override the OnClick to increment the count,
      // update the button text and then invoke the base method
      protected override void OnClick(EventArgs e)
      {
         ViewState["Count"] =  ((int)ViewState["Count"]) + 1;
         this.Text = ViewState["Count"] + " clicks";
         base.OnClick(e);
      }
   }
}

Example 14-16: CountedButton implementation in VB.NET

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
' custom control derives from button
Public Class CountedButton
   Inherits System.Web.UI.WebControls.Button
 
   ' constructor initializes view state value
   Public Sub New(  )
      Me.Text = "Click me"
      ViewState("Count") = 0
   End Sub
 
   ' count as property maintained in view state
   Public Property Count(  ) As Integer
      Get
         Return CInt(ViewState("Count"))
      End Get
      Set(ByVal Value As Integer)
         ViewState("Count") = Value
      End Set
   End Property
 
   ' override the OnClick to increment the count,
   ' update the button text and then invoke the base method
   Protected Overrides Sub OnClick(ByVal e As EventArgs)
      ViewState("Count") = CInt(ViewState("Count")) + 1
      Me.Text = ViewState("Count") & " clicks"
      MyBase.OnClick(e)
   End Sub
End Class

You begin by deriving your new class from the existing Button type:

public class CountedButton : System.Web.UI.WebControls.Button

The VB.NET equivalent is:

Public Class CountedButton
   Inherits System.Web.UI.WebControls.Button

The work of this class is to maintain its state: how many times the button has been clicked. You provide a public property, Count, which is backed not by a private member variable but rather by a value stored in view state. This is necessary because the button will post the page, and the state would otherwise be lost. The Count property is defined as follows in C#:

public int Count 
{
   get
   {
      return (int) ViewState["Count"];
   }
 
   set
   {
      ViewState["Count"] = value;
   }
}

and it is defined as follows in VB.NET:

Public Property Count(  ) As Integer
   Get
      Return CInt(ViewState("Count"))
   End Get
   Set(ByVal Value As Integer)
      ViewState("Count") = Value
   End Set
End Property

To retrieve the value "Count" from view state, you use the string Count as an offset into the ViewState collection. What is returned is an object that you cast to an int in C# or an Integer in VB.NET.

To ensure that the property will return a valid value, you initialize the Count property in the constructor, where you also set the initial text for the button. The constructor in C# is:

public CountedButton(  )
{
  this.Text = "Click me";
   ViewState["Count"] = 0;
}

and in VB.NET it appears as follows:

Public Sub New(  )
   Me.Text = "Click me"
   ViewState("Count") = 0
End Sub

Because CountedButton derives from Button, it is easy to override the behavior of a Click event. In this case, when the user clicks the button, you will increment the Count value held in view state and update the text on the button to reflect the new count. You will then call the base class' OnClick method to carry on with the normal processing of the Click event. The C# event handler is as follows:

protected override void OnClick(EventArgs e)
{
   ViewState["Count"] =  ((int)ViewState["Count"]) + 1;
   this.Text = ViewState["Count"] + " clicks";
   base.OnClick(e);
}

While the source code for the VB.NET Click event handler is:

Protected Overrides Sub OnClick(ByVal e As EventArgs)
   ViewState("Count") = CInt(ViewState("Count")) + 1
   Me.Text = ViewState("Count") & " clicks"
   MyBase.OnClick(e)
End Sub

You add this control to the .aspx form just as you would your composite control:

<OReilly:CountedButton Runat="Server" id="CB1" />

You do not need to add an additional Register statement because this control, like the custom control, is in the CustomControls namespace and the CustomControls assembly.

When you click the button four times, the button reflects the current count of clicks, as shown in Figure 14-14.

Figure 14-14. Counted button

 

Creating Composite Controls

The third way to create a custom control is to combine two or more existing controls. In the next example, you will act as a contract programmer, and I will act as the client. I'd like you to build a slightly more complex control that I might use to keep track of the number of inquiries I receive about my books.

As your potential client, I might ask you to write a control that lets me put in one or more books, and each time I click on a book the control will keep track of the number of clicks for that book, as shown in Figure 14-15.

Figure 14-15. Composite control

 

The .aspx file for this program is shown in Example 14-17. Its C# and VB versions are identical, except for the @ Page directive.

Example 14-17: The .aspx file for the composite control

<%@ Page language="c#" 
Codebehind="WebForm1.aspx.cs" 
AutoEventWireup="false" 
Inherits="CustomControlWebPage.WebForm1" %>
 
<%@ Register TagPrefix="OReilly" Namespace="CustomControls" Assembly="CustomControls" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
<meta content="Microsoft Visual Studio 7.0" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
<meta content="JavaScript (ECMAScript)" name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
  </HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server">
     
      <OReilly:BookInquiryList
      Runat="Server"
      id="bookInquiry1">
 
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Programming ASP.NET" 
         ID="Bookcounter1"/>
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Programming C#" 
         ID="Bookcounter2" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Teach Yourself C++ 21 Days" 
         ID="BookCounter3" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Teach Yourself C++ 24 Hours" 
         ID="Bookcounter4" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Clouds To Code" 
         ID="Bookcounter5" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="C++ From Scratch" 
         ID="Bookcounter6" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="Web Classes From Scratch" 
         ID="Bookcounter7" />
   
         <OReilly:BookCounter 
         Runat="server" 
         BookName="XML Web Documents From Srcatch" 
         ID="Bookcounter8" />
   
      </OReilly:BookInquiryList>  
 
</FORM>      
  </body>
</HTML>

The key thing to note in this code is that the BookInquiryList component contains a number of BookCounter elements. There is one BookCounter element for each book I wish to track in the control. The control is quite flexible. I can track one, eight (as shown here), or any arbitrary number of books. Each BookCounter element has a BookName attribute that is used to display the name of the book being tracked.

You can see from Figure 14-15 that each book is tracked using a CountedButton custom control, but you do not see a declaration of the CountedButton in the .aspx file. The CountedButton control is entirely encapsulated within the BookCounter custom control.

The entire architecture therefore is as follows:

  1. The BookInquiry composite control derives from WebControl and implements INamingContainer, as described shortly.
  2. The BookInquiry control has a Controls property that it inherits from the Control class (through WebControl) and that returns a collection of child controls.
  3. Within this Controls collection is an arbitrary number of BookCounter controls.
  4. BookCounter is itself a composite control that derives from WebControl and that also implements INamingContainer.
    1. Each instance of BookContainer has two properties, BookName and Count.
    2. The Name property is backed by view state and is initialized through the BookName BookName in the .aspx file
    3. The Count property delegates to a private CountedButton object, which is instantiated in BookContainer.CreateChildControls( ).

The BookInquiry object has only two purposes: it acts as a container for the BookCounter objects, and it is responsible for rendering itself and ensuring that its contained BookCounter objects render themselves on demand.

The best way to see how all this works is to work your way through the code from the inside out. The most contained object is the CountedButton.

Modifying the CountedButton derived control

CountedButton needs only minor modification, as shown in Example 14-18 for C# and Example 14-19 for VB.NET.

Example 14-18: The modified CountedButton.cs file

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
 
namespace CustomControls
{
   // custom control derives from button
   public class CountedButton : System.Web.UI.WebControls.Button
   {
 
      private string displayString;
 
      // default constructor
      public CountedButton(  )
      {
         displayString = "clicks";
         InitValues(  );
      }
 
      // overloaded, takes string to display (e.g., 5 books)
      public CountedButton(string displayString)
      {
         this.displayString = displayString;
         InitValues(  );
      }
 
      // called by constructors
      private void InitValues(  )
      {
         if (ViewState["Count"] == null)
            ViewState["Count"] = 0;
         this.Text = "Click me";
      }
   
      // count as property maintained in view state
      public int Count 
      {
         get
         {
            // initialized in constructor
            // can not be null
            return (int) ViewState["Count"];
         }
 
         set
         {
            ViewState["Count"] = value;
         }
      }
 
      // override the OnClick to increment the count,
      // update the button text and then invoke the base method
      protected override void OnClick(EventArgs e)
      {
         ViewState["Count"] =  ((int)ViewState["Count"]) + 1;
         this.Text = ViewState["Count"] + " " + displayString;
         base.OnClick(e);
      }
   }
}

Example 14-19: The modified CountedButton.vb file

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
' custom control derives from button
Public Class CountedButton
   Inherits System.Web.UI.WebControls.Button
 
   Private displayString As String
 
   ' constructor initializes view state value
   Public Sub New(  )
      displayString = "clicks"
      Init(  )
   End Sub
 
  ' overloaded, takes string to display (e.g., 5 books)
   Public Sub New(ByVal displayString As String)
      Me.displayString = displayString
      Init(  )
   End Sub
 
   ' called by constructors
   Private Shadows Sub Init(  )
      If ViewState("Count") = Is Nothing Then
         ViewState("Count") = 0
         Me.Text = "Click me"
      End If
   End Sub
 
   ' count as property maintained in view state
   Public Property Count(  ) As Integer
      Get
         Return CInt(ViewState("Count"))
      End Get
      Set(ByVal Value As Integer)
         ViewState("Count") = Value
      End Set
   End Property
 
   ' override the OnClick to increment the count,
   ' update the button text and then invoke the base method
   Protected Overrides Sub OnClick(ByVal e As EventArgs)
      ViewState("Count") = CInt(ViewState("Count")) + 1
      Me.Text = CStr(ViewState("Count") & " " & displayString
      MyBase.OnClick(e)
   End Sub
End Class

Because you want the button to be able to display the string 5 Inquiries rather than 5 clicks, you must change the line within the OnClick method that sets the button's text:

this.Text = ViewState["Count"] + " " + displayString;

The VB.NET equivalent is:

Me.Text = ViewState("Count") & " " & displayString

Rather than hard-wiring the string, you'll use a private member variable, displayString, to store a value passed in to the constructor:

private string displayString;

In VB.NET, you'd use:

Private displayString As String

You must set this string in the constructor. To protect client code that already uses the default constructor (with no parameters), you'll overload the constructor, adding a version that takes a string:

public CountedButton(string displayString)
 {
    this.displayString = displayString;
    Init(  );
 }

In VB.NET, the code is:

Public Sub New(ByVal displayString As String)
   Me.displayString = displayString
   Initialize(  )
End Sub

You can now modify the default constructor to set the displayString member variable to a reasonable default value. In C#, the code is:

public CountedButton(  )
{
   displayString = "clicks";
   InitValues(  );
}

In VB.NET, use:

Public Sub New(  )
   displayString = "clicks"
   Init(  )
End Sub

The code common to both constructors has been factored out to the private helper method Init, which ensures that the Count property is initialized to zero and sets the initial text for the button:

private void Init(  )
{
   if (ViewState["Count"] == null)
      ViewState["Count"] = 0;
   this.Text = "Click me";
}

In VB.NET, the same thing is accomplished using:

Private Shadows Sub Init(  )
   If ViewState("Count") = Nothing Then
      ViewState("Count") = 0
      Me.Text = "Click me"
   End If
End Sub

With these changes, the CountedButton is ready to be used in the first composite control, BookCounter.

Creating the BookCounter composite control

The BookCounter composite control is responsible for keeping track of and displaying the number of inquiries about an individual book. Its complete source code is shown in C# in Example 14-20 and in VB.NET in Example 14-21.

Example 14-20: BookCounter.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
 
namespace CustomControls
{
   public class BookCounter : 
      System.Web.UI.WebControls.WebControl, 
      INamingContainer
   {
   
      // intialize the counted button member
      CountedButton btn = new CountedButton("inquiries");
    
      public string BookName 
      {
         get
         {
            return (string) ViewState["BookName"];
         }
 
         set
         {
            ViewState["BookName"] = value;
         }
      }
 
      public int Count
      {
         get
         {
            return btn.Count;
         }
         set
         {
            btn.Count = value;
         }
      }
 
      public void Reset(  )
      {
         btn.Count = 0;
      }
 
      protected override void CreateChildControls(  )
      {
         Controls.Add(btn);
      }
   }
}

Example 14-21: BookCounter.vb

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
 
Public Class BookCounter
   Inherits System.Web.UI.WebControls.WebControl
   Implements INamingContainer
 
   ' intialize the counted button member
   Public btn As CountedButton = New CountedButton("inquiries")
 
   Public Property BookName(  ) As String
      Get
         Return CStr(ViewState("BookName"))
      End Get
      Set(ByVal Value As String)
         ViewState("BookName") = Value
      End Set
   End Property
 
   Public Property Count(  ) As Integer
      Get
         Return btn.Count
      End Get
      Set(ByVal Value As Integer)
         btn.Count = Value
      End Set
   End Property
 
   Public Sub Reset(  )
      btn.Count = 0
   End Sub
 
   Protected Overrides Sub CreateChildControls(  )
      Controls.Add(btn)
   End Sub
 
End Class

INamingContainer

The first thing to note about the BookCounter class is that it implements the INamingContainer interface. This is a "marker" interface that has no methods. The purpose of this interface is to identify a container control that creates a new ID namespace, guaranteeing that all child controls have IDs that are unique to the application.

Containing CountedButton

The BookCounter class contains an instance of CountedButton:

CountedButton btn = new CountedButton("inquiries");

or:

Public btn As CountedButton = New CountedButton("inquiries")

The btn member is instantiated in the CreateChildControls method inherited from System.Control:

protected override void CreateChildControls(  )
{
   Controls.Add(btn);
}

The VB.NET equivalent is:

Protected Overrides Sub CreateChildControls(  )
   Controls.Add(btn)
End Sub

CreateChildControls is called in preparation for rendering and offers the BookCounter class the opportunity to add the btn object as a contained control.

There is no need for BookCounter to override the Render method; the only thing it must render is the CountedButton, which can render itself. The default behavior of Render is to render all the child controls, so you need not do anything special to make this work.

BookCounter also has two properties: BookName and Count. BookName is a string to be displayed in the control and is managed through ViewState. Its C# source code is:

public string BookName 
{
    get
    {
        return (string) ViewState["BookName"];
    }
 
    set
    {
        ViewState["BookName"] = value;
    }
}

Its VB.NET source code is:

Public Property BookName(  ) As String
   Get
      Return CStr(ViewState("BookName"))
   End Get
   Set(ByVal Value As String)
      ViewState("BookName") = Value
   End Set
End Property

Count is the count of inquires about this particular book; responsibility for keeping track of this value is delegated to the CountedButton. In C#, the code is:

public int Count
{
   get
   {
      return btn.Count;
   }
   set
   {
      btn.Count = value;
   }
}

and in VB.NET, it's:

Public Property Count(  ) As Integer
   Get
      Return btn.Count
   End Get
   Set(ByVal Value As Integer)
      btn.Count = Value
   End Set
End Property

There is no need to place the value in ViewState, since the button itself is responsible for its own data.

Creating the BookInquiryList composite control

Each of the BookCounter objects is contained within the Controls collection of the BookInquiryList. This control has no properties or state. Its only method is Render, as shown in C# in Example 14-22 and in VB.NET in Example 14-23.

Example 14-22: BookInquiryList source in C#

[ControlBuilderAttribute(typeof(BookCounterBuilder)),ParseChildren(false)]
public class BookInquiryList : System.Web.UI.WebControls.WebControl, INamingContainer
{
 
   protected override void Render(HtmlTextWriter output)
   {
      int totalInquiries = 0;
      BookCounter current;
 
      // Write the header
      output.Write("<Table border='1' width='90%' cellpadding='1'" +
         "cellspacing='1' align = 'center' >");
      output.Write("<TR><TD colspan = '2' align='center'>");
      output.Write("<B> Inquiries </B></TD></TR>");
 
      // if you have no contained controls, write the default msg.
      if (Controls.Count == 0)
      {
         output.Write("<TR><TD colspan = '2'> align='center'");
         output.Write("<B> No books listed </B></TD></TR>");         
      }
      // otherwise render each of the contained controls
      else
      {
         // iterate over the controls colelction and
         // display the book name for each
         // then tell each contained control to render itself
         for (int i = 0; i < Controls.Count; i++)
         {
            current = (BookCounter) Controls[i];
            totalInquiries += current.Count;
            output.Write("<TR><TD align='left'>" +
               current.BookName + "</TD>");
            output.RenderBeginTag("TD");
            current.RenderControl(output);
            output.RenderEndTag(  );  // end td
            output.Write("</tr>");
         }
         output.Write("<TR><TD colspan='2' align='center'> " +
            " Total Inquiries: " +
            totalInquiries + "</TD></TR>");
      }
      output.Write("</TABLE>");
   }
}

Example 14-23: BookInquiryList source in VB.NET

Imports System.ComponentModel
Imports System.Web.UI
 
 
<ControlBuilder(GetType(BookCounterBuilder)), ParseChildren(False)> _
Public Class BookInquiryList
   Inherits System.Web.UI.WebControls.WebControl
   Implements INamingContainer
 
   Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
 
      Dim totalInquiries As Integer = 0
 
      ' Write the header
      output.Write("<Table border='1' width='90%' cellpadding='1'" & _
         "cellspacing='1' align = 'center' >")
      output.Write("<TR><TD colspan = '2' align='center'>")
      output.Write("<B> Inquiries </B></TD></TR>")
 
      ' if you have no contained controls, write the default msg.
      If Controls.Count = 0 Then
          output.Write("<TR><TD colspan = '2'> align='center'")
          output.Write("<B> No books listed </B></TD></TR>")
          ' otherwise render each of the contained controls
      Else
         ' iterate over the controls colelction and
         ' display the book name for each
         ' then tell each contained control to render itself
         Dim current As BookCounter
 
         For Each current In Controls
            totalInquiries += current.Count
            output.Write("<TR><TD align='left'>" & _
               current.BookName + "</TD>")
            output.RenderBeginTag("TD")
            current.RenderControl(output)
            output.RenderEndTag()            ' end td
            output.Write("</tr>")
         Next
         Dim strTotalInquiries As String
         strTotalInquiries = totalInquiries.ToString
         output.Write("<TR><TD colspan='2' align='center'> " & _
            " Total Inquiries: " & _
            CStr(strTotalInquiries) & "</TD></TR>")
      End If
      output.Write("</TABLE>")
   End Sub
 
End Class
 
Friend Class BookCounterBuilder
   Inherits ControlBuilder
 
   Public Overrides Function GetChildControlType( _
         ByVal tagName As String, ByVal attributes As IDictionary) As Type
      If tagName = "BookCounter" Then
         Dim x As BookCounter
         Return x.GetType
      Else
         Return Nothing
      End If
   End Function
 
   Public Overrides Sub AppendLiteralString(ByVal s As String)
   End Sub
 
End Class
 

ControlBuilder and ParseChildren attributes

The BookCounter class must be associated with the BookInquiryClass so ASP.NET can translate the elements in the .aspx page into the appropriate code. This is accomplished using the ControlBuilder attribute:

[ControlBuilderAttribute(typeof(BookCounterBuilder)),ParseChildren(false)]

The argument to the ControlBuilderAttribute is a Type object that you obtain by passing in BookCounterBuilder, a class you will define to return the type of the BookCounter class given a tag named BookCounter. The code for the BookCounterBuilder is shown in C# in Example 14-24 and in VB.NET in Example 14-25.

Example 14-24: C# version of BookCounterBuilder

internal class BookCounterBuilder : ControlBuilder
{
   public override Type GetChildControlType(
      string tagName, IDictionary attributes)
   {
      if (tagName == "BookCounter")
         return typeof(BookCounter);
      else
         return null;
   }
 
   public override void AppendLiteralString(string s)
   {
   }
}

Example 14-25: VB.NET version of BookCounterBuilder

Friend Class BookCounterBuilder
    Inherits ControlBuilder
 
    Public Overrides Function GetChildControlType(_
        ByVal tagName As String, ByVal attributes As Idictionary) As Type
      If tagName = "BookCounter" Then
        Dim x As BookCounter
        Return x.GetType
      Else
        Return Nothing
      End If
    End Function
 
    Public Overrides Sub AppendLiteralString(ByVal s As String)
    End Sub
 
End Class

ASP.NET will use this BookCounterBuilder, which derives from ControlBuilder, to determine the type of the object indicated by the BookCounter tag. Through this association, each of the BookCounter objects will be instantiated and added to the Controls collection of the BookInquiryClass.

The second attribute, ParseChildren, must be set to false to tell ASP.NET that you have handled the children attributes and no further parsing is required. A value of false indicates that the nested child attributes are not properties of the outer object, but rather are child controls.

Render

The only method of the BookInquiryClass is the override of Render. The purpose of Render is to draw the table shown earlier in Figure 14-15, using the data managed by each of the BookCounter child controls.

The BookInquiryClass provides a count of the total number of inquiries, as shown in Figure 14-16.

Figure 14-16. Total inquiries displayed

 

The code tallies inquiries by initializing an integer variable, totalInquiries, to zero and then iterating over each control in turn, asking the control for its Count property. The statement is the same in C# and VB.NET, except for the closing semicolon in C#:

totalInquiries += current.Count;

The Count property of the control delegates to the CountedButton's count property, as you can see if you step through this code in a debugger, as illustrated in Figure 14-17.

Figure 14-17. Stepping into BookCounter.Count

 

Rendering the output

That same loop renders each of the child controls by iterating over each of the controls. In C#, this is done using:

for (int i = 0; i < Controls.Count; i++)
{
   current = (BookCounter) Controls[i];
   totalInquiries += current.Count;
   output.Write("<TR><TD align='left'>" +
      current.BookName + "</TD>");
   output.RenderBeginTag("TD");
   current.RenderControl(output);
   output.RenderEndTag(  );  // end td
   output.Write("</tr>");
}

In VB.NET, the code is:

For Each current in Controls
   totalInquiries += current.Count
   output.Write("<TR><TD align='left'>" & _
      current.BookName + "</TD>")
   output.RenderBeginTag("TD")
   current.RenderControl(output)
   output.RenderEndTag(  )            ' end td
   output.Write("</tr>")
Next

The local BookCounter object, current, is assigned to each object in the Controls collection in succession:

for (int i = 0; i < Controls.Count; i++)
{
   current = (BookCounter) Controls[i];

With that object, you are able to get the Count, as described previously:

totalInquiries += current.Count;

and then you proceed to render the object. The HtmlTextWriter is used first to create a row and to display the name of the book, using the BookName property of the current BookCounter object:

output.Write("<TR><TD align='left'>" +
   current.BookName + "</TD>");

You then render a TD tag, and within that tag you tell the BookCounter object to render itself. Finally, you render an ending TD tag using RenderEndTag, and an ending row tag using the Write method of the HTMLTextWriter:

output.RenderBeginTag("TD");
 current.RenderControl(output);
 output.RenderEndTag(  );  // end td
 output.Write("</tr>");

When you tell the contained control to render itself:

current.RenderControl(output);

the Render method of BookCounter is called. Since you have not overridden this method, the Render method of the base class is called, which tells each contained object to render itself. The only contained object is CountedButton. Since you have not overridden Render in CountedButton, the base Render method in Button is called, and the button is rendered.

Assignment of Responsibilities

This simple example of a composite control is interesting because the various responsibilities are spread among the participating objects. The BookInquiryList object assumes all responsibility for laying out the control, creating the table, and deciding what will be rendered where. However, it delegates responsibility for rendering the button object to the individual contained controls.

Similarly, the BookInquiryList is responsible for the total number of inquiries--because that information transcends what any individual BookCounter object might know. However, the responsibility for the count held by each BookCounter is delegated to the BookCounter itself. As far as the BookInquiryList is concerned, it gets that information directly from the BookCounter's Count property. It turns out, however, that BookCounter in turn delegates that responsibility to the CountedButton.

Rendering the summary

Once all of the child controls have been rendered, the BookInquiryList creates a new row to display the total inquiries:

output.Write("<TR><TD colspan='2' align='center'> " +
   " Total Inquiries: " + 
   totalInquiries + "</TD></TR>");

Back to: Programming ASP.NET


oreilly.com Home | O'Reilly Bookstores | How to Order | O'Reilly Contacts
International | About O'Reilly | Affiliated Companies | Privacy Policy

© 2001, O'Reilly & Associates, Inc.
webmaster@oreilly.com