title: Accessing TemplateField controls value of gridview inside button click handler in asp.net
body: <p>After clicking the edit linkbutton of my gridview, I show the data on different text boxes which are not inside the gridview. I have a "Reset" button which i want to use to get back to the original values. But I am having problem to access those gridview data inside the button click handler and reset it.I tried using DirectCast() but its showing System.NullReferenceException.</p>
  
  <pre><code>&lt;asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="id"&gt;
  &lt;Columns&gt;
       &lt;asp:TemplateField&gt;
           &lt;ItemTemplate&gt;
               &lt;asp:LinkButton ID="lblEdit" runat="server" CausesValidation="false" CommandName="editRecord" Text="EDIT" CommandArgument="&lt;%# Container.DataItemIndex %&gt;"&gt;&lt;/asp:LinkButton&gt;
           &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
       &lt;asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False"&gt;
          &lt;ItemTemplate&gt;
               &lt;asp:Label ID="lblRecordID" runat="server" Text='&lt;%# Bind("id") %&gt;'&gt;&lt;/asp:Label&gt;
           &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
      &lt;asp:TemplateField HeaderText="HANGER"&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:Label ID="lblHANGER" runat="server" Text='&lt;%# Bind("HANGER") %&gt;'&gt;&lt;/asp:Label&gt;
          &lt;/ItemTemplate&gt;
       &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;&lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The backend vb.net code is-</p>
  
  <pre><code>Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnReset.Click
          Dim vID As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
          Dim vHanger As Label = DirectCast(GridView3.SelectedRow.FindControl("lblRecordID"), Label)
          txtID.Text.Text = vID.Text()
          ddlHanger.SelectedValue = vHanger.Text 'dropdown list that's why selectedValue used
  
  End Sub
  </code></pre>
  
  <p>I have copied the portion of the code cause the gridview has lot more rows. I would appreciate if anyone please show me a solution.Thanks in advance.</p>