Description: The AlsoBought user control displays a list of the 5 other products most often purchased by customers who also purchased a specified product.
Implementation Notes: The AlsoBought control is used in the ProductDetails.aspx page.
The AlsoBought user control exposes a single public field -- ProductID -- at the top of the file. This enables consumers of the AlsoBought user control (like the ProductDetails.aspx page) to set the ProductID of the AlsoBought product either programmatically or declaratively (via a tag attribute). Note that only those fields, properties and methods that have a public accessor declaration can be externally accessed in this way. For example, the Page_Load event handler does not have a public accessor keyword before it, and therefore can not be called from the ProductDetails.aspx page.
The AlsoBought user control logic is encapsulated entirely within its Page_Load event handler. This event handler is called when a page containing the user control is accessed by a browser client.
Page_Load Event Handler: The Page_Load event handler creates an instance of the ProductDB class and calls its GetProductsAlsoPurchased method, passing it the product ID. This method internally uses the usp_CustomerAlsoBought stored procedure to fetch the product information from the Adventure Works Cycles database.
The product collection is displayed using a templated <asp:Repeater> server control. As used in this user control, the Repeater server control contains three user-defined templates -- HeaderTemplate, ItemTemplate and FooterTemplate -- that describe how the list should look. The ItemTemplate defines how the bound data is displayed; the HeaderTemplate defines HTML that is emitted at the beginning of the list; and the FooterTemplate defines HTML that is emitted at the end of the list.
The data values returned from the ProductsDB.GetProductsAlsoPurchased() method are populated into the Repeater by setting its Datasource property, and then calling its DataBind() method. When DataBind() is called, the Repeater will render its HeaderTemplate, then iterate over the DataSource and render a copy of the ItemTemplate for each row, populating it data from the row. Finally, the Repeater will render its FooterTemplate.
Performance Notes: