0

我如何遍历数据列表中的每个数据?因为我目前从“Label8”获得一个值,这导致我的“Label7”对所有人显示“否”。

 Protected Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList2.ItemDataBound
    For Each li As DataListItem In DataList2.Items
        Dim labelasd As Label = DirectCast(e.Item.FindControl("**Label8**"), Label)
        Dim reviewid As Integer = labelasd.Text
        Dim connectionString As String = _
        ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim connection As SqlConnection = New SqlConnection(connectionString)
        connection.Open()
        Dim sql As String = "Select Count(reviewYes) AS Expr1 From ProductReviewHelp Where ProductReviewID = " & reviewid & ""
        Dim command As SqlCommand = New SqlCommand(sql, connection)
        Dim reader As SqlDataReader = command.ExecuteReader()
        Dim countofreview As Integer = 0
        Dim reviewcountboolean As Boolean
        If (reader.Read()) Then
            If (IsDBNull(reader.GetValue(0)) = False) Then


                countofreview = reader.GetValue(0)


            End If
        End If
        If countofreview = 0 Then
            reviewcountboolean = False
        Else
            reviewcountboolean = True
        End If

        If (reviewcountboolean = True) Then

            Dim label1 As Label = DirectCast(e.Item.FindControl(**"Label7"**), Label)
            label1.Text = "Hello"

        ElseIf (reviewcountboolean = False) Then

            Dim label1 As Label = DirectCast(e.Item.FindControl(**"Label7"**), Label)
            label1.Text = "No"

        End If
    Next
End Sub

我如何遍历数据列表中的每个数据?因为我目前从“Label8”获得一个值,这导致我的“Label7”对所有人显示“否”。

4

2 回答 2

0

您正在循环 DataList2 项,但是在每个循环中,您都使用当前项的逻辑更新 label7,从而有效地删除了前一个循环的结果。这意味着,当您到达最后一项时,Label7 将反映字符串“Hello”或“No”,具体取决于应用于循环中最后一项的逻辑。

除了这个逻辑错误之外,显示的代码中还有许多错误。

  • 连接永远不会关闭。
  • 您使用字符串连接而不是参数。
  • 当在这种情况下更适合 ExecuteScalar 时,您可以使用 ExecuteReader。
于 2012-08-04T14:09:23.097 回答
0

您可以迭代然后使用循环这里是一个例子

Try
        readFromDL1 = DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
        readFromQ = DirectCast(SqlDataSource7.Select(DataSourceSelectArguments.Empty), DataView)

    Catch ex As Exception

    End Try
    'End 
    'datalist1
    i = 0
    _rowCount = DataList1.Items.Count
    If _rowCount > 0 Then
        _getCall = DataList1.Items.Item(i).FindControl("lnkEdit")
    End If

    For Each readr As DataRowView In readFromQ
        findQNumber = readr(1).ToString()
        For Each readfdlr1 As DataRowView In readFromDL1
            findQNumber1 = readfdlr1(1).ToString

            Try
                indexofitems = DataList1.Items.Item(i1).ItemIndex
                If findQNumber.ToString = findQNumber1.ToString Then
                    _getCall = DataList1.Items.Item(indexofitems).FindControl("lnkEdit")
                    _getCall.Text = "Called"
                    _getCall.Enabled = False
                    _getCall.ForeColor = Drawing.Color.Red
                    _getCall.BackColor = Drawing.Color.Yellow

                End If
                i1 = i1 + 1
            Catch e As Exception

            End Try

        Next
        i1 = 0
        i = i + 1
于 2015-02-04T20:45:04.297 回答