title: ASP.NET - Finding Label Control in a GridView
body: <p>I'm having a problem trying to find a label control that is inside a GridView.
  Please see my codes below:</p>
  
  <pre><code>&lt;asp:GridView ID="MyGridView" runat="server"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Date"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:TextBox runat="server" ID="txtDate" MaxLength="10" Width="70" /&gt;
                  &lt;asp:ImageButton ID="imgScoreDate" runat="server" ImageUrl="~/images/calendar.gif" /&gt;
                  &lt;ajaxtoolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" Format="MM/dd/yyyy" TargetControlID="txtDate" PopupButtonID="imgDate" /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Name"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="lblName" runat="server"/&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And here is my .cs file:</p>
  
  <pre><code>protected void LoadGridView()
  {
      //Do something else
  
      foreach (GridViewRow row in MyGridView.Rows)
      {
          //Tried A
          System.Web.UI.WebControls.Label lblName = row.FindControl("lblName") as System.Web.UI.WebControls.Label;
          lblName.Text = "Name";
  
          //Tried B
          ((System.Web.UI.WebControls.Label)row.FindControl("lblName")).Text = "Name";
      }
  }
  </code></pre>
  
  <p>I debug this code and it seems to work fine because my breakpoint is being hit each time the debugger runs. It even loops through my <code>foreach</code> block the same count as to how many rows my GridView has.</p>
  
  <p>But I don't understand why my <code>lblName</code> control doesn't get the "Name" text as a value? Am I missing anything here? I tried both <code>//Tried A</code> and <code>//Tried B</code> methods but they both doesn't update my label's text.</p>
  
  <p>Any help would be appreciated!</p>
  
  <p>Thanks! Cheers!</p>