Working with Class Inheritance


Imagine an application in which you have to fill out an entry form. For example, suppose you are creating a work order application that records maintenance requests for a building. When something in the building needs to be fixed, a user would create a work order and enter the building code the problem is in along with the department that will be responsible for handling the problem. The application can also have other fields, such as priority for the work order, estimated cost, a description of the problem, etc.

Let's also say that we want to make the application flexible enough so that if the user keys in a building code and discovers that the building doesn't exist, the user may enter a new building code on the fly. This can be done by displaying what I call a "quick entry" form (a smaller second browser window) that asks for the new building code.

Now, instead of writing the code for each button it would be better to write the code in one class, then use the class as a base class for each "quick entry" button. In addition, it would be nice to write a generic form that encapsulated the basic functionality of all entry forms and use it as a base to every entry form in the project.

The sample application shows how to implement both cases of inheritance. You will first write a simple class that will encapsulate the functionality of the Page_Load event for all the forms in your project. Then, you will write a generic class that serves as a button. When the button is clicked the program will display a lookup form that lets you enter a new building or department code.

To create a test project for this chapter:

  1. Launch Visual Studio .NET. (Start > All Programs > Microsoft Visual Studio .NET > Microsoft Visual Studio .NET).

  2. Select File > New > Project to bring up the New Project dialog.

  3. Under project types on the left side of the New Project window click the Visual C# projects folder.

  4. Select the ASP.NET Web Application icon and change the name of the application to inheritanceproject ( Figure 5.1 ).

    Figure 5.1. The sample application for this chapter will show you how to use inheritance to encapsulate the code of Page_Load for every form in the project, and how to use inheritance to create a custom button control.

    graphics/05fig01.gif

  5. Visual Studio will create a new project and open WebForm1.aspx.

  6. Change the form's name to WorkOrder .aspx. Do so by choosing View > Solution Explorer from the top menu bar.

  7. Right-click on WebForm1.aspx and choose properties. In the property grid below change the FileName property from WebForm1.aspx to WorkOrder.aspx ( Figure 5.2 ).

    Figure 5.2. Change the name of the main form to WorkOrder.aspx.

    graphics/05fig02.gif

  8. Create a form that looks like the form in Figure 5.3 . This may be a lot of work to do by hand, so you may want to enter the HTML directly into the editor. Figure 5.4 (on next page) shows the HTML necessary to create the form. To enter the HTML directly click the HTML button under the editor's window. As an alternative you could download the skeleton file for this project (see Tip on next page).

    Figure 5.3. The WorkOrder form has two drop-down lists: one for building and one for department. Initially the drop-down lists don't have any values; to enter the values you click the New button to display a quick entry form that enables you to add items to the drop-down list.

    graphics/05fig03.gif

    Figure 5.4 The form is composed of two label controls, two drop-down lists, and two buttons .
     <%@ Page language="c#"      Codebehind="WorkOrder.aspx.cs"      AutoEventWireup="false"      Inherits="inheritanceproject.WorkOrder" %> <HTML>    <HEAD>       <title>WorkOrder</title>    </HEAD>    <body MS_POSITIONING="GridLayout">    <form id="WorkOrder" method="post"              runat="server">    <  asp:label  id="lblBldg"               style="Z-INDEX: 101; LEFT:               22px; POSITION: absolute;               TOP: 23px" runat="server"               Height="22px" Width="61px">  Building:  </asp:label>     <  asp:label  id="lblDepartment"               style="Z-INDEX: 106; LEFT:               23px; POSITION: absolute;               TOP: 55px" runat="server"               Height="22px" Width="61px">  Department:  </asp:label>     <  asp:dropdownlist  id="lstBuilding"               style="Z-INDEX: 102; LEFT:               113px; POSITION: absolute;               TOP: 23px" runat="server"               Height="26px"               Width="176px">               </asp:dropdownlist>     <  asp:dropdownlist  id="lstDepartment"               style="Z-INDEX: 103; LEFT:               113px; POSITION: absolute;               TOP: 55px" runat="server"               Height="26px"               Width="176px">               </asp:dropdownlist>     <  asp:button  id="btnNewBldg"               style="Z-INDEX: 104; LEFT:               299px; POSITION: absolute;               TOP: 23px" runat="server"               Height="21px" Width="61px"               Control="lstBuilding"               Text="  New...  ">               </asp:button>     <  asp:button  id="btnNewDept" style="Z-               INDEX: 105; LEFT: 299px;               POSITION: absolute; TOP:               56px" runat="server"               Height="21px" Width="61px"               Text="New...">               </asp:button>    </form>    </body> </HTML> 

graphics/tick.gif Tip

  • A skeleton for this project can be downloaded from Peachpit's Web site, http://www.peachpit.com/vqs/csharp.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net