title: c# - error using FindControl on div id
body: <p>I have an ASP.NET site that I am trying to access div elements by their ID from the C# code behind file. Essentially I want to see if a div element exists, and if so, alter its properties.</p>
  
  <p>I've found many resources out there that point to a dozen different solutions and none of them seem to work.</p>
  
  <p><strong>HTML on ASP.Net Page:</strong></p>
  
  <pre><code>&lt;div class="contentArea"&gt;
       &lt;div class="block" id="button1" runat="server"&gt;
              Some Content Here
       &lt;/div&gt;
       &lt;div class="block" id="button2" runat="server"&gt;
              Some Content Here
       &lt;/div&gt;
       &lt;div class="block" id="button3" runat="server"&gt;
              Some Content Here
       &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>C# Code Behind (examples I've tried):</strong></p>
  
  <pre><code>System.Web.UI.HtmlControls.HtmlGenericControl div1 = (System.Web.UI.HtmlControls.HtmlGenericControl)this.FindControl("button1");
  div1.Attributes["class"] = "classNameHere";
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Control div1 = this.FindControl("button1");
  div1.GetType();
  </code></pre>
  
  <p>When the code gets to the second line of each of the above examples, I get an error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>If I try the following:</p>
  
  <pre><code>if (div1 != null)
  {
      // Do Something;
  }
  </code></pre>
  
  <p>Nothing ever happens because div1 is always set to null. Ironically, if I look at the Locals window and examine this, I can see the button# ids in the listing, so I know they are there, but the system is acting like it isn't finding the control.</p>
  
  <p>My ultimate goal is to find the max id # of the button divs (looking at my html example, the max id would be 3 (button3). Maybe there is a better way to go about it, but either way, once I have my max id, I want to be able to touch each div and alter some css properties.</p>
  
  <p>Although I could easily do all of this via jQuery, in this instance I need to do this in C#.</p>
  
  <p>Any help is much appreciated. If you need more info, let me know.</p>
  
  <p><strong>UPDATE</strong>
  I created a new C# web project from scratch. After adding a masterpage (and not altering it) and adding a webform using masterpage, I only added one line to the webform under Content ID="Content2":</p>
  
  <pre><code>&lt;div id="button1"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>From c# code behind I still run into the same exact issue as before.</p>
  
  <p><strong>FINAL UPDATE AND ANSWER</strong>
  I'm shocked no one (including myself) caught my mistake from the above update. I never put runat="server" when I created a new project from scratch under the div. Here is how I fixed my problem under my new project from scratch:</p>
  
  <p>Add runat="server" to div:</p>
  
  <pre><code>&lt;div id="button1" runat="server"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Then I did a FindControl on the ContentPlaceHolder under the MasterPage:</p>
  
  <pre><code>ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
  </code></pre>
  
  <p>Note: This is what the ContentPlaceHolder code looks like on the Site.Master page created by default:</p>
  
  <pre><code>&lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"&gt;
  
  &lt;/asp:ContentPlaceHolder&gt;
  </code></pre>
  
  <p>After finding this ContentPlaceHolder in code behind, I then searched within this placeholder for button1:</p>
  
  <pre><code>using System.Web.UI.HtmlControls;
  HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
  </code></pre>
  
  <p>Finally I check to see if myControl is null:</p>
  
  <pre><code>if (myControl != null)
  {
      \\ Do Something
  }
  </code></pre>
  
  <p>When I ran this code, it found the div I was looking for. Here is the complete code behind all put together:</p>
  
  <pre><code>using System.Web.UI.HtmlControls;
  
  ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
  HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
  
  if (myControl != null)
  {
      // Do Something
  }
  </code></pre>