2016-10-17 185 views
0

我有一個問題,我搜索了很多,但沒有工作的答案 問題是:我創建動態GridView,然後添加動態創建的複選框到GridView和其添加但是當IAM試圖獲得複選框狀態(選中與否)我不能達到複選框 請幫助我,感謝這個馬麗娟網站獲取CheckBox在動態創建的GridView中動態創建

Dim MessagesTable As DataTable = Recipient.CreateInboxMessagesTable(LoginName, WhatToShow) 
    Dim Tbl As New DataTable 
    Dim Col As DataColumn 
    Dim Row As DataRow 

    Col = New DataColumn("ID", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("Select", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("Sender", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("Subject", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("ReceiveDate", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("Privacy", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("ImportanceLevel", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("JustDate", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("LastReplyID", GetType(String)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("Status", GetType(Integer)) : Tbl.Columns.Add(Col) 
    Col = New DataColumn("From", GetType(String)) : Tbl.Columns.Add(Col) 
    For Each TRow As DataRow In MessagesTable.Rows 
     Row = Tbl.NewRow 
     Row("ID") = TRow.Item("ID_Rec") 
     'Row("Select") = New CheckBox 
     Row("Sender") = Member.GetUserName(TRow.Item("From_Msg")) 
     Row("Subject") = TRow.Item("Subject_Msg") & " =+-+= " & Message.HTMLToText(TRow.Item("Body_Msg")) 
     Row("ReceiveDate") = Recipient.Convert24TimeTo12(TRow.Item("ReceiveDate_Rec")).Substring(11) 
     Row("Privacy") = TRow.Item("Privacy_Msg") 
     Row("ImportanceLevel") = TRow.Item("ImportanceLevel_Msg") 
     Row("JustDate") = TRow.Item("ReceiveDate_Rec") 
     Row("LastReplyID") = TRow.Item("LastReplyID_Msg") 
     Row("Status") = TRow.Item("Status_Msg") 
     Row("From") = TRow.Item("From_Msg") 
     Tbl.Rows.Add(Row) 
    Next 
    aGrid.DataSource = Tbl 
    aGrid.DataBind() 

這就是我創建下面我怎麼添加的複選框我的網格視圖和代碼

For Each GVRow As GridViewRow In aGrid.Rows 
        Dim CB As New CheckBox 
     GVRow.Cells(1).Controls.Add(CB) 
    Next 

這我如何試圖獲取複選框的狀態,但它並沒有工作

Protected Sub btnCompleted_Click(sender As Object, e As EventArgs) Handles btnCompleted.Click 
    For Each Row As GridViewRow In gvInbox.Rows 
     Dim cbSelect As CheckBox = DirectCast(Message.FindControlRecursive(Row, "CB"), CheckBox) 
     'If cbSelect.Checked = True Then 

     'End If 
    Next 
End Sub 

謝謝

+0

您是否收到任何異常? – Sami

+0

不,因爲我追蹤了代碼,並在它未能取回複選框 – Ali

回答

0

我發現我的問題的解決方案通過獲取控制值將templateField和項目模板添加到HTML代碼GridView,然後它會發現複選框控件

<Columns> 
<asp:TemplateField HeaderText="Select"> 
<ItemTemplate> 
    <asp:checkbox ID="cbSelect" runat="server"/> 
</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
0

所有的代碼看起來不錯。但是你在沒有任何ID的情況下將複選框添加到gridview。

For Each GVRow As GridViewRow In aGrid.Rows 
     Dim CB As New CheckBox 
     CB.ID = "chkRow" // Is the ID for checkbox 
     AddHandler CB.CheckedChanged, AddressOf CheckedChaged // Could add event to the checkbox 
     CB.AutoPostBack = True // make Autopostback to true to handle postback 
     GVRow.Cells(1).Controls.Add(CB) // then add your control to specific position 
Next 

你必須創建複選框實例然後設置屬性(ID,自動回)值,這樣你就可以使用特定的ID

For Each row As GridViewRow In GridView1.Rows 
     If row.RowType = DataControlRowType.DataRow Then 
      Dim chkRow As CheckBox = TryCast(row.Cells(0).FindControl("chkRow"), CheckBox) //same ID to find that control 
      If chkRow.Checked Then 
       //Your Logic 
      End If 
     End If 
    Next 
+0

時停止它,感謝Rojalin Sahoo的回覆。我將ID添加到複選框,但仍無法取回複選框 – Ali