|
Hot-Link Menu in Right-Side Column |
Wal Mart Online → Store Pickup → Free Shipping![]() |
||||||||||||||||||||||||||||||||||
|
Extract Local Access Database DataEnter the following code segment above the Page_Load Event in the Order.aspx.vb file to handle the extraction of data from the Access Database:
'Create a Product Object to hold the cell data of the Access Database
Private SelectedProduct As New Product
Private Function GetSelectedProduct() As Product
'Create a DataView Object to hold the Access Database
Dim ProductTable As DataView
'Create a DataRowView Object to hold 1 Row of the Access Database
Dim ProductRow As DataRowView
'Place entire Database into ProductTable
ProductTable = CType(AccessDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
'Remove all Rows except the row that is currently selected on the DropDownList
ProductTable.RowFilter = "ProductID = '" & ddlProducts.SelectedValue & "'"
'ProductRow = ProductTable(0)
ProductRow = ProductTable(0)
'Transfer the cell data into the Selected Product Object
SelectedProduct.ProductID = ProductRow("ProductID").ToString
SelectedProduct.Name = ProductRow("Name").ToString
SelectedProduct.ShortDescription = ProductRow("ShortDescription").ToString
SelectedProduct.LongDescription = ProductRow("LongDescription").ToString
SelectedProduct.UnitPrice = CDec(ProductRow("UnitPrice"))
SelectedProduct.ImageFile = ProductRow("ImageFile").ToString
Return SelectedProduct
End Function
Order Page Visual Basic CodeWhen a page loads in VB.NET the IsPostBack property is set as true or false. This property indicates whether the page is being loaded in response to a Client Postback in which case the property is true or whether the page being loaded is being processed by the Browser for the first time, in which case the property is set to false. If the page is being is loaded for the first time, we will need to bind the data from the Access Database to the DropDownList (ddlProducts). The data is automatically bound by VB.NET but not until after the page completes the PageLoad process. Enter the following code for the PageLoad function on the Order.aspx.vb page:
Protected Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Bind Data if this is the first load by the Browser
If Not IsPostBack Then
ddlProducts.DataBind()
End If
'Get the Cell Data
SelectedProduct = Me.GetSelectedProduct
'Update the Label Controls
lblName.Text = SelectedProduct.Name
lblShortDescription.Text = SelectedProduct.ShortDescription
lblLongDescription.Text = SelectedProduct.LongDescription
lblUnitPrice.Text = FormatCurrency(SelectedProduct.UnitPrice)
ProductImage.ImageUrl = "http://centurion2.com/VBHomework/VB130/Images/Products/" & SelectedProduct.ImageFile
End Sub
Correct any errors and run the program. When you do, you should get the following web page:
Well, isn't that wonderful? We still have a problem, though, if you select a different product, the page does not update. What good is that? The reason is that there is no event for the Drop Down List changed. One choice is to solve this problem the old-fashioned way, by writing an Event Handler for the Drop Down List, but, we have already written the code to display the product with the Page_Load event handler. That would also mean writing a sub to update the product display like we did for the Page_Load event handler, or... How about if we just set the AutoPostBack property to true? If we do that, the page will reload every time a different product is selected, and viola. Set the AutoPostBack to true as indicated in the diagram below:
Run the program again, and let's see if that's all we have to do to effectively create an event handler for the Drop Down List. Change the DropDownList selection to something like severed head. When you do, it should look like the diagram below:
Just Lovely, isn't it? Think I will set one or two up on my mantle. Of course, you can too. But, if severed heads aren't your thing maybe you might want to consider something a little more mundane like a new TV, DVR or something. To get the best deals with great tech service, and free shipping if you work it right, follow the link above to: Centurion Online → Electronics → Video ProductsIf you haven't spent all of your disposable income @ Centurion Online yet, you might want to save a couple of pennies for one of those Severed Heads as illustrated in Figure CC-18. To make it possible to buy those and several other ghoulish items we will need to make a Chopping Cart page, to display the items the customer has selected for purchase.
I see you've made it here, and didn't get distracted by that Google Ad, right above. That being the case, let's add a second page to our site: Add New Page to Web SiteSelect: Project → Add New Item as illustrated in the diagram below:
Left-Clicking on the New Item option brings up the Template Selection Dialog Box as illustrated in the diagram below:
Any serious developer needs a system with the necessary Horsepower to perform the countless calculations peformed by Visual Studio. In addition, an adequate size monitor is a must so you can view the neccesary documents simultaneously. In order to get the best value for your development dollar take a trip to: Centurion Online → Computers → DesktopsIn order to continue with this guide, and start the discussion on Setting the Start Page: Press the Button below:
|
Home Visual Basic Introduction to VB.NET.NET Framework VS2008 IDE How VB is Compiled Start Visual Studio Windows Form App Save Your Work VB OOP ProgrammingVisual Basic Code Exit Code Button Event Code Coding RecommendationsIf/Then/Else Error List Window Comment Syntax Help Window Language Essentianl Built-In Data Types Declare Variables Declare ConstantsCode Arithmetic Expressions Assignment Statements Operator Precedence Type Casting Math Class String DeclarationConversion Functions Conversion Methods Formatting Functions String Formatting Variable Scope EnumerationsNullable Types Loop Constructs For Next LoopDo While Loop Do Until Loop Do...Loop-WhileDo...Loop-Until Exit Do | Exit For Do...LoopNested Loops Arrays Array DeclarationRnd( ) Function Listbox ControlKeyPressEventArgs Parallel Arrays Key Event ArgsDynamic Arrays Redimension ArraySet Breakpoint Start Debugger ReDim Preserve MultiDimensional Arrays DataGridView ControlLength and Sort Methods Structures Pad RightSplit Method IsNumeric Function Multiform Projects Add Form To Project Form Object Methods Form Show Method ShowDialog Method Form Close Method Form Accept Button Multiform Project Example ASP.NET Web Programming Create Data SourceConfigure Access Data Source Add Product Class Extract Local Database Data Order PageLoad VB CodeAdd New Web Page Set Start Page Display Cart Aspx CodeDisplay Cart Design View Sorted List Definition VB.NET Session State Create CartItem Class GetCartContents FunctionAdd To Cart Event Handler Remove Cart Item EventClear Cart Event Handler |