2010-02-24 62 views
1

我在ASP.NET页面上有一个GridView,其TemplateField列在ItemTemplate中有一个TextBox。然后我有一个命令字段,它应该从该TextBox中提取文本,并在运行存储过程的SqlCommand中使用它。无法从gridview上的文本框中获取值

这里是我的C#代码:

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(); 

当我添加一个破发点,看我得到正确的ID但TBOX的文字是“为INTID和tbox.Text值”。

的加价为两列,我引用是

<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" /> 

任何人都可以提供任何帮助,为什么我不能得到从文本框中的文本?它第一次运行,但后来所有的tbox文本值已经“”。

非常感谢

安迪

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; 
+0

设法让代码工作后,它突然停止工作,我不知道为什么。上面包含了正在工作的更新代码。 – anD666

回答

1

什么事件是该代码?你应该e.Item.FindControl。如果你确实掌握了正确的控制方式,但是文本是空的,这意味着它要么不被回传,要么被清除到别的地方。

+0

GridView的OnRowCommand事件。我会尝试使用e.Item.FindControl。 – anD666

+0

由于使用GridViewCommandEventArgs,因此无法通过RunCommand访问e.Item。令人讨厌的是,代码与另一个webform相同,除了控制名称之外。 – anD666

+0

在这种情况下...不知道该怎么告诉你。使用Fiddler并确保数据得到发布。然后检查代码并确保它不会在其他地方被覆盖/清除。 – Bryan

1

我曾经有同样的问题。我也必须从服务器上的GridView中的TextBox中检索文本值,并且我总是得到一个空字符串。我不得不为Gridview和Textbox设置EnableViewState =“false”,它的功能就像一个魅力。

相关问题