0

I have a gridview which is populated with UserId, serviceNo, forename and surname on page load. The page has an id which is a querystring (NewTrain) which i bulk insert into my DB table along with the different users (UserId). This I can already do and works. However what I would now like to do is only insert the rows of the GridView were a checkbox is checked!

I have been trying thid for a while and have continually run into problems.

I have tried two different scenerios but have encountered the same results

can anybody please help?

1.

    Protected Sub btnAttend_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim myTrain As String = Request.QueryString("NewTrainId")


        If myTrain IsNot Nothing Then
            Dim strQuery As String
            Dim cmd As SqlCommand


            For x As Integer = 0 To GridView1.Rows.Count - 1
                Dim chkBox As CheckBox = DirectCast(GridView1.Rows(x).FindControl("RowLevelCheckBox"), CheckBox)

                If chkBox.Checked Then
                    Dim lblUsr2 As HtmlInputHidden = CType(GridView1.Rows(x).FindControl("txtUsr"), HtmlInputHidden)
                    Dim userID As Guid = New Guid(lblUsr2.Value().ToString)
                    Dim HidTrainId As HtmlInputHidden = FindControlRecursive(MultiTabs, "txtTrainId")

                    strQuery = "Insert into userAssessmentTbl(tt_id, UserId)values(@NewTrainId, @UserId)"

                    cmd = New SqlCommand(strQuery)
                    cmd.Parameters.AddWithValue("@UserId", userID)
                    cmd.Parameters.AddWithValue("@NewTrainId", myTrain)

                    InsertUpdateData(cmd)


                End If

            Next
        End If


    End Sub

2.

Protected Sub btnAttend_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim myTrain As String = Request.QueryString("NewTrainId")


        If myTrain IsNot Nothing Then
            Dim strQuery As String
            Dim cmd As SqlCommand


            For Each rowItem As GridViewRow In GridView1.Rows


                Dim chkBox As CheckBox = DirectCast(rowItem.FindControl("RowLevelCheckBox"), CheckBox)
                If chkBox IsNot Nothing AndAlso chkBox.Checked Then

                    Dim lblUsr2 As HtmlInputHidden = CType(rowItem.FindControl("txtUsr"), HtmlInputHidden)
                    Dim userID As Guid = New Guid(lblUsr2.Value().ToString)
                    Dim HidTrainId As HtmlInputHidden = FindControlRecursive(MultiTabs, "txtTrainId")

                    strQuery = "Insert into userAssessmentTbl(tt_id, UserId)values(@NewTrainId, @UserId)"

                    cmd = New SqlCommand(strQuery)
                    cmd.Parameters.AddWithValue("@UserId", userID)
                    cmd.Parameters.AddWithValue("@NewTrainId", myTrain)

                    InsertUpdateData(cmd)

                End If

            Next
        End If


    End Sub
4

1 回答 1

0

这是在 C# 中,但您可以将其转换为 vb.net:

            foreach ( ListItem li in your_ check_box_list.Items )
            {


                if (li.Selected == true)
                {
                    // execute code 
                }

            }

我在 vb.net 中发现了这个,不确定它是否确实有效:

For Each selecteditem As [Object] In Listbx.SelectedItems
//Your code on each item
Next
于 2012-12-21T18:44:26.557 回答