title: Accessing asp.net controls of datalist in codebehind
body: <p>I am trying to access an asp Image tag in my code behind so that I can display a default image if there is no other image present. The problem that I am having is that I am getting an "Object reference not set to an instance of an object." error message. 
  The aspx page</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="MemberProfileList.ascx.cs"
  Inherits="UserControls_MemberProfileList" %&gt;
  &lt;%--&lt;asp:GridView ID="GridView1" runat="server"&gt;
  &lt;/asp:GridView&gt;--%&gt;
  &lt;asp:DataList ID="profilelist" runat="server" RepeatColumns="2" OnDataBinding="profilelist_DataBound" &gt;
  &lt;ItemTemplate&gt;
      &lt;asp:Image ID="ProfileImage"  runat="server" ImageUrl='&lt;%# System.String.Format("/Images/{0}", DataBinder.Eval(Container.DataItem, "ProfilePictureThumb"))%&gt;'  /&gt;
      &lt;asp:Hyperlink runat="server" NavigateUrl='&lt;%#Link.ToSpecificMemberProfile(Eval("Username").ToString()) %&gt;' Text='&lt;%#HttpUtility.HtmlDecode(Eval("Username").ToString()) %&gt;' /&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:DataList&gt;
  </code></pre>
  
  <p>The method in the code behind</p>
  
  <pre><code>profilelist.DataSource = myProfileList;
      profilelist.DataBind();
      Image ProfileImage = profilelist.FindControl("ProfileImage") as Image;
      string profilepic = ProfileImage.ImageUrl.ToString();
      if (profilepic == null)
      {
          ProfileImage.Visible = false;
      }
  </code></pre>
  
  <p>Any help is appreciated</p>