The Product Detail page displays the details about a specific product. The product displayed is the one corresponding to the ProductID parameter that was carried from the ProductsList.aspx page. All of the code is contained in the Page_Load() method. The Page_Load() method starts by converting the ProductID parameter into an integer. It then instantiates a ProductsDB object. A ProductDetails structure is obtained by calling the ProductsDB.GetProductDetails() method with the product ID as an argument to the method. The user interface objects are then populated. Table 8.2 shows the user interface objects that are populated.
Once the product data is fetched from the database, it is pushed into a number of server controls on the page. Most of these server controls are standard ASP.NET ones (Labels, Images, Hyperlinks, and so on). Two of the server controls, however, are custom IBuySpy user controls that we have written to encapsulate the Review List of a product, as well as a list of products that people who bought the current product Also Bought. Note that these user controls can be programmatically manipulated (property sets/gets, methods called, events sunk and raised) just like any other standard server control. They provide a very nice way to encapsulate and reuse functionality, as well as to cleanly partition work among multiple developers. Listing 8.5 shows the Page_Load() method in C# and VB. Listing 8.5 The Page_Load() Method That Populates the User Interface Objects for the Product Detail PageC# void Page_Load(Object sender, EventArgs e) { // Obtain ProductID from QueryString int ProductID = Int32.Parse(Request.Params["ProductID"]); // Obtain Product Details IBuySpy.ProductsDB products = new IBuySpy.ProductsDB(); IBuySpy.ProductDetails myProductDetails = products.GetProductDetails(ProductID); // Update Controls with Product Details desc.Text = myProductDetails.Description; UnitCost.Text = String.Format("{0:c}", myProductDetails.UnitCost); ModelName.Text = myProductDetails.ModelName; ModelNumber.Text = myProductDetails.ModelNumber.ToString(); ProductImage.ImageUrl = "ProductImages/" + myProductDetails.ProductImage; addToCart.NavigateUrl = "AddToCart.aspx?ProductdocText">VBSub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Obtain ProductID from QueryString Dim ProductID As Integer = CInt(Request.Params("ProductID")) ' Obtain Product Details Dim products As IBuySpy.ProductsDB = New IBuySpy.ProductsDB() Dim myProductDetails As IBuySpy.ProductDetails = _ products.GetProductDetails(ProductID) ' Update Controls with Product Details desc.Text = myProductDetails.Description UnitCost.Text = String.Format("{0:c}", myProductDetails.UnitCost) ModelName.Text = myProductDetails.ModelName ModelNumber.Text = myProductDetails.ModelNumber.ToString() ProductImage.ImageUrl = "ProductImages/" & _ myProductDetails.ProductImage addToCart.NavigateUrl = "AddToCart.aspx?ProductID=" & ProductID ReviewList.ProductID = ProductID AlsoBoughtList.ProductID = ProductID End Sub |