gpt4 book ai didi

asp.net - 无法检测到 CheckBox 的状态

转载 作者:行者123 更新时间:2023-11-30 23:21:51 27 4
gpt4 key购买 nike

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

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

这是前面部分的 GridView 的代码:

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

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

最佳答案

在这个循环中

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

关于asp.net - 无法检测到 CheckBox 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15157832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com