Question

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.

I have the gridView defined as follows

    <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?

Was it helpful?

Solution

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.

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 Edit mode.

So, you need to get the value of the Label: Label1 here actually, which has the same value and is inside <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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top