Creating a Calendar Entry Page


The first page you ll create will gather your appointment information: the date, the time, and a description of each appointment. The centerpiece of the page will be a Calendar control that you ll use to select dates. When the page runs, it will look something like Figure 14-1.

click to expand
Figure 14-1: A page for entering appointments, featuring a Calendar control.

Create the CalendarEntry page

  1. In Web Matrix, create a new page, named  CalendarEntry.aspx.

  2. Add this heading to the page: Enter Appointments.

  3. From the Web Control tab of the Toolbox, drag a Calendar control onto the page. If you want to change the appearance of the Calendar control, select the control, click the Auto Format link at the bottom of the Properties window, and select a format (scheme) that you like.

    Tip 

    You can change the look of calendar elements individually as well. In the Properties window, open the nodes for DayStyle, TodayDayStyle, SelectedDayStyle, and so on in other words, any property that ends in Style and set the font, color, or other properties for that style.

  4. Below the calendar, add the following controls to the page and set their properties as indicated:

    Control

    Property Settings

    Label

    ID: labelDate

    Text: [Please select a date]

    Font.Bold: True

    Font.Size: Medium

    DropDownList

    ID: listHours

    DropDownList

    ID: listMinutes

    DropDownList

    ID: listAMPM

    TextBox

    ID: textAppointment

    MaxLength: 50 (or whatever length you set the Event column to in the database)

    RequiredFieldValidator

    ControlToValidate: textAppointment

    ErrorMessage: You must enter an appointment.

    Button

    ID: buttonSave

    Text: Save

    Button

    ID: buttonNewAppointment

    Text: New Appointment

    Label

    ID: labelStatus

    ForeColor: Red

    Text: (empty)

