C#获取GridView中绑定文本框的值(文本) [英] c# Get value (text) of a bound textbox in a gridview
问题描述
我在gridview中绑定了文本框.创建gridview单元的代码:
I have bound textboxes in a gridview. Code to create the gridview cell:
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtWLA" runat="server" Text='<%# Eval("WLA") %>' />
</ItemTemplate>
</asp:TemplateField>
我试图遍历我的gridview并在文本框中获取值.我已经尝试了几种方法:
I'm trying to loop through my gridview and get the values in the textboxes. I've tried several variations of this:
txtValue = row.Cells[1].FindControl("txtWLA").Text; //I've tried "text" and "textbox" too
但是无论我尝试什么,我总是收到一个错误消息,告诉我'text'值是错误的.这是错误:
But no matter what I try, I keep getting an error telling me that the 'text' value is wrong. Here's the error:
'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是获取文本框内容的正确方法.谁能告诉我我在做什么错?谢谢.
I'm baffled. Every resource I try tells me that .Text is the right way to get the contents of a textbox. Can anyone tell me what I'm doing wrong? Thanks.
推荐答案
FindControl()的返回类型为 System.Web.UI.Control
,并且没有名称为文本
The return type of FindControl() is System.Web.UI.Control
and it does not have a property with the name Text
如果您知道实际的类型是什么,只需进行强制转换并访问它即可:
If you know what the actual type is, just cast and access it:
txtValue =(row.Cells [1] .FindControl("txtWLA")as TextBox).Text;
txtValue = (row.Cells[1].FindControl("txtWLA") as TextBox).Text;
这篇关于C#获取GridView中绑定文本框的值(文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!