我有一個帶有複選框的GridView。但是我在確定給定行的複選框是否被選中時遇到了實際問題。無法確定網格視圖中複選框的狀態
我需要從行中檢索某個值並將其放入代碼中。但是,當我遍歷GridView行時,程序不會輸入檢查checkBox'x狀態的if語句。
這裏是後端代碼:
Dim Primaryid As String = "Initial stage"
For Each gvr As GridViewRow In GridView1.Rows
If (CType(gvr.FindControl("CheckBox1"), CheckBox)).Checked = True Then
Primaryid = gvr.Cells(1).Text
End If
Next gvr
Dim exmess As String = "alert('" & Primaryid & "')"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True)
這裏是GridView控件的代碼。我在頁面加載時自動填充它:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
如果您能指出我的錯誤,我將不勝感激。
我剛剛試過,它仍然沒有得到我所選行的單元格值。它相當於處理所有的(如果我把它放在外面,如果if/else語句),或者如果它在裏面 – meks 2013-02-26 08:35:59
爲什麼yo9u放置'Cells [3]'不是我們檢查總是出現在第一列的複選框的值網格視圖? – meks 2013-02-26 08:39:38
如果你想要選擇的行應該是這樣的:GridView1.SelectedRows。這要求GridView將FullrowSelect作爲選擇模式。 – WozzeC 2013-02-26 08:41:39