ASP.NET
  Home arrow ASP.NET arrow Page 3 - An ASP.NET Web Application in Action
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

An ASP.NET Web Application in Action
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-06-24

    Table of Contents:
  • An ASP.NET Web Application in Action
  • The aspx code for the Order form
  • The Visual Basic code for the Order form
  • How an ASP.NET application is compiled and run
  • Perspective

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    An ASP.NET Web Application in Action - The Visual Basic code for the Order form


    (Page 3 of 5 )

    To give you some idea of how the Visual Basic code for a form works, figure 1-11 presents the code-behind file for the Order form. Here, I’ve highlighted the code that’s specific to ASP.NET, and all of the other code is standard Visual Basic code.

    The first highlighted line is a Class statement that names the class, which has the same name as the form. Because this statement uses the Partial keyword, this is a partial class that must be combined with another partial class when it’s compiled. The second line of code in this class indicates that it inherits the System.Web.UI.Page class, which is the .NET class that provides all of the basic functionality of ASP.NET pages.

    Each time the page is requested, ASP.NET initializes it and raises the Load event, which is handled by the Page_Load procedure. This procedure uses the IsPostBack property of the page to determine whether the page is being posted back to the server from the same page. If this property isn’t True, the DataBind method of the drop-down list is used to bind the products that are retrieved from a database to the list. You will often see the IsPostBack property used like this in a Page_Load procedure to determine whether or not a page is being posted back by the user.

    The btnAdd_Click procedure is executed when the user clicks on the Add to Cart button on the Order page, which starts a postback. After this procedure adds the selected item to the cart, it uses the Redirect method of the Response property of the page to transfer to the Cart page. This is a second way to switch from one page to another.

    To refer to the session state object, you use the Session property of the page. This is done in the GetCart procedure, which is the last procedure for the page. Here, if the session state object doesn’t already contain a cart, a new sorted list is added to the object. Otherwise, the current cart is retrieved from the session object as a sorted list. Either way, control returns to the calling procedure, which is the AddToCart procedure, and that procedure can add a cart item to the cart. In addition, the Cart page will have access to the cart when the Order page is redirected to the Cart page.

    One coding point worth noting is that IsPostBack, Response, and Session are all properties of the page. As a result, you could code them with Page as a qualifier as in

      Page.IsPostBack

    However, since Page is the default object within a code-behind file, you can omit this reference.

    Although this is just a quick introduction to a code-behind file, I hope it gives you some insight into the way code-behind files are coded. Keep in mind, though, that all of this code will be explained in detail in the next two chapters. For now, you just need a general idea of how the events are processed, how the IsPostBack property is used, how one web page can switch to another page, and how the session state object is used.

    The code-behind file for the Order form (Order.aspx.vb)

    Imports System.Data
    Partial Class Order
        Inherits System.Web.UI.Page

        Private selectedProduct As Product

        Protected Sub Page_Load(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                ddlProducts.DataBind()
            End If
            selectedProduct = Me.GetSelectedProduct
            lblName.Text = selectedProduct.Name
            lblShortDescription.Text = selectedProduct.ShortDescription
            lblLongDescription.Text = selectedProduct.LongDescription
            lblUnitPrice.Text = FormatCurrency(selectedProduct.UnitPrice)
            imgProduct.ImageUrl = "Images\Products" & selectedProduct.ImageFile
       
    End Sub

        Private Function GetSelectedProduct() As Product
            Dim productTable As DataView = CType(AccessDataSource1.Select( _ 

               DataSourceSelectArguments.Empty), DataView)
            productTable.RowFilter = "ProductID = '" & ddlProducts.SelectedValue & "'"
            Dim productRow As DataRowView = productTable(0)
            Dim product As New Product
            product.ProductID = productRow("ProductID").ToString
            product.Name = productRow("Name").ToString
            product.ShortDescription = productRow("ShortDescription").ToString
            product.LongDescription = productRow("LongDescription").ToString
            product.UnitPrice = CDec(productRow("UnitPrice"))
            product.ImageFile = productRow("ImageFile").ToString
            Return product
    End Function

    Protected Sub btnAdd_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim cartItem As New CartItem
        cartItem.Product = selectedProduct
        cartItem.Quantity = CInt(txtQuantity.Text)
        Me.AddToCart(cartItem)
        Response.Redirect("Cart.aspx")
    End Sub

    Private Sub AddToCart(ByVal cartItem As CartItem)
        Dim cart As SortedList = Me.GetCart
        Dim productID As String = selectedProduct.ProductID
        If cart.ContainsKey(productID) Then
            cartItem = CType(cart(productID), CartItem)
            cartItem.Quantity += CInt(txtQuantity.Text)
        Else
            cart.Add(productID, cartItem)
        End If
    End Sub

    Private Function GetCart() As SortedList
        If Session("Cart") Is Nothing Then
            Session.Add("Cart", New SortedList)
        End If
        Return CType(Session("Cart"), SortedList)
    End Function

    Figure 1-11  The Visual Basic code for the Order form

    More ASP.NET Articles
    More By Murach Publishing


     

    Buy this book now. This article is an excerpt from chapter one of Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472). Check it out today at your favorite bookstore. Buy this book now.

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek