Adding Loops to the Sample Program


Adding Loops to the Sample Program

It's time to put our new knowledge of loops into practice. In this section you will add the code to make the Place Order link work. First you need to add a new Web Form to your project. Then you can add the code to calculate the order's grand total. Calculating the total cost isn't difficult. All you need to do is get hold of the ArrayList that contains all the items.

Earlier you learned that the sample code stores this ArrayList in the Session object. So we need only retrieve it from the Session object, go through all the items in the list, calculate the price times the quantity, add them all up, and print the value. And that's where loops come in.

To add loops to the sample application:

  1. Choose Project > Add Web Form. You will see the dialog in Figure 3.46 . Enter finishorder.aspx for the file name and press Enter.

    Figure 3.46. Use this dialog to add a new Web Form to your application.

    graphics/03fig46.gif

  2. Create a form that looks like the form in Figure 3.47 . Do so by entering the HTML directly into the editor ( Figure 3.48 ) or by downloading the skeleton file for this project from Peachpit's Web site.

    Figure 3.47. The finishorder.aspx Web Form has two labels. One is static and says thank you; the other reports the total amount for the order and is calculated at runtime.

    graphics/03fig47.gif

    Figure 3.48 You can recreate the form from the HTML, but it might be easier to download it from Peachpit's Web site.
     <%@ Page language="c#"     Codebehind="finishorder.aspx.cs"     AutoEventWireup="false"     Inherits="loopsandconditionals.     finishorder" %> <HTML>    <HEAD>       <title>finishorder</title>    </HEAD>    <body MS_POSITIONING="GridLayout">    <form id="finishorder" method="post"          runat="server">    <  asp:Label  id="lblThankYou"              style="Z-INDEX: 101;              LEFT: 24px;              POSITION: absolute;              TOP: 22px" runat="server"              Width="246px" Height="53px"              Font-Size="Large">  Thank you for your order.   Your total is:  </asp:Label>    <  asp:Label  id="lblTotal"              style="Z-INDEX: 102;              LEFT: 26px;              POSITION: absolute;              TOP: 95px" runat="server"              Width="243px"              Height="53px">              </asp:Label>    </form>    </body> </HTML> 
  3. Double-click on a blank space in the form. This will cause VS.NET to open the code editor and add a Page_Load function.

  4. In the Page_Load function, add the code in Figure 3.49 . This code retrieves the list of purchased items from the Session object, adds the cost for each entry, and then displays the total in a label.

    Figure 3.49 When the user clicks the Place Order link in the enterorder.aspx form, control is transferred to finishorder.aspx and ASP.NET calls the Page_Load function in your code. In this case we take the list of items from Session and add up all the prices times the quantities . Then we display the amount in the form's label.
     private void Page_Load(object sender,                      System.EventArgs e) {  ArrayList items =   (ArrayList)Session["items"];   if (items != null)   {   double total=0;   for (int i=0; i < items.Count; i++)   {   PurchasedItem item =   (PurchasedItem)items[i];   total += item.Price *   item.Quantity;   }   lblTotal.Text = total.ToString();   }  } 
  5. Build and execute your program.

graphics/tick.gif Tips

  • After clicking the Place Order link in your Web Form the results should look similar to Figure 3.50 .

    Figure 3.50. If everything works correctly your results should resemble this, give or take a few thousand dollars.

    graphics/03fig50.gif

  • This completes the sample code for this chapter, and the chapter as well.




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