我有一个gridview,在页面加载时填充了UserId,serviceNo,forename和surname。该页面的id是一个查询字符串(NewTrain),它与不同的用户(UserId)一起批量插入到我的数据库表中。这我已经可以做了并且有效。但是我现在想要做的只是插入GridView的行,我们选中了一个复选框!
我已经尝试了一段时间并不断遇到问题。
我尝试了两种不同的场景,但遇到了相同的结果
任何人都可以帮忙吗?
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
答案 0 :(得分: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