I'm attempting to make a cart system for my C# based website using ASP.NET.
Currently I've created a method which updates a linkbutton which I have in the master page.
When I try and reference the linkbutton in a product page it errors saying 'productpages_stand' does not contain a definition for 'lbnCart' and no extension method 'lbnCart' accepting a first argument of type 'productpages_stand' could be found'.
Am I not able to link a linkbutton to a masterpage from a web form?
The method:
private void updateCartSummary()
{
// get number of items in cart and show summary in link button
ArrayList cart = (ArrayList)Session["CART"];
int totalItems = cart.Count;
this.lbnCart.Text = "Cart : " + "(" + totalItems + ")";
}
Link button from masterpage:
<asp:LinkButton ID="lbnCart" CssClass="shoppingcarttext button small third" runat="server">Cart : (0)</asp:LinkButton>
EDIT: Main code segment which is erroring on the product page:
public partial class productpages_atomosninja : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // first time
{
updateCartSummary();
}
LinkButton lbnCart = (LinkButton)Master.FindControl("lbnCart");
if (lbnCart != null)
{
lbnCart.Text = "Cart : " + "(" + totalItems + ")";
}
}
protected void btnAtomos_Click(object sender, EventArgs e)
{
Trace.Warn("Adding an item to the cart");
// create cart item object with the book details
CartItem cartItem = new CartItem();
cartItem.setCost(299.00);
cartItem.setItemName("Atomos Ninja 2");
// extract arraylist from session variable
ArrayList arrCart = (ArrayList)Session["CART"];
// add the cartitem object to the arraylist
arrCart.Add(cartItem);
//store arrayList back into the session variable
Session.Add("CART", arrCart);
updateCartSummary();
}
private void updateCartSummary()
{
// get number of items in cart and show summary in link button
ArrayList cart = (ArrayList)Session["CART"];
int totalItems = cart.Count;
this.lbnCart.Text = "Cart : " + "(" + totalItems + ")";
}
}
Error for totalItems in the page_load method saying it doesn't exist since it isn't defined until later in the document.
EDIT 2: After adding the code where @David said it still errors.
public partial class product : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // first time
{
updateCartSummary();
}
}
LinkButton lbnCart = (LinkButton)Master.FindControl("lbnCart");
if (lbnCart != null)
{
lbnCart.Text = "Cart : " + "(" + totalItems + ")";
}
}