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
Next: How an ASP.NET application is compiled and run >>
More ASP.NET Articles
More By Murach Publishing
|
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.
|
|