Private Sub GetSelectedBasketRecord()
Dim j As Integer = 0
ViewState("SelectedBasket") = Nothing
For i As Integer = 0 To gvSapBaskets.Rows.Count - 1
Dim rb As CheckBox = DirectCast(gvSapBaskets.Rows(i).Cells(0).FindControl("CHKButton1"), CheckBox)
If rb IsNot Nothing Then
If rb.Checked Then
Dim hf As HiddenField = DirectCast(gvSapBaskets.Rows(i).Cells(0).FindControl("HiddenFieldBasketID"), HiddenField)
If hf IsNot Nothing Then
j = j + 1
If j = 1 Then
ViewState("SelectedBasket") = "PositionID = " & hf.Value & " "
Else
ViewState("SelectedBasket") = ViewState("SelectedBasket") & "AND PositionID = " & hf.Value & " "
End If
BasketIDLBL.Text = ViewState("SelectedBasket")
End If
If i = gvSapBaskets.Rows.Count - 1 Then
Exit For
End If
End If
End If
Next
End Sub
我在gridview中有复选框,
目前我打算做的是创建一个SQL查询,根据用户在gridview中选择的内容返回行,viewstate将生成SQL查询的一部分
PositionID = 1234
PositionID = 1234 AND PositionID = 5678
然后我将生成像
这样的SQL "SELECT * FROM TABLE WHERE " & ViewState("SelectedBasket") & "
ViewState(" SelectedBasket")类似于PositionID = 1234 AND PositionID = 5678
我有一种感觉可能有更好的解决方法,只是想不到它
非常感谢任何帮助!
答案 0 :(得分:1)
你可能是指OR而不是AND。
但更简单的方法是使用IN:
SELECT * FROM TABLE WHERE PositionID IN (1234, 5678)
答案 1 :(得分:0)
我认为这可能就是你要找的东西:
Private Sub GetSelectedBasketRecord()
ViewState("SelectedBasket") = ""
For Each gvRow in gvSapBaskets.Rows
Dim rb As CheckBox = DirectCast(gvRow.FindControl("CHKButton1"), CheckBox)
If rb IsNot Nothing Then
If rb.Checked Then
Dim hf As HiddenField = DirectCast(gvRow.FindControl("HiddenFieldBasketID"), HiddenField)
If hf IsNot Nothing Then
ViewState("SelectedBasket") &= ViewState("SelectedBasket") & ", "
End If
End If
End If
Next
If ViewState("SelectedBasket") <> "" Then
ViewState("SelectedBasket") = Left(ViewState("SelectedBasket"), LEN(ViewState("SelectedBasket"))-2)
ViewState("SelectedBasket") = "In (" & ViewState("SelectedBasket") & ")"
End If
End Sub