Working with Loops and Conditionals


Working with Loops and Conditionals

Like in all the other chapters in this book, the best way to learn about the topic at hand is to create a small project in which you can try new things.

In this chapter, we will create a very simple shopping-cart application. In the application you'll be able to select an item from among a list of items and then enter the quantity for the item you wish to purchase. The program will add the item to the shopping cart. After you're done adding items you can click on Place Order to view the total amount spent. The first half of the program will teach you how to use conditional statements to find a description and unit price for each item. The second half will teach you how to write a loop to calculate the total amount for the order.

To create a test project for this chapter:

  1. Launch Visual Studio .NET. (Start > All Programs > Microsoft Visual Studio .NET > Microsoft Visual Studio .NET).

  2. Select File > New > Project to bring up the New Project dialog.

  3. Under project types on the left side of the New Project window click the Visual C# projects folder.

  4. Select the ASP.NET Web Application icon and change the name of the application to loopsandconditionals ( Figure 3.1 ).

    Figure 3.1. The New Project dialog lets you select among various project types. For this chapter you are writing an ASP.NETWeb Application.

    graphics/03fig01.gif

  5. Visual Studio will create a new project and open WebForm1.aspx.

  6. Change the form's name to enterorder .aspx. Do so by choosing View > Solution Explorer from the top menu bar.

  7. Right-click on WebForm1.aspx and choose Properties. In the property grid below change the FileName property from WebForm1.aspx to enterorder.aspx ( Figure 3.2 ).

    Figure 3.2. Changing the filename through the property browser also renames the physical file.

    graphics/03fig02.gif

  8. Create a form that looks like the form in Figure 3.3 . Obviously this is a lot of work to do by hand. Instead you can enter the HTML directly into the editor. Figure 3.4 ( next page) shows the HTML necessary to create the form. To enter the HTML directly click the HTML button under the editor's window. As an alternative you could download the skeleton file for this project (see Tips below).

    Figure 3.3. You're going to write a shopping cart application for this chapter. The user selects the item to purchase and the quantity and the items are seen in a grid (a table). Then you click place order to see the total amount purchased.

    graphics/03fig03.gif

    Figure 3.4 The Visual Studio .NET lets you draw each control graphically, or manipulate the HTML script directly. If you add the HTML script below the editor will display the controls for the project.
     <%@ Page language="c#" Codebehind="enterorder.aspx.cs"          AutoEventWireup="false"          Inherits="loopsandconditionals.WebForm1"          enableViewState="False"          enableViewStateMac="False"%> <HTML>    <HEAD>      <title>WebForm1</title> </HEAD> <body MS_POSITIONING="GridLayout">      <form id="Form1" method="post" runat="server">         <asp:  TextBox  id="txtQty"                     style="Z-INDEX: 101; LEFT: 127px;                     POSITION: absolute; TOP: 56px"                     runat="server" Width="157"                     Height="24"                     EnableViewState="False">                     </asp:TextBox>         <asp:  label  id="lblItem"                     style="Z-INDEX: 102; LEFT: 45px;                     POSITION: absolute; TOP: 25px"                     runat="server"                     EnableViewState="False"                     Height="19"                     Width="70">  Item:  </asp:label>         <asp:  label  id="lblQty"                     style="Z-INDEX: 103; LEFT: 45px;                     POSITION: absolute; TOP: 60px"                     runat="server"                     EnableViewState="False"                     Height="19px"                     Width="70px">  Qty:  </asp:label>         <asp:  button  id="btnAdd"                     style="Z-INDEX: 104; LEFT: 291px;                     POSITION: absolute; TOP: 56px"                     runat="server"                     EnableViewState="False"                     Text="  Add  ">                     </asp:button>         <asp:  dropdownlist  id="lstItems"                     style="Z-INDEX: 105; LEFT: 127px;                     POSITION: absolute; TOP: 25px"                     runat="server"                     EnableViewState="False"                     Width="157">            <asp:ListItem Value="  Notebook  ">  Notebook Computer  </asp:ListItem>            <asp:ListItem Value="  Desktop  ">  Desktop Computer  </asp:ListItem>            <asp:ListItem Value="  FlatMon  ">  Flat Screen Monitor  </asp:ListItem>            <asp:ListItem Value="  FatMon  ">  Big Fat Monitor  </asp:ListItem>                      </asp:dropdownlist>          <asp:  datagrid  id="grdItems                      style="Z-INDEX: 106; LEFT: 47px;                      POSITION: absolute; TOP: 103px"                      runat="server"                      EnableViewState="False"                      Width="284px"                      AutoGenerateColumns="False">            <Columns>               <asp:BoundColumn DataField="Name"               HeaderText="  Name  ">               <HeaderStyle HorizontalAlign="Center"               ForeColor="White" BackColor="Black">               </HeaderStyle>               </asp:BoundColumn>               <asp:BoundColumn DataField="Quantity"               HeaderText="  Quantity  ">               <HeaderStyle HorizontalAlign="Center"               ForeColor="White" BackColor="Black">               </HeaderStyle>               </asp:BoundColumn>               <asp:BoundColumn DataField="Description"               HeaderText="  Description  ">               <HeaderStyle HorizontalAlign="Center"               ForeColor="White" BackColor="Black">               </HeaderStyle>               </asp:BoundColumn>               <asp:BoundColumn DataField="Price"               HeaderText="  Unit Price  ">               <HeaderStyle HorizontalAlign="Center"               ForeColor="White" BackColor="Black">               </HeaderStyle>               </asp:BoundColumn>            </Columns>         </asp:datagrid>         <asp:  label  id="lblEmpty"                    style="Z-INDEX: 107; LEFT: 51px;                    POSITION: absolute; TOP: 166px"                    runat="server"                    Width="284px"                    Visible="False">  Your shopping cart is empty - shame   on you!  </asp:label>        <asp:  hyperlink  id="lnkOrder"                    style="Z-INDEX: 108; LEFT: 256px;                    POSITION: absolute; TOP: 248px"                    runat="server"                    EnableViewState="False"                    NavigateUrl="finishorder.aspx">  Place Order  </asp:hyperlink>      </form>    </body> </HTML> 

graphics/tick.gif Tips

  • Remember that like in any other projects in this book, building the project isn't necessary for learning the concepts in this chapter.

  • Skeletons for each project can be downloaded from Peachpit's Web site, http://www.peachpit.com/vqs/csharp.




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