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.
Here is my C# 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();
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 “”.
The mark-up for the two columns I am referencing is
<asp:TemplateField HeaderText="SN">
<ItemTemplate>
<asp:TextBox ID="txtSN" runat="server" MaxLength="20" Width="100px" />
</ItemTemplate>
<HeaderStyle BackColor="#171695" Font-Names="Arial" Font-Size="Small" ForeColor="White" Wrap="true" />
</asp:TemplateField>
<asp:BoundField DataField="Request" HeaderText="Request" ReadOnly="true" SortExpression="Request" />
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 “”.
Many Thanks
Andy
NEW CODE (05/03/2010):
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;
What event is this code in? You should e.Item.FindControl. If you are getting the right control for sure, but Text is empty, that means it’s either not getting posted back, or it’s being cleared somewhere else.