简体   繁体   English

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

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

I have bound textboxes in a gridview.我在 gridview 中绑定了文本框。 Code to create the gridview cell:创建 gridview 单元的代码:

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

I'm trying to loop through my gridview and get the values in the textboxes.我正在尝试遍历我的 gridview 并获取文本框中的值。 I've tried several variations of this:我已经尝试了几种变体:

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

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

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.谢谢。

The return type of FindControl() is System.Web.UI.Control and it does not have a property with the name Text FindControl() 的返回类型是System.Web.UI.Control并且它没有名为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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM