I am building a shopping cart. My requirement is to update the label to reflect the no. of items in the cart
The structure of the webapplication is as follows
1. Master Page
2. Header control within master page.
3. This header control has a label that shows the number of elements in the cart.
I have written an event handler that updates this cart label everytime an item is added. The header control has subscribed to this event handler. The problem is that the number does not get displayed in the label.
All this addition of items is done via ajax
Code of header.ascx file
*********************************************************************************************************
protected override void OnInit(EventArgs e)
{
var ceh = CustomEventHandler.GetCustomEventHandler();
ceh.UpdateCartEvent += csh_UpdateCartEvent;
base.OnInit(e);
}
void csh_UpdateCartEvent(int itemCount)
{
lblTotalCartItems.Text = itemCount.ToString(CultureInfo.InvariantCulture); // THIS SHOULD DISPLAY THE VALUE, BUT THIS DOES NOT
}
*********************************************************************************************************
********************************************************************************************************************
ShoppingCartFile where the item is added
protected int CartItemCount
{
set
{
var ceh = CustomEventHandler.GetCustomEventHandler(); // BINDING THE EVENT TO FIRE WHENEVER VALUE IS CHANGED
ceh.OnCartUpdate(value);
}
}
private void AddNewItemToCart()
{
var cart = new ShoppingCart
{
ProductId = 1,
ProductName = "Apple"
};
ShoppingCartManager.AddToCart(cart);
var sessionCart = SessionManager.GetSession<System.Collections.Hashtable>("Cart");
if (sessionCart != null)
{
CartItemCount = sessionCart.Count; // WHENEVER THE VARIABLE VALUE CHANGES, FIRE THE EVENT
}
}
********************************************************************************************************************
Eventhandler file
public class CustomEventHandler
{
private CustomEventHandler()
{
}
private static CustomEventHandler _customEventHandler;
public static CustomEventHandler GetCustomEventHandler()
{
return _customEventHandler ?? (_customEventHandler = new CustomEventHandler());
}
public delegate void UpdateCart(int itemCount);
public event UpdateCart UpdateCartEvent;
public void OnCartUpdate(int cartCount)
{
if (UpdateCartEvent != null)
UpdateCartEvent(cartCount);
}
}