title: Asp.Net / C# - How to get the Text of a Label control nested inside a Repeater?
body: <p>I am trying to get the text of my label which is inside a repeater, but I keep getting a <code>NullPointerException</code>. </p>
  
  <p>All of the data is coming from database and it is coming correctly. </p>
  
  <p>When I click on the LinkButton, I want to use the Label Text for next bit code. </p>
  
  <p>Aspx page:</p>
  
  <pre><code>   &lt;asp:Repeater ID="RepeaterDepartmentParent" runat="server"&gt;
         &lt;ItemTemplate&gt;
                  &lt;div id="outerDiv" class="col-lg-3 col-xs-6" runat="server"&gt;
                          &lt;!-- small box --&gt;
                          &lt;div&gt;
                              &lt;div class="inner"&gt;
  
                                  &lt;p&gt;
                                     &lt;%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%&gt;
                                  &lt;/p&gt;
                              &lt;/div&gt;
  
  
                              &lt;asp:Label ID="lblDepartmentId" runat="server" Text='&lt;%# DataBinder.Eval(Container.DataItem, "Department_Idint")%&gt;' Visible="true"&gt;&lt;/asp:Label&gt;
  
                              &lt;asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click"&gt;More info&lt;i class="fa fa-arrow-circle-right"&gt;&lt;/i&gt;&lt;/asp:LinkButton&gt;
  
                          &lt;/div&gt;
                  &lt;/div&gt;&lt;%--&lt;%-- ./col --&gt;--%&gt;
         &lt;/ItemTemplate&gt;
         &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Code Behind:</p>
  
  <pre><code>  protected void Page_Load(object sender, EventArgs e)
      {
          DataSet dsParentDepartment = null;
          dsParentDepartment = objDepartmentBL.viewDepartmentparent();
           RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
          RepeaterDepartmentParent.DataBind();
  
      }
  
    protected void linkChildDepartment_Click(Object sender, EventArgs e)
      {
          //what to write here??
          //i have tried the bellow code but it gives me every data in that loop but i
          //want the single data for a link button click.
          //foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
         // {
            //  Label myLabel = (Label)item.FindControl("lblDepartmentId");
           //   myLabel.Text = Id;
          //}
         //edited code that works properly
          LinkButton linkChildDepartment = (LinkButton)sender;
          RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
          Label myLabel = (Label)item.FindControl("lblDepartmentId");
  
      }
  </code></pre>
  
  <p>How can I correctly reference the Link Button Label Text?</p>