title: Find A Control (Inside Master Page) From Content Page, Error = NullReferenceException
body: <p>Please See The Simple Example Below For Understanding My situation.<br>
  (Attention To Comments Inside Codes)  </p>
  
  <p>Master Page (ASPX) :   </p>
  
  <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="NiceFileExplorer.Site1" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;asp:ContentPlaceHolder ID="head" runat="server"&gt;
      &lt;/asp:ContentPlaceHolder&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;span runat="server" id="SummaryContainer"&gt;
              &lt;asp:Label ID="lblDownload_Count_By_UserID_Today_Title" runat="server" Text="Count :"
                  ToolTip="Your Download Count-Today" CssClass="lblTitleInStatistics_Master"&gt;&lt;/asp:Label&gt;
              &lt;asp:Label ID="lblDownload_Count_By_UserID_Today" runat="server" Text="&lt;%# Download_Count_By_UserID_Today() %&gt;"
                  CssClass="lblCountInStatistics_Master" ToolTip="Your Download Count-Today"&gt;&lt;/asp:Label&gt;
              &lt;span style="color: white;"&gt;&amp;nbsp;|&amp;nbsp;&lt;/span&gt;
              &lt;asp:Label ID="lblDownload_Size_By_UserID_Today_Title" runat="server" Text="Size :"
                  ToolTip="Your Download Size-Today" CssClass="lblTitleInStatistics_Master"&gt;&lt;/asp:Label&gt;
              &lt;asp:Label ID="lblDownload_Size_By_UserID_Today" runat="server" Text="&lt;%# Download_Size_By_UserID_Today() %&gt;"
                  CssClass="lblCountInStatistics_Master" ToolTip="Your Download Size-Today"&gt;&lt;/asp:Label&gt;
          &lt;/span&gt;
      &lt;/div&gt;
      &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" ViewStateMode="Inherit" ClientIDMode="Static"&gt;
      &lt;/asp:ContentPlaceHolder&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>as you see i set ClientIDMode="Static".  </p>
  
  <p>Master Page (CodeBehind) :  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace NiceFileExplorer
  {
      public partial class Site1 : System.Web.UI.MasterPage
      {
          protected void Page_Load(object sender, EventArgs e)
          {
               SummaryContainer.DataBind();
          }
  
          protected string Download_Count_By_UserID_Today()
          {
              //Read New Count From DataBase
              //return Count;
              return "Test";
          }
  
          protected string Download_Size_By_UserID_Today()
          {
              //Read New Size From DataBase
              //return Size;
              return "Test";
          }
      }
  }
  </code></pre>
  
  <p>Content Page (ASPX) :  </p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NiceFileExplorer.WebForm1" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;
  Conntent Page
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Content Page (CodeBehind) :  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace NiceFileExplorer
  {
      public partial class WebForm1 : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              MyMethod();
          }
  
          private void MyMethod()
          {
              //Add New Downloaded File Info To DataBase(); -&gt; For Getting Count And Size Of Them Per Day
  
              //Here I Wand To Access Master Page Controls And Update Count And Size Lables
              //So, I Tried Codes Below Without Any Results -&gt; How Can I Fix This ?
              var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer");
              SummaryContainer.DataBind();
              SummaryContainer.InnerHtml = "&lt;h2&gt;Hello World&lt;/h2&gt;";
  
              //After Update Those Lables Failed, I test the codes Below With Null Execption Error -&gt; How Can I Fix This ?
              var lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title");
              lblDownload_Count_By_UserID_Today_Title.Text = "test";
  
              DwonloadFile();
          }
  
          private void DwonloadFile()
          {
              //A Class (Method) That Shows Download Window To My Users, So Page_Load Of Master Will Never Fire...
              //And This Is The Reason That I want to update count &amp; size lables from content page
          }
  
      }
  }
  </code></pre>
  
  <p>i want to DataBind SummaryContainer(a span) from content page's code-behind.
  so i tried the codes below : </p>
  
  <pre><code>            var SummaryContainer = (System.Web.UI.HtmlControls.HtmlGenericControl)Page.Master.FindControl("SummaryContainer");
              SummaryContainer.DataBind():
  </code></pre>
  
  <p>but i can not see new results.<br>
  After That Fail I tried to find a label's text(that label is inside Master) from content page code behind for test like below :                     var </p>
  
  <pre><code>lblDownload_Count_By_UserID_Today_Title = (Label)Page.Master.FindControl("lblDownload_Count_By_UserID_Today_Title");
                      lblDownload_Count_By_UserID_Today_Title.Text = "test"; 
  </code></pre>
  
  <p>but i have <code>System.NullReferenceException</code> ERROR :</p>
  
  <p><code>Object reference not set to an instance of an object.</code></p>
  
  <p>how can i fix that error and force that span to show me new results? </p>
  
  <p>thanks in advance</p>