title: User Control Postback
body: <p>I am working on rendering a set of cart items using a user control. Each cart item can be removed via a button in the user control. When a cart item is removed I need to visually show it's removal. However, since the cart item existed during the loading of the page it remains until the page is refreshed again. What I am after is a means to refresh the page after the work to remove the cartitem has been completed.</p>
  
  <p>The code behind cart.aspx.cs looks like:</p>
  
  <pre><code>protected void Page_Init(object sender, EventArgs e)
  {
      CreateCartItemControls();
  }
  
  private void CreateCartItemControls()
  {
      foreach (CartItem ci in Profile.Cart.Items)
      {
          ASP.CartItemControl cic = new ASP.CartItemControl();
          cic.ItemName = ci.Name;
  
          cic.CartID = ci.ID;
          cic.Cost = ci.BaseCost.ToString("c");
          cic.ItemComponents = ci.Components;
  
          cic.CartItemRemoved += new EventHandler(CartItemRemoved);
  
          Content.Controls.Add(cic);
      }
  }
  
  void CartItemRemoved(object sender, EventArgs e)
  {
      Master.UpdateCartItemCount();
  }
  </code></pre>
  
  <p>Markup for CartItemControl.ascx</p>
  
  <pre><code>&lt;%@ Control Language="C#" ClassName="CartItemControl" AutoEventWireup="true"
      CodeFile="CartItemControl.ascx.cs"
      Inherits="UserControls_CartItemControl" %&gt;
  &lt;fieldset id="FieldSet" runat="server"&gt;
      &lt;legend&gt;
          &lt;asp:HyperLink ID="ItemLink" runat="server" /&gt;
      &lt;/legend&gt;
      &lt;asp:ImageButton ID="RemoveCartItem" AlternateText="Remove Item" 
          ImageUrl="~/img/buttons/remove_4c.gif" runat="server"
          CommandName="Remove" OnCommand="RemoveCartItem_Command" /&gt;
      &lt;asp:Label ID="TotalItemCost" runat="server" Text="$0.00" /&gt;
      &lt;ol&gt;
          &lt;li runat="server" id="ComponentsLI" visible="false"&gt;
              &lt;fieldset id="ComponentsFieldSet" runat="server"&gt;
                  &lt;legend&gt;Item Components&lt;/legend&gt;
                  &lt;asp:CheckBoxList ID="ItemComponentsCheckList"
                      runat="server" /&gt;
              &lt;/fieldset&gt;
          &lt;/li&gt;
      &lt;/ol&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>Code behind for the UserControl CartItemControl.ascx.cs</p>
  
  <pre><code>public partial class UserControls_CartItemControl
  : System.Web.UI.UserControl
  {
  public string ItemName { get; set; }
  public int CartID { get; set; }
  public string Cost { get; set; }
  public IDictionary&lt;int, SoftwareComponent&gt; ItemComponents { get; set; }
  
  protected void Page_PreRender(object sender, EventArgs e)
  {
      SetCartItemControlAttributes();
  }
  
  private void SetCartItemControlAttributes()
  {
      ItemLink.Text = ItemName;
      TotalItemCost.Text = Cost;
  
      RemoveCartItem.CommandArgument = CartID.ToString();
  
      if (!ItemComponents.Count.Equals(0))
      {
          ComponentsLI.Visible = true;
          foreach (KeyValuePair&lt;int, ItemComponent&gt; kvp in
              ItemComponents)
          {
              ItemComponentsCheckList.Items.Add(
                  new ListItem(string.Format("{0} {1}",
                      kvp.Value.ComponentName,
                      kvp.Value.ComponentCost.ToString("c")),
                      kvp.Key.ToString()));
          }
      }
  }
  
  public event EventHandler CartItemRemoved;
  
  protected void RemoveCartItem_Command(object sender, CommandEventArgs e)
  {
      int itemID;
  
      if (int.TryParse(e.CommandArgument.ToString(), out itemID))
      {
          Profile.Cart.RemoveCartItem(itemID);
          CartItemRemoved(sender, e);
          Parent.Controls.Remove(this);
      }
  }
  }
  </code></pre>