I'm trying to set up a shopping basket on an ASP.NET (VB) site that I'm putting together.
The code I'm using for this is largely based on the sample website at the end of SAMS ASP.NET Unleashed 2nd Edition.
- a VB class file (Namespace="StoreComponent
s", Public Class="ShoppingCart")
- a .ASCX control which raises an instance of ShoppingCart and displays existing ShoppingCart items and provides buttons and events for removing items from the cart.
- a .ASPX page which includes the .ASCX control, alongside a repeater with items I would like to add to the basket.
What I would like to do is use a Button_Click event in the ASPX's repeater area to run a sub which collates product info and, in turn, runs a sub in the .ASCX control to add a new item to the basket and update the basket display.
A shortened version of the ShoppingCart.ascx code is below:
<%@ Import Namespace="StoreComponents" %>
Dim objShoppingCart As ShoppingCart
objShoppingCart = New ShoppingCart
Public Sub AddToBasket(ItemId As Long, PartCode As String, Brand As String, UnitPrice As Decimal)
objShoppingCart.Add(ItemId, PartCode, Brand, UnitPrice)
dgrdShoppingCart.DataSource = objShoppingCart.Items
<asp:DataGrid ID="dgrdShoppingCart" runat="server"></asp:DataGrid>
A shortened version of the Products.aspx code is below:
<%@ Page Language="VB" %>
<%@ Register TagPrefix="myControls" TagName="ShoppingCart" Src="~/ShoppingCart.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
Sub Button_AddToBasket_Click(s As Object, e As RepeaterCommandEventArgs)
If e.CommandName = "cmdItemID" Then
Dim lngXrefId As Long
lngXrefId = Convert.ToInt64(e.CommandArgument)
'SOME CODE WHICH RETRIEVES THE VALUES FOR PartCode, Brand etc
AddToBasket(ItemId, PartCode, Brand, MatchBrand, Reference, MatchReference, UnitPrice)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head id="Head1" runat="server">
<form id="form1" runat="server">
<myControls:ShoppingCart runat="server" />
<asp:Repeater ID="rptPartsForUniqueModel" OnItemCommand="Button_AddToBasket_Click" runat="server">
<%#Container.DataItem( "PartCode" ) %>
<asp:Button Text="Add to basket" CommandArgument=<%#Container.DataItem( "ItemID" ) %> CommandName="cmdItemID" runat="server" /></ItemTemplate>
The problem comes trying to achieve line 11 of the above code.
I can call the AddToBasket() sub from within the ASCX page based on events on the ASCX page themselves. However, I cannot get the AddToBasket() sub to be called from the script part of the .ASPX page.
The items I've Googled on this have left me with more questions than answers and tend to be C# examples (which I really struggle with)
Are there a few lines I can add or modify in my existing code to expose the Sub in the .ASCX file to the .ASPX file which uses this control?