title: How to FindControl on content page from dynamic User Control
body: <p>I am getting a null reference exception when trying to FindControl on a button. I have a shopping cart setup where I have a content page (.aspx) based on a master page. On the content page, there is a placeholder control in which I am dynamically adding user controls (one control per product, each with an 'add to cart' button inside it).</p>
  
  <p>When the user clicks button to add item to cart, I can add it to the cart successfully, then I am counting the number of items in the cart and if more than 1, trying to show the 'checkout' button, or if none in the cart hide it.</p>
  
  <p>I am using FindControl, but getting a null reference error. Why can't it find the checkout button? My current code is below:</p>
  
  <p>MASTER PAGE (template.Master):</p>
  
  <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="template.master.cs" Inherits="OUWP.template" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en"&gt;
      &lt;head runat="server"&gt;&lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;asp:ContentPlaceHolder ID="m_cph_body" runat="server"&gt;&lt;/asp:ContentPlaceHolder&gt;
      &lt;/form&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>CONTENT PAGE (shop.aspx)</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/template.Master" AutoEventWireup="true" CodeBehind="shop.aspx.cs" Inherits="OUWP.shop" %&gt;
  &lt;%@ MasterType VirtualPath="~/template.Master" %&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="m_cph_body" runat="server"&gt;
      &lt;asp:PlaceHolder ID="Catalogue" runat="server"&gt;
          &lt;!-- this is where the user controls are dynamically generated--&gt;
      &lt;/asp:PlaceHolder&gt;
      &lt;asp:Panel ID="pnl_Basket" runat="server"&gt;
          &lt;div&gt;
              &lt;asp:LinkButton ID="lbtnCheckout" runat="server"&gt;Continue to Checkout&lt;/asp:LinkButton&gt;
          &lt;/div&gt;
      &lt;/asp:Panel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>USER CONTROL PAGE (PurchasableProduct.ascx)</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PurchasableProduct.ascx.cs" Inherits="OUWP.CustomControls.PurchasableProduct" %&gt;
  
  &lt;asp:UpdatePanel ID="udpBody" runat="server"&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:Panel ID="pnlPurchasableProduct" runat="server"&gt;
              &lt;asp:LinkButton ID="lbtnAddLine" runat="server" OnClick="lbtnAddLine_Click"&gt;&lt;/asp:LinkButton&gt;
          &lt;/asp:Panel&gt;
      &lt;/ContentTemplate&gt;
      &lt;Triggers&gt;
          &lt;asp:PostBackTrigger ControlID="lbtnAddLine" /&gt;
      &lt;/Triggers&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>USER CONTROL CODE BEHIND (PurchasableProduct.ascx.cs)</p>
  
  <pre><code>protected void lbtnAddLine_Click(object sender, EventArgs e)
  {
      // try to find checkout button on parent page of control
      System.Web.UI.Page page = (System.Web.UI.Page)this.Page;
      LinkButton Target1 = (LinkButton)page.FindControl("lbtnCheckout");
  
      // count the items in the cart (saved in session variable)
      DataTable dt = (DataTable)Session["varDataTableCart"];
      Int32 ItemCount = 0;
      Int Line Quantity = 0;
      foreach (DataRow dr in dt.Rows)
      {
          Int32 LineQuantity = Convert.ToInt32(dt.Rows[dt.Rows.IndexOf(dr)]["Quantity"].ToString());
          ItemCount = ItemCount + LineQuantity;
      }
  
      // if 1 or more items in cart, try to make button visible
      if (ItemCount &gt; 0)
      {
          Target1.Visible = true;
      }
      // otherwise no items in cart, so try to hide checkout button
      else
      {
          Target1.Visible = false;
      }
  }
  </code></pre>
  
  <p>I have also tried to get at it through the master page using the code below but that didn't work either:</p>
  
  <pre><code>MasterPage mp1 = (MasterPage)page.Master;
  LinkButton Target1 = (LinkButton)mp1.Page.FindControl("lbtnCheckout");
  </code></pre>