vb.net 在 GridView 文本框 TemplateField 中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19145323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Values in GridView textbox TemplateField
提问by Diego Oliveira
I have an asp.net page called Default.aspx, and it's master page is Site.master. In the Default.aspx, i added a gridview with 3 databound fields and 1 Templatefield, and then, dragged a TextBox inside this templatefield.
我有一个名为 Default.aspx 的 asp.net 页面,它的母版页是 Site.master。在 Default.aspx 中,我添加了一个带有 3 个数据绑定字段和 1 个模板字段的网格视图,然后在这个模板字段中拖动了一个文本框。


I'm trying to get the textbox values for each row in this gridview, using the FindControl method, but it's returning Nothing.
我正在尝试使用 FindControl 方法获取此 gridview 中每一行的文本框值,但它不返回任何内容。
Here is the code that i'm using to retrieve these values:
这是我用来检索这些值的代码:
For Each gvr As GridViewRow In GridView1.Rows
Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Dim txt As String = tb.Text
MsgBox(txt)
Next
Note: I'm using masterPages, and i'm thinking this is causing the problem.
注意:我正在使用 masterPages,我认为这是导致问题的原因。
[edit]
[编辑]
In the Page_load event, to bound the gridview, i'm using the code:
在 Page_load 事件中,为了绑定 gridview,我使用了以下代码:
GridView1.DataSource = f.xDa
GridView1.DataBind()
In the Button1, I've added the code:
在 Button1 中,我添加了代码:
For Each gvr As GridViewRow In GridView1.Rows
Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Dim txt As String = tb.Text
MsgBox(txt)
Next
But i'm Always getting an empty textbox.
但我总是得到一个空的文本框。
Thank's everybody!
谢谢大家!
回答by Josh Darnell
You need to update your Page_Loadcode to this:
您需要将Page_Load代码更新为:
If Not IsPostBack Then
GridView1.DataSource = f.xDa
GridView1.DataBind()
End If
By the time your code gets to the Button_Clickevent, it has already repopulated the GridView with data from your database (overwriting what your user typed into the TextBox).
当您的代码到达Button_Click事件时,它已经用您的数据库中的数据重新填充了 GridView(覆盖了您的用户在 TextBox 中键入的内容)。
The code I've added above causes the data to be loaded only the first time - then the ASP.NET viewstate handles making sure the state of the GridView is kept up-to-date.
我在上面添加的代码只在第一次加载数据 - 然后 ASP.NET viewstate 处理确保 GridView 的状态保持最新。
回答by Jeremiah Allen
I had a similar problem, but my gridview wasn't rendered at Page_Load so I couldn't add the "If Not IsPostBack" bindings to the Page_Load as the SQL dataset I was using wasn't yet declared.
我有一个类似的问题,但我的 gridview 没有在 Page_Load 呈现,所以我无法将“If Not IsPostBack”绑定添加到 Page_Load,因为我使用的 SQL 数据集尚未声明。
Instead of the FindControl and calling your textbox by name, trying something like this might work if you can't use the Page_Load gridview databinding:
如果您不能使用 Page_Load gridview 数据绑定,则尝试使用类似的方法而不是 FindControl 并按名称调用您的文本框:
For Each gvr As GridViewRow In GridView1.Rows
Dim txt As String = CType(gvr.Cells(0).Controls(0), TextBox).Text
MsgBox(txt)
Next
The (0) in the Cells(0) is the column number in your gridview that you are trying to access. So for example If "TextBox1" is the 1st column use Cells(0), if it is the 2nd column use Cells(1) and so on. This allows the text in the textbox to be retrieved without having to add an additional section in the Page_Load
Cells(0) 中的 (0) 是您尝试访问的 gridview 中的列号。例如,如果“TextBox1”是第一列,则使用 Cells(0),如果是第二列,则使用 Cells(1),依此类推。这允许检索文本框中的文本,而无需在 Page_Load 中添加额外的部分

