This is the eighth part of the solution for Papa Bob's Mega Challenge. In this part, we're going to create functionality so that the order's total cost will continually be updated on the web page, and redirect the user to a success page upon creating an order.
What we need to do first is create a method that will recalculate the total cost of the order. The end goal is that this method will be called every time the user changes a selection that will affect the price.
Navigate to Default.aspx.cs and create a new recalculateTotalCost() method:
You may have noticed that this method signature is different than most that we've created, but still familiar. This is because it follows the same signature of the event handlers, such as _Click:
What we're actually creating here is a custom event handler that we'll reference in our code! This will fire off each time the selection is changed, as you'll see implemented soon.
This recalculateTotalCost() method will need to call the Domain layer's CalculateCost() method to get the price of the order. However, remember that CalculateCost() requires an OrderDTO object. Currently, our order isn't created until the orderButton is clicked.
To remedy this problem, we can create a new helper method called buildOrder() that will create the order apart from the click event:
Cut the following code from the orderButton_Click and paste it into buildOrder():
Finally, call the buildOrder() method inside the Click event to create the order when the user clicks the button:
Return now to the recalculateTotalCost() method. Its purpose will be to recalculate the cost whenever the user changes something that affects the price, but only when at least the size and crust are already selected. We need to create conditional statements to catch these cases before the rest of the method can begin.
What we can do in this case is create two conditional statements that will check to ensure that the SelectedValue of the DropDownListBoxes are not empty strings and, if they are, return from the method:
Then, underneath these conditional checks, build the order and pass an order object to the CalculateCost() method:
CalculateCost() returns a decimal type value. Create a new decimal variable called cost and assign it to that returned value. Then, call string.Format(), passing in that cost variable. Set the resultLabel's text to that formatted string:
Now that the actual code for recalculating the cost has been written, all that's left is to call it whenever an appropriate control's value has been changed. We'll begin with the Size and Crust within Default.aspx. First of all, we need to set the value for the default selection to an empty string, ensuring that the conditional check will apply:
Next, change the OnSelectionChanged property of each DropDownList to call recalculateTotalCost() and set AutoPostBack equal to true:
Move now to the CheckBoxes and set their OnCheckChanged properties to recalculateTotalCost, and set their AutoPostBack properties to true as well:
Now, save and run your project to see the results:
The final step in the ordering process is to redirect the user to a simple 'Success' page upon a successful order being placed. To begin, we'll need to add a new Web Form called Success.aspx to the Presentation layer:
In the Source view of this page, simply add a header with the text "Success!" for the user to see upon being redirected:
Return to Default.aspx.cs and locate the okButton_Click event. Beneath the call to CreateOrder(), write the following code to redirect the user, supplying the file name of the Success page:
Now, save and run your project to see the result upon a successful order:
This completes this portion of the solution of Papa Bob's Mega Challenge. In the next and final part of the solution, we'll create the OrderManagement page and display the orders in a GridView.
Papa Bob's Mega Solution Part 8 - Displaying the Price to the User