When you defined the Calendar table, you created a single CalendarDateTime column. On the page, however, you have separate controls for entering the date (the Calendar control) and the time (the three drop-down lists). Having separate controls for entering the date and time makes entering those values easier and less error prone than entering them in a text box. In a few moments, I ll explain how you ll reunite the values from these controls to create a single date-time value to send to the database.

  • Add text to the page for captions.

  • You added three drop-down lists, from which users will choose the hour, minutes, and AM/PM designator for their appointments. Let s look quickly at how to populate these lists.

    Populate the time drop-down lists

    1. Select the listHour control, and in the Properties window, click the ellipsis button in the Items property to display the ListItem Collection Editor dialog box.

    2. Click the Add button twelve times to create twelve list items.

    3. In the right-hand properties grid, set the Text property to the values 12, 1, 2, 3, and so on, up to 11. (I put 12 first because it seemed more clock- like to start with 12.) When you ve finished, the dialog box will look like this:

      click to expand

  • Click OK to close the dialog box.

  • Select the listMinutes control, and create four items for it with the text 00, 15, 30, and 45. (I m assuming that fifteen-minute increments are granular enough for our purposes.)

  • Select the listAMPM control, and create two items for it, with the text AM and PM.

  • With the page s layout in place and drop-down lists populated, we can start on the modest amount of programming required for this page. The first task is to get date selection working.

    Programming Date Selection with the Calendar

    In the  CalendarEntry.aspx page, you use the Calendar control to allow users to pick a date. When users select a date, you want to display that date in the labelDate control, which will reinforce for users that they ll be entering an appointment for the selected date. By default, no date is selected in the Calendar control, so you ll also want to preselect a placeholder date namely, today s date.

    Program the selected date

    1. Double-click the Calendar control. Web Matrix switches to Code view and creates a handler for the Calendar control s SelectionChanged event, which is raised whenever users click a day in the calendar.

    2. In the handler, set the text of the labelDate control to the selected date using the following boldfaced code:

      Sub Calendar1_SelectionChanged(sender As Objec t, e As EventArgs     labelDate.Text = Calendar1.SelectedDate End Sub

    The Calendar control s SelectedDate property, as you can see, contains the value of the date that the user has clicked.

  • Create a Page_Load handler, and in the handler, set the Calendar control s SelectedDate property to today s date. You need to initialize the date only the first time the page runs. The code looks like this:

    Sub Page_Load(     If Not Page.IsPostBack The         Calendar1.SelectedDate = Toda         labelDate.Text = Toda     End I End Sub

    Notice that after I ve preselected the date in the calendar, I also display the same date in the labelDate control.

  • Test the page by pressing F5. Make sure that the labelDate control shows the current date. Click on different days in the calendar; as you click, the labelDate control will change to reflect the date you ve clicked. Use the month links at the top of the calendar to go to different months and click dates there.

    As you click different dates, notice the status bar in Microsoft Internet Explorer. You ll see that the browser reloads the page when you click a date in other words, the page makes a round trip every time you click a date.

    Note 

    Clicking a date sets the Calendar control s SelectedDate property. You don t need to explicitly set the date in your code; on the contrary, you can simply read the selected date out of the calendar. The fact that the Calendar control sets the SelectedDate property automatically when you click has some implications that you ll need to accommodate in the second page of the calendar application.

  • Test the drop-down lists to verify that you can set times.

  • Up to now, the code you ve written reads the selected date in the Calendar control. It s time now to add the code to accomplish the basic purpose of the page, which is to add a new calendar record to the database.

    Adding a Calendar Record to the Database

    When users click the Save button, you want to write their appointment information to the database. To accomplish this, you ll use the same basic technique that you used in Chapter 10 to save guestbook entries.

    Add an appointment to the database

    1. Switch to Code view, and from the Toolbox, drag an Insert Data Method element onto the page. Web Matrix prompts you to connect to a database. Connect to the WebMatrix database as usual.

    2. In the Query Builder dialog box, make sure that only CalendarID is checked and then click Next.

    3. Name the method InsertCalendar, and then click Finish.

    4. Switch to Design view, and double-click the Save button to create a Click handler for the button.

    5. In the buttonSave_Click handler, add the following boldfaced code:

      Sub buttonSave_Click(sender As Object, e As Ev entArgs     Dim calDate As String     calDate = labelDate.Text & " "     calDate &= listHours.SelectedItem.Text & ":"     calDate &= listMinutes.SelectedItem.Text & " "     calDate &= listAMPM.SelectedItem.Text     Dim calAppointment As String = _         Server.HtmlEncode(textAppointment.text)     Dim rowsAffected as Integer     Try         rowsAffected = InsertCalendar(calDate, calAppoint ment)         buttonSave.Enabled = False         labelStatus.Text = "Appointment saved!"     Catch ex As Exception        labelStatus.Text = "Unable to save appointment information!"     End Try End Sub

      To perform the database update, you call the InsertCalendar function that you just created before creating the Click handler. You need to pass two values to the function: a date-time string and the appointment text. To create the date-time string, you concatenate the values of the Text property of the labelDate control and the selected text of the drop-down lists, with a colon or space between the elements as appropriate. Notice that you re again using the Server.HtmlEncode method to convert any potentially malicious HTML in the textAppointment control to harmless characters. You don t need to worry about the date or time values, because the user isn t entering those as free-form text. As usual, you enclose the database update in a Try-Catch block in case the update produces an error. As a helpful cue to the user, you re displaying a message, and for good measure, you re disabling the Save button so that users don t inadvertently enter the same appointment twice.

    6. Switch to Design view, and double-click the New Appointment button to create a Click handler for the button. Add the following boldfaced code:

      Sub buttonNewAppointment_Click(sender As Objec t, e As EventArgs     buttonSave.Enabled = True     listHours.SelectedIndex = 0     listMinutes.SelectedIndex = 0     listAMPM.SelectedIndex = 0     textAppointment.Text = ""     labelStatus.Text = "" End Sub 

      This code simply reenables the Save button, reinitializes the values of the drop-down lists, and clears the appointment description. I elected not to clear the selected date, because users might want to enter another appointment for the same day.

    7. Test the page again. Press F5, and when the page appears in the browser, select a date and time, enter an appointment, and click Save. If all goes well, you ll see the text Appointment saved! at the bottom of the page.

    8. Go back to Web Matrix, and in the Data window, double-click the Calendar table. You ll see the new appointment entry in the table.

    You re finished now with the page for entering appointments. The next task is to create a page that will allow you to view your appointments.

    start sidebar
    The Difficulties of Sending Reminders in a Web-Based Calendar

    An extremely useful feature of most scheduling systems is that they can remind you about your appointments. In Outlook, for example, you can set a reminder, and at the appropriate time, Outlook will display a dialog box with a reminder. Alas, our simple Web-based calendar won t include a reminder feature. Adding a reminder feature turns out to be a knotty problem that derives yet again from the way Web pages work. As you know, Web pages run only when a user requests them from the Web server that is, the lifetime of a page is just long enough to process the request and send some HTML to the user s browser. A process for sending reminders, on the other hand, has to run continuously so that it can monitor the clock and compare the time against upcoming appointments. Web pages simply can t be made to run continuously, so you can t create a Web page that will send reminders properly. The correct way to create a reminder system is to write a Microsoft Windows service that runs continuously in the background on your Web server computer. The Microsoft .NET Framework includes facilities for writing a Windows service, but services aren t part of ASP.NET and are in any event beyond the scope of this book.

    end sidebar



    Microsoft ASP. NET Web Matrix Starter Kit
    Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
    ISBN: 0735618569
    EAN: 2147483647
    Year: 2003
    Pages: 169
    Authors: Mike Pope
    BUY ON AMAZON

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