无法检测到CheckBox的状态

时间:2013-03-01 12:30:24

标签: asp.net mysql vb.net

我在检测复选框的状态时遇到问题。我有一个Grid View With复选框。 Ad我需要从选定的行中检索某个值以将其传递给查询。

但问题是,当我遍历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 backEnd代码的功能:

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>

我真的无法理解我做错了什么,我应该改变什么以达到结果。如果你能帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:0)

在这个循环中

 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

改变
  chk = CType(rowItem.Cells(0).FindControl("CheckBox1"), CheckBox)

  chk = CType(rowItem.FindControl("CheckBox1"), CheckBox)

这也是不可能的

Dim Chkb As CheckBox = (CType(gvr.FindControl("CheckBox1"), CheckBox))

而不是试试这个

Dim Chkb As CheckBox = (CType(rowItem.FindControl("CheckBox1"), CheckBox))

答案 1 :(得分:0)

您可以在按钮点击操作

中尝试此代码
 For Each rowItem As GridViewRow In GridView1.Rows
    If rowItem.RowType = DataControlRowType.DataRow Then
        Dim chk As CheckBox = TryCast(rowItem.FindControl("CheckBox1"), CheckBox)
        Dim lbl As Label = TryCast(rowItem.FindControl("primarylbl"), Label)

        Dim Primaryid As String = Nothing
        If chk.Checked Then
            Primaryid += lbl.Text
        End If
    End If
Next

它会起作用