vb.net 如何在 Gridview 中的 TemplateField 中找到控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19158413/
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
How to find a control inside a TemplateField in Gridview
提问by Selrac
I have a gridView with a textBox inside a templateField. I wan to extract the text of the textBox if a checkbox is marked in the row.
我在模板字段中有一个带有文本框的 gridView。如果行中标记了复选框,我想提取 textBox 的文本。
I have the gridView defined as follows
我有 gridView 定义如下
<asp:GridView ID="GV_Comments" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SQL_Comments">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="Comment_Select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:TemplateField HeaderText="comment" SortExpression="comment">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("comment") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="CommentForPeriod" runat="server" Text='<%# Bind("comment") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="B_Load" runat="server" Text="Transfer Selection" onclick="B_Load_Click" />
<br />
<asp:TextBox ID="CompiledText" runat="server" Width="662px" Rows="10"
TextMode="MultiLine"></asp:TextBox>
And the code as follows
代码如下
Protected Sub B_Load_Click(ByVal sender As Object, ByVal e As EventArgs) '(sender As Object, e As System.EventArgs) Handles B_Load.Click
Dim FullText As String = ""
For Each row As GridViewRow In GV_Comments.Rows
Dim CB_Control As CheckBox = CType(row.FindControl("Comment_Select"), CheckBox)
Dim Txt_Control As TextBox = CType(row.FindControl("CommentForPeriod"), TextBox)
If CB_Control IsNot Nothing AndAlso CB_Control.Checked AndAlso Txt_Control IsNot Nothing Then
FullText = FullText & Txt_Control.Text & "<br/>"
End If
Next row
CompiledText.Text = FullText.ToString
End Sub
When I debug the code I can see that the Checkbox control is found but not the TextBox control. Would anyone see why?
当我调试代码时,我可以看到找到了 Checkbox 控件,但没有找到 TextBox 控件。有人会明白为什么吗?
回答by R.C
You can't do like this. When you click the button: B_Load, then GridView is NOT in Edit mode. And this is why you can't get the TextBox, which is in EditItemTemplate.
你不能这样做。当您单击按钮时:B_Load,则 GridView 不处于编辑模式。这就是为什么你不能得到的TextBox,这是在EditItemTemplate。
You can only get the controls inside <ItemTemplate>in your button click as gridview is in Normal display Mode. <EditItemTemplate>controls are rendered only when GridView enters Editmode.
<ItemTemplate>由于 gridview 处于正常显示模式,因此您只能在按钮单击中获取控件。<EditItemTemplate>控件仅在 GridView 进入Editmode时呈现。
So, you need to get the value of the Label: Label1here actually, which has the same value and is inside <ItemTemplate>.
因此,您需要获取 Label: 的值:Label1实际上,它具有相同的值并且在<ItemTemplate>.
Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)
// button click as usual, just get and check the value of Label control, rather than TextBox control.
// 按钮点击照常,只是获取并检查Label控件的值,而不是TextBox控件。
Protected Sub B_Load_Click(ByVal sender As Object, ByVal e As EventArgs) '(sender As
Object, e As System.EventArgs) Handles B_Load.Click
Dim FullText As String = ""
For Each row As GridViewRow In GV_Comments.Rows
Dim CB_Control As CheckBox = CType(row.FindControl("Comment_Select"),
CheckBox)
Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)
If CB_Control IsNot Nothing AndAlso CB_Control.Checked AndAlso Lbl_Control
IsNot Nothing Then
FullText = FullText & Lbl_Control.Text & "<br/>"
End If
Next row
CompiledText.Text = FullText.ToString
End Sub

