2013-03-01 54 views
1

我在检测复选框的状态时遇到问题。我有一个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> 

我真的不明白我在做什么错,我应该改变什么来实现结果。如果你能帮助我,我将非常感激。

+1

你肯定'CheckBox1'是'细胞[0]'?第一列通常有像select这样的按钮。调试和检查,如果你看到它在控制集合 – nunespascal 2013-03-01 12:35:48

+0

无论什么细胞价值,我把它仍然没有返回 – meks 2013-03-01 12:56:33

回答

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)) 
+0

如果这有助于你,使它作为答案.... – 2013-03-01 12:36:58

+0

不,我刚刚尝试过。警报消息不返回任何内容。我仍然无法检索ID值 – meks 2013-03-01 12:45:17

+0

我几乎绝望。尝试一切,但它不起作用。最难的是我不明白问题在哪里。 – meks 2013-03-01 12:46:12

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 

这里面的代码,将工作

+0

,其中'primarylbl'来自哪里?我应该在前面声明吗? – meks 2013-03-01 13:33:55

+0

这行有一个问题:'Dim Primaryid As String = Nothing'错误信息是:“primaryid隐藏了一个变量在一个封闭的块中” – meks 2013-03-01 13:38:45

+0

我试过这段代码。不工作以及我不知道该怎么做 – meks 2013-03-01 13:53:13