Create a Data Entry Form

Purpose:

This process will demonstrate how to create a data entry and update form that will work with your tested business object.

Starting Point:

The Region business object should be complete and fully tested.

Steps:

  1. Add a new form to your Web forms project called RegionDataEntry.
  2. Add two text boxes with labels to the form by dragging them from the toolbox and placing them on the designer so that the labels are to the left of the textboxes and aligned with them.  Call the first textbox 'regionIdTextBox' and make its label say "Region ID:"  Call the second textbox 'regionDescriptionTextBox' and have its label say "Name:". 
  3. Add two buttons aligned with each other below the textboxes.  Name the left button 'cancelButton' and the right button 'saveButton'.  Change the text and access key properties appropriately.  (Access keys let you access the control using the alt key and the access key letter you specify.)
  4. Add a label called resultsLabel below textboxes but above the two buttons.  Make the label big enough to display "The record was saved."  Clear the text property after naming the label.
  5. Double-click any blank area of the designer window to open the code window in the Page_Load method. 
  6. We have to initialize the form the first time it is loaded, but we don't want to reinitialize it on PostBacks.  Viewstate will maintain the information in the controls between postbacks, but everything else needs to be reinitialized or made part of the viewstate.  Fortunately, this form will display all of the information that we need so we don't have to do any initialization on postback.  This makes the look of our Page_Load routine pretty easy to see:

    if (!Page.IsPostBack)
    {
        //InitializeForm();
    }
     
  7. Create the InitializeForm routine to load the textboxes on the form.
  8. Create a Save routine to create a Region business object, load the values from the form into the object.

Rationale

This is just the way it works.

Discussion:

The theory is that the work we have done up until now has two advantages.  The first advantage has already shown itself: we have an easy to use methodology that delivers robust, tested business objects in a predictable amount of time.  All of that is rather moot however, if the process fails to simplify the construction of user interface objects.  Let's take a look at what is not here and why it is not here.

First, we have stored procedures handling much of the work of database updates and retrievals.  That saves us from having to write SQL code on the fly, eliminates it from the user interface, and avoids problems like SQL injection attacks.  Second, all of that data table mapping, connection string, and command procedure code created by the data adapter wizard has bee cleaned up, organized, tested, and will not have to be dealt with here.  Third, all of that code in our dataset class is working for us in a way that simplifies rather than bulks up our Web forms.  Fourth, the business object class manages and hides the complexities of the various operations to update the database, often imposing business rules as it does so.  The interface programmer does not need to be concerned with any of those details, but simply needs to know how to get or update information using the business object class.  Finally, we will see there are far more advantages than convenience to the UI programmer; flexibility, reliability, and maintainability are the real reasons Microsoft's Best Practices team and almost every other software engineering organization in the world recommend an architecture that is multi-tiered and object-oriented.

One of the complexities of building Web forms is postbacks.  PostBacks happen every time that you click on a Web button or use a data control that has its autoPostBack property set to true.  PostBacks reload the whole page so you often need to make provisions so that information persists between PostBacks.  We don't need to do anything on this form because all of the information is automatically maintained by the viewstate.

Previous Step: Run NUnit

Next Step: Create an Admin Home Page