title: Cant get value from textbox on gridview
body: <p>I have a GridView on an ASP.NET page with a TemplateField column that has a TextBox in the ItemTemplate.  I then have a command field which is supposed to pull the text from this TextBox and use it in a SqlCommand running a stored procedure.</p>
  
  <p>Here is my C# code:</p>
  
  <pre><code>int index = Convert.ToInt32(e.CommandArgument);
  GridViewRow selectedRow = this.gvwSNConfirm.Rows[index];
  TableCell intID = selectedRow.Cells[1];
  int reqID = int.Parse(intID.Text);
  // Get the SN from the text box on the selected row
  TableCell snCell = selectedRow.Cells[0];
  TextBox tbox = (TextBox)staffNum.FindControl("txtSN");
  string stSN = tbox.Text.ToString();
  </code></pre>
  
  <p>When I add a break point to see the values for intID and tbox.Text I get the right ID but the Text for tbox is "".</p>
  
  <p>The mark-up for the two columns I am referencing is</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="SN"&gt;
    &lt;ItemTemplate&gt;
      &lt;asp:TextBox ID="txtSN" runat="server" MaxLength="20" Width="100px" /&gt;
    &lt;/ItemTemplate&gt;
    &lt;HeaderStyle BackColor="#171695" Font-Names="Arial" Font-Size="Small" ForeColor="White" Wrap="true" /&gt;
  &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField="Request" HeaderText="Request" ReadOnly="true" SortExpression="Request" /&gt;
  </code></pre>
  
  <p>Can anyone provide any help why I cant get the text from the text box?  It worked the first time round but subsequently all tbox text values have been "".</p>
  
  <p>Many Thanks</p>
  
  <p>Andy</p>
  
  <p>NEW CODE (05/03/2010):</p>
  
  <pre><code>protected void gvwSecond_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if ((e.CommandName == "Confirm") || (e.CommandName == "Decline"))
    {
      int index = Convert.ToInt32(e.CommandArgument);
      GridViewRow row = gvwSecond.Rows[index];
  
      int secondID = int.Parse(row.Cells[1].Text);
      TextBox txtsn = ((TextBox)row.FindControl("txtSecSN"));
      string sn = txtsn.Text;
  </code></pre>