问题
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>
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
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.
回答1:
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;
来源:https://stackoverflow.com/questions/61036385/c-sharp-get-value-text-of-a-bound-textbox-in-a-gridview