1

我在检测复选框的状态时遇到问题。我有一个带有复选框的网格视图。广告我需要从选定的行中检索某个值以将其传递给查询。

但问题是当我遍历 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>

我真的不明白我做错了什么以及我应该改变什么来达到结果。如果您能帮助我,将不胜感激。

4

2 回答 2

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

它会起作用的

于 2013-03-01T13:18:25.453 回答
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))
于 2013-03-01T12:33:49.450 回答