如何在Gridview中的TemplateField中查找控件

时间:2013-10-03 11:47:58

标签: asp.net vb.net gridview templatefield

我在templateField中有一个带有textBox的gridView。如果在行中标记了一个复选框,我想提取textBox的文本。

我将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>

代码如下

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

当我调试代码时,我可以看到找到了Checkbox控件但没有找到TextBox控件。谁会明白为什么?

1 个答案:

答案 0 :(得分:5)

你不能这样做。单击按钮:B_Load时,GridView不处于编辑模式。这就是为什么你无法获得TextBox中的EditItemTemplate

当网格视图处于正常显示模式时,您只能在按钮单击中获取<ItemTemplate>内的控件。仅当GridView进入<EditItemTemplate>模式 时,才会呈现 Edit控件。

因此,您需要实际获取Label的值:Label1,其值相同且位于<ItemTemplate>内。

 Dim Lbl_Control As Label= CType(row.FindControl("Label1"), Label)

//按钮像往常一样单击,只需获取并检查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