Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP.NET 2.0 Instant Results

.pdf
Скачиваний:
67
Добавлен:
17.08.2013
Размер:
11.03 Mб
Скачать

Modifying the Customer Support Site

12.The final step in the Contacts page is to pre-populate the TextBox controls with the user’s details if they exist in the profile. To do that, add the following code to the Page_Load event handler that you added in Step 7:

If Not Page.IsPostBack Then

txtUserName.Text = Profile.Name

txtEmailAddress.Text = Profile.EmailAddress End If

With this Contact page done, users can now send a message to the support department, requesting more information or making a suggestion about a product. To make it easier for returning users to fill in the form, the Contact page saves the user’s details using the new ASP.NET 2.0 Profile feature. This information is then used to fill the text boxes when the Contact page is loaded again on a subsequent visit.

With the Customer Support Site you have seen most of the features required to provide your users with enough information and support. However, there may be circumstances where you need more from a support site. With the current site, it’s relatively easy to create new features such as:

1.Search by serial number. If you have a database with serial numbers for the products you sell, you can link those to downloadable files and frequently asked questions. That way, a user can quickly find the relevant information simply by entering the serial number of their product.

2.Integration with a web shop. In the next chapter you’ll learn how to create a web shop. You can integrate the web shop with the Customer Support Site to enhance the user’s experience. This way, they can browse products, order them, and download support files for those products all from one single web site.

3.Implement an “other users download...” feature. It’s a common feature to show users what other users downloaded on the site. You could add this feature to the Downloads page where you can display a list of links to other downloads related to the one the user is viewing. To implement this, you’d need to keep track of which user downloads what file.

37

9

Modifying the Wrox

WebShop

Although the WebShop already has quite some useful features, it should be easy to come up with a list of possible extensions and enhancements. Features that come to mind are:

Reporting: It would be very useful to have reports about customers, their buying behavior, and sales figures.

Export Capabilities: The finalized orders are now stored in the database. To process them, you either need a reporting solution or a way to export the orders to another system.

Product Reviews: Let your customers tell others what they think about your products.

Extended Maintenance Section: The current maintenance section is pretty limited. A more comprehensive version could allow you to update existing products, manage the product categories and possibly your customer list.

Different Pricing Mechanism: Each product in the WebShop has a fixed price for all customers. You could change the site so that returning users get a discount. You could also charge for shipping costs; either by product, by weight, or by the entire order.

Another area for improvement is the feedback to the user. Right now, after they finalize their order, all they get is a static text showing the order number and a thank you message. It would look much more professional if you could email them a nicely formatted order confirmation message that showed them exactly what they ordered. This walkthrough shows you how to implement this enhancement.

Adding E-mail Capabilities

To add e-mailing capabilities, you’ll need to make changes at four locations. First, you need to add a static text file that serves as the template for the email. Next, you need to add a property to the AppConfiguration class that returns the location of this template file. Then you need to create three shared helper methods to format the contents of the email and to send it. The final step involves modifying code in the Business Layer to send out the mail when the order has been finalized. In the next series of steps, you learn how to accomplish these four tasks.

Chapter 9

1.Start by adding a new regular folder to the root of the project and call it StaticText.

2.Inside this folder, create a new text file and call it ConfirmationMessage.txt.

3.Add the following HTML to this file:

<html>

<head>

<title>Your Order at Wrox WebShop</title> </head>

<body>

Dear customer,<br /><br />

Thank you for your order at Wrox WebShop. Your order number is <b>##OrderNumber##</b><br /><br />

Below you find a list of the products you have ordered. The goods will ship as soon as we receive your payment.<br /><br />

##ShoppingCart## <br /><br /> Thanks<br /><br />

The Wrox WebShop Team </body>

</html>

You can change the contents of the template in any way you want. What’s important to know is that the markers ##OrderNumber## and ##ShoppingCart## are placeholders that are replaced with the actual content when the message is sent.

4.Open the file AppConfiguration.vb from the App_Code folder and add a shared, read-only property called ConfirmationMessageLocation that has the following code:

Public Shared ReadOnly Property ConfirmationMessageLocation() As String Get

Dim tempValue As String = “~/StaticText/ConfirmationMessage.txt” Try

If ConfigurationManager.AppSettings.Get(“ConfirmationMessageLocation”) _ IsNot Nothing Then

tempValue = _

ConfigurationManager.AppSettings.Get(“ConfirmationMessageLocation”)

End If

Catch ex As Exception End Try

Return HttpContext.Current.Server.MapPath(tempValue) End Get

End Property

This code reads the virtual location of the text file from the Web.config file. That value is then translated to a physical path using Server.MapPath. The application setting that is read by this property is added in the next step.

