2013-10-03 53 views
1

我在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控件。有人會看到爲什麼嗎?

+1

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

感謝您的建議 – Selrac

回答

5

你不能這樣做。當你點擊按鈕:B_Load,那麼GridView不在編輯模式。這就是爲什麼你不能得到TextBox,這是在EditItemTemplate

由於gridview處於正常顯示模式,因此只能在點擊按鈕時獲取<ItemTemplate>中的控件。 <EditItemTemplate>控件僅在GridView進入Edit模式時纔會呈現

因此,您需要在此處獲得標籤的值: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 
+3

您還可以檢查行是否處於編輯狀態如果row.RowState = DataControlRowState.Edit然後 –

+0

完美!這已經整理了問題。太感謝了!! – Selrac

相關問題