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
}
}
The following list describes the property settings of the UpdatePanel control that determine when a panel's content is updated during partial-page
rendering.
If the UpdateMode property is set to
Always, the
UpdatePanel control’s content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls
that are inside other UpdatePanel controls, and postbacks from controls that are not inside
UpdatePanel controls.
If the UpdateMode property is set to
Conditional, the
UpdatePanel control’s content is updated when one of the following is true:
When the postback is caused by a trigger for that UpdatePanel control.
When the UpdatePanel control is nested inside another
UpdatePanel control and the parent panel is updated.
When the ChildrenAsTriggers property is set to
true and any child control of the UpdatePanel control causes a postback. Child controls of nested
UpdatePanel controls do not cause an update to the outer
UpdatePanel control unless they are explicitly defined as triggers for the parent panel.
</div>
Chetan Sarode
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
vijaykoul
0 Points
2 Posts
Issue updating label via ajax
Dec 19, 2012 09:00 AM|LINK
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);
}
}
hector_anton...
Member
82 Points
27 Posts
Re: Issue updating label via ajax
Dec 19, 2012 03:28 PM|LINK
if the label that display the amount is not inside your update pannel you will never see the updated value during an AJAX post.
chetan.sarod...
All-Star
65749 Points
11148 Posts
Re: Issue updating label via ajax
Dec 24, 2012 02:50 AM|LINK
Put the lable inside UpdatePanel
How UpdatePanel Controls Are Refreshed
<div class="subsection">The following list describes the property settings of the UpdatePanel control that determine when a panel's content is updated during partial-page rendering.
-
-
-
-
-
-
</div>If the UpdateMode property is set to Always, the UpdatePanel control’s content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls that are inside other UpdatePanel controls, and postbacks from controls that are not inside UpdatePanel controls.
If the UpdateMode property is set to Conditional, the UpdatePanel control’s content is updated when one of the following is true:
When the postback is caused by a trigger for that UpdatePanel control.
When you explicitly call the UpdatePanel control's Update method.
When the UpdatePanel control is nested inside another UpdatePanel control and the parent panel is updated.
When the ChildrenAsTriggers property is set to true and any child control of the UpdatePanel control causes a postback. Child controls of nested UpdatePanel controls do not cause an update to the outer UpdatePanel control unless they are explicitly defined as triggers for the parent panel.
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.