5.Open the Web.config file for the WebShop and add the following highlighted node under

<appSettings>:

<appSettings>

<add key=”MailFromAddress” value=”You@YourProvider.Com”/> <add key=”ConfirmationMessageLocation”

value=”~/StaticText/ConfirmationMessage.txt”/>

</appSettings>

40

Modifying the Wrox Webshop

With the template and code to read its location set up, the following steps show you how to add the code that reads in the message template and constructs the personalized and formatted message. The formatted message will contain the contents of the shopping cart, similar to the cart shown on the web site.

1.Open the file Helpers.vb and add the following helper method:

Public Shared Function CreateGridView() As GridView Dim myGridView As GridView = New GridView myGridView.AutoGenerateColumns = False myGridView.ShowFooter = True

myGridView.Width = Unit.Percentage(100) myGridView.BorderWidth = Unit.Pixel(1)

myGridView.HeaderStyle.BackColor = System.Drawing.Color.FromName(“#ffb49f”) myGridView.HeaderStyle.ForeColor = System.Drawing.Color.White myGridView.HeaderStyle.HorizontalAlign = HorizontalAlign.Left myGridView.HeaderStyle.Font.Name = “Arial”

myGridView.RowStyle.BackColor = System.Drawing.Color.White myGridView.RowStyle.ForeColor = System.Drawing.Color.Black myGridView.RowStyle.Font.Name = “Arial”

myGridView.FooterStyle.Font.Bold = True myGridView.FooterStyle.Font.Name = “Arial”

Dim myGridViewColumn As New BoundField myGridViewColumn.HeaderText = “Title” myGridViewColumn.DataField = “Title” myGridView.Columns.Add(myGridViewColumn)

myGridViewColumn = New BoundField myGridViewColumn.HeaderText = “Quantity” myGridViewColumn.DataField = “Quantity”

myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right myGridView.Columns.Add(myGridViewColumn)

myGridViewColumn = New BoundField() myGridViewColumn.HeaderText = “Price” myGridViewColumn.DataField = “Price” myGridViewColumn.DataFormatString = “{0:c}”

myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right myGridView.Columns.Add(myGridViewColumn)

myGridViewColumn = New BoundField() myGridViewColumn.HeaderText = “Total” myGridViewColumn.DataField = “SubTotal” myGridViewColumn.DataFormatString = “{0:c}”

myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right myGridViewColumn.FooterStyle.HorizontalAlign = HorizontalAlign.Right myGridView.Columns.Add(myGridViewColumn)

Return myGridView

End Function

41

Chapter 9

This code creates a new GridView on the fly and formats it using the available styles such as HeaderStyle, RowStyle, and FooterStyle. It then goes on to add four new columns for the Title, the Quantity, the Price, and the Total of each product in the shopping cart.

2.In the same Helpers.vb file, add a method that accepts a generic WebControl and returns its rendered HTML. Note that this method is a generic method and not tied to the shopping cart or WebShop; you could easily reuse this method to get the HTML from any control:

Public Shared Function GetHtmlFromControl(ByVal theControl As WebControl) As String Dim myStringBuilder As StringBuilder = New StringBuilder()

Dim myStringWriter As System.IO.StringWriter = _ New IO.StringWriter(myStringBuilder)

Dim myHtmlTextWriter As HtmlTextWriter = New HtmlTextWriter(myStringWriter) theControl.RenderControl(myHtmlTextWriter)

Return myStringBuilder.ToString() End Function

3.Still in the Helpers.vb file, add a method that retrieves the template file, gets the HTML from the shopping cart, and then formats and sends the email message:

Public Shared Sub SendConfirmationMessage( _

ByVal theShoppingCart As ShoppingCart, ByVal orderId As Integer, _ ByVal emailAddress As String)

Try

Dim myGridView As GridView = CreateGridView() myGridView.DataSource = theShoppingCart.Items myGridView.DataBind()

If myGridView.Rows.Count > 0 Then myGridView.FooterRow.Cells(0).Text = “Totals:” myGridView.FooterRow.Cells(3).Text = _

String.Format(“{0:c}”, theShoppingCart.Total)

Dim theSubject As String = “Your order at Wrox WebShop”

Dim theMessage As String = My.Computer.FileSystem.ReadAllText _ (AppConfiguration.ConfirmationMessageLocation)

Dim mySmtpClient As New System.Net.Mail.SmtpClient theMessage = theMessage.Replace(“##ShoppingCart##”, _

GetHtmlFromControl(myGridView))

theMessage = theMessage.Replace(“##OrderNumber##”, orderId.ToString()) mySmtpClient.Send(“You@YourProvider.Com”, emailAddress, theSubject,

theMessage) End If

Catch ex As Exception End Try

End Sub

This method uses the CreateGridView method to create a brand new GridView on the fly. It then uses standard databinding to get the data from the shopping cart into the GridView. Besides the order items (from theShoppingCart.Items) it also adds the total for the entire order in the last column. It then reads in the message template using

42

Modifying the Wrox Webshop

the new My class and uses the template to fill the message body. Using Replace, the placeholders are replaced with the actual values. The ShoppingCart placeholder is replaced with the HTML representation of the GridView using the GetHtmlFromControl method you added in step 2. Don’t forget to change the You@YourProvider.Com address to your own email address.

4.The final change you need to make is in the FinalizeOrder method of the ShopManager class. Open the file ShopManager.vb from the BusinessLogic folder, locate the method and add the following line of code:

Public Shared Function FinalizeOrder(ByVal theCustomer As Customer) As Integer Dim orderId As Integer = ShopManagerDB.FinalizeOrder(ShoppingCart, theCustomer)

Helpers.SendConfirmationMessage(ShoppingCart, orderId, Membership.GetUser().Email) ShoppingCart.Clear()

Return orderId End Function

That’s all there is to implementing the confirmation functionality. When you now finalize an order in the Wrox WebShop you get a nicely formatted HTML message similar to Figure 9-1.

Figure 9-1

43

10

Modifying the Appointment

Booking System

The current Appointment Booking System has quite a few features that make it useful already, but it shouldn’t be hard to come up with a list of its shortcomings or enhancement requests. For example, the current application only allows appointments that start at a whole hour and that last one or more whole hours. While this is usually fine for booking objects like conference rooms and laptops, it may not be fine-grained enough for other type of booking objects. You could modify the application so the user can choose an arbitrary starting time, and an end time as well, instead of just indicating the planned duration.

Another enhancement you can make to the site is appointment validation and user feedback. Right now, after an appointment is entered in the system there is no way to see it again, cancel it, or change it. Also, the managers of the system have no option to cancel or move an appointment. It would be a useful addition if you could modify appointments in the system. Whenever an appointment is changed, the end-user could receive an e-mail with the changes and the option to accept or reject the changes.

You could also enhance the mapping between a booking object, the working days, and the hours the booking object is available. Right now, you have an all or nothing solution where you can define the start and end time for all of the working days that the booking object is available. To make this change, you need to move the start and end time columns from the BookingObject table into the junction table BookingObjectWorkingDay. This way, you attach the start and end time on the relation between the booking object and the working day, allowing you to determine the availability on a day-to-day basis.

While the previous suggestion allows you to change the start and end time for each working day, you may also have the need to change the availability for individual days. Right now, a booking object is available on one of the working days, or not. You don’t have the option to create exceptions

— for example, to keep track of the vacation an employee has taken, or for the time the booking object is in maintenance and thus not available. The following walkthrough guides you in the process of creating this exception functionality.

Chapter 10

Design Considerations

Implementing the exceptions functionality requires a few additions and changes to the user interface (in the Management section), the business and data access layers, and the database. Each of these changes is summarized in the following sections. Once you know what to change, the walkthrough will guide you through each of the modifications.

The Database

The database needs a table called Exception that stores the exception date and the ID of the booking object. To add, remove, and list exception dates, you also need three stored procedures.

To take the exceptions into account when checking an appointment, you’ll need to modify the stored procedure sprocAppointmentCheckAvailability and query this new Exception table. The same applies to the stored procedure that gets the time sheet information (sprocTimesheetSelectList).

Business and Data Access Layer

In the BookingObjectManager and BookingObjectManagerDB class you need to create three methods to get, create, and delete exceptions for a booking object. As usual, the methods in the business layer forward their calls to the data access layer, which in turns talks to the database.

User Interface

In the Management section you’ll need to modify the CreateUpdateBookingObject.aspx page so that you can enter and remove exception dates for a specific booking object.

In the next section you’ll see what you need to do exactly to make these changes. You’ll find the complete code for the modification in the folder Chapter 10 - Appointment Booking\Modifications on the CD-ROM or in the code download, so there ‘s no need to type all this code yourself.

Modifying the Database

Follow these steps to modify the database:

1.Open the Database Explorer window in Visual Web Developer (press Ctrl+Alt+s to open it) and create a new table called Exception. The table should look like Figure 10-1.

Figure 10-1

Set the Identity property of the Id column to Yes and make the three columns a combined primary key by selecting all three columns and clicking the key icon on the Database Diagram toolbar.

46