我在gridField中有一個帶有textBox的gridView。我想提取文本框的文本,如果複選框在行中標記。如何在GridView中找到TemplateField中的控件
我定義爲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控件。有人會看到爲什麼嗎?
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –
感謝您的建議 – Selrac