c#获取gridview中绑定文本框的值(文本)

c# Get value (text) of a bound textbox in a gridview

我在网格视图中绑定了文本框。创建 gridview 单元格的代码:

<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="txtWLA" runat="server" Text='<%# Eval("WLA") %>' />
  </ItemTemplate>
</asp:TemplateField>

我正在尝试遍历我的 gridview 并获取文本框中的值。我已经尝试了几种变体:

 txtValue =  row.Cells[1].FindControl("txtWLA").Text;   //I've tried "text" and "textbox" too

但无论我尝试什么,我总是收到一条错误消息,告诉我 'text' 值是错误的。这是错误:

'System.Web.UI.Control' does not contain a definition for 'Text' and no 
extension method 'Text' accepting a first argument of type 
'System.Web.UI.Control' could be found

我很困惑。我尝试的每个资源都告诉我 .Text 是获取文本框内容的正确方法。谁能告诉我我做错了什么?谢谢

FindControl() 的 return 类型是 System.Web.UI.Control,它没有名称为 Text

的 属性

如果您知道实际类型是什么,只需转换并访问它:

txtValue = (row.Cells[1].FindControl("txtWLA") as TextBox).Text;