Asp.net 类型为'的值;控制';无法转换为';复选框';在vb.net中

Asp.net 类型为'的值;控制';无法转换为';复选框';在vb.net中,asp.net,vb.net,webforms,aspxgridview,Asp.net,Vb.net,Webforms,Aspxgridview,我试图在GridView中使用多个select复选框来删除记录 这是我的密码:- Protected Sub btnDelete_Click(sender As Object, e As EventArgs) Try 'Loop through all the rows in gridview For Each grv As GridViewRow In GridView1.Rows 'Finidin

我试图在GridView中使用多个select复选框来删除记录

这是我的密码:-

Protected Sub btnDelete_Click(sender As Object, e As EventArgs)
        Try
            'Loop through all the rows in gridview
            For Each grv As GridViewRow In GridView1.Rows
                'Finiding checkbox control in gridview for particular row
                Dim chk As CheckBox = CType(grv.FindControl("chkSelect"), CheckBox)
                If chk.Checked Then
                    'get EmpId from DatakeyNames from gridview
                    Dim empid As Integer = Convert.ToInt32(GridView1.DataKeys(grv.RowIndex).Value)
                    cmd = New SqlCommand("DeleteRecord_Sp", con)
                    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = empid
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.ExecuteNonQuery()
                End If
            Next
            GridView1.EditIndex = -1
            DisplayData()
            ScriptManager.RegisterClientScriptBlock(Page, Page.[GetType](), Guid.NewGuid().ToString(), "alert('Selected Records has been deleted successfully');", True)
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " & ex.Message.ToString() & "');", True)
        Finally
            cmd.Dispose()
            con.Close()
        End Try
    End Sub
内部栅格视图:-

              <asp:CheckBox ID="chkSelect" runat="server" />
      </ItemTemplate>
    <asp:Button ID="btnDelete" runat="server" Text="Multiple Delete"
         OnClientClick="return confirm('Are you sure you want to delete selected records?')"
         OnClick="btnDelete_Click" />

无法在vb.net中将“Control”类型的值转换为“CheckBox”

这是因为Ctype返回集合,而不是单个项

您需要DirectCast来转换单个项目。但是,由于您正在将其捕获到一个变量中,所以根本不需要这样做

Dim chk As CheckBox = grv.FindControl("chkSelect")
但您也可以简单地通过它的名称访问它。没有“找到”它,也没有将它捕获到变量中

If ChkSelect.Checked then

end if

这似乎很奇怪,但无论如何你都应该使用
DirectCast
,所以试试吧。我尝试过DirectCast、Ctype、TryCast,但都不起作用,而且我是asp.net的新手。如果你看一下呈现HTML的页面源代码,它看起来像是你想要的吗?是的,它看起来像是这样:Dim chk=DirectCast(grv.FindControl(“chkSelect”),HtmlInputCheckBox)
但不将:-
Dim chk作为复选框=grv.FindControl(“chkSelect”)
If ChkSelect.Checked then

end if