我在检测复选框的状态时遇到问题。我有一个Grid View With复选框。广告我需要从选定的行中检索某个值以将其传递给查询。CheckBox的状态无法检测
但问题是,当我遍历GridView线,我无法确定哪个复选框被选中,哪些不是。该过程根本不会输入此条件语句:
Dim chk As CheckBox
....
chk = CType(rowItem.Cells(0).FindControl("CheckBox1"), CheckBox)
If chk.Checked Then
Primaryid &= GridView1.DataKeys(rowItem.RowIndex)("account_id").ToString()
End If
我也试过了这种类型的条件语句。但它不工作,以及:
Dim Chkb As CheckBox = (CType(gvr.FindControl("CheckBox1"), CheckBox))
If Chkb IsNot Nothing AndAlso Chkb.Checked Then
Primaryid = gvr.Cells(0).Text
End If
这是该按钮的VB后端代码的功能,从而触发执行:
Protected Sub RegBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles RegBtn.Click
Dim Primaryid As String = "Initial stage"
Dim chk As CheckBox
For Each rowItem As GridViewRow In GridView1.Rows
chk = CType(rowItem.Cells(0).FindControl("CheckBox1"), CheckBox)
If chk.Checked Then
Primaryid &= GridView1.DataKeys(rowItem.RowIndex)("account_id").ToString()
End If
Next
Dim exmess As String = "alert('" & Primaryid & "')"
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ErrorAlert", exmess, True)
End Sub
这是我如何填充在GridView:
Dim StrQwery As String = "SELECT account_id, account_name bla bla bla"
Dim smd As MySqlCommand
smd = New MySqlCommand(StrQwery, myconn)
smd.CommandType = CommandType.Text
Dim da As New MySqlDataAdapter(smd)
Dim cb As New MySqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
,这是网格视图前面部分的代码:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" textAlign="right" />
</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>
我真的不明白我在做什么错,我应该改变什么来实现结果。如果你能帮助我,我将非常感激。
你肯定'CheckBox1'是'细胞[0]'?第一列通常有像select这样的按钮。调试和检查,如果你看到它在控制集合 – nunespascal 2013-03-01 12:35:48
无论什么细胞价值,我把它仍然没有返回 – meks 2013-03-01 12:56:33