【问题标题】:Status of CheckBox can't be detected无法检测到 CheckBox 的状态
【发布时间】:2013-03-01 12:30:23
【问题描述】:

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

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

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

【问题讨论】:

  • 您确定CheckBox1Cells[0] 中吗?第一列通常有选择之类的按钮。调试并检查您是否在 Controls 集合中看到它
  • 无论我输入什么单元格值,它仍然没有返回任何内容

标签: asp.net mysql vb.net


【解决方案1】:

在这个循环中

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

【讨论】:

  • 不,我刚试过。警报消息不返回任何内容。我仍然无法检索到 id 值
  • 我快绝望了。尝试一切,但它不起作用。最奇怪的是我不明白问题出在哪里。
  • 在按钮时钟上你试试这个... GridView1.Rows.count();
  • 按钮时钟是什么意思?我应该为按钮点击添加一个事件吗?
  • for 循环正在实际工作。如果我放置 Primaryid = gvr.Cells(1).Text 它实际上返回 GridView 中最后一个条目的 id 值
【解决方案2】:

您好,在按钮单击操作中尝试此代码

 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

会有用的

【讨论】:

  • primarylbl 来自哪里?我应该在前面声明吗?
  • 这一行有问题:Dim Primaryid As String = Nothing 错误信息是:“primaryid hides a variable in an enclosure block”
  • 我试过这段代码。效果不好我已经不知道该怎么办了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 1970-01-01
相关资源
最近更新 更多