views:

770

answers:

1

Hiya

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;
A: 

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.

Bryan
The OnRowCommand event for the gridview. I'll try using e.Item.FindControl.
anD666
Can't access e.Item via the RunCommand as it uses GridViewCommandEventArgs. The annoying this is the code is the same as another webform except bar the control names.
anD666
In that case... not sure what to tell ya. Use Fiddler and make sure the data is getting posted. Then go through the code and make sure it's not being overwritten/cleared elsewhere.
Bryan
I managed to fix the problem, I deleted the page and started again but with slightly different code and it worked fine. I'm not sure if there was something hiding in there which was stopping the text box from being picked up but its working now :)Thank you for your help
anD666