-2

我有一个我在后面的 VB.net 代码中创建的表。我有一个到数据库的连接,它返回记录以放入我的 html 表中。我遇到的问题是我希望每行有 5 个单元格和无限数量的行。每次我尝试做每行只有 5 个单元格的事情时,它都会返回我不需要的东西。请帮忙!!这是我正在使用的代码。

Protected Sub LoadProducts()
    Dim con5 As New SqlConnection
    Dim cmd5 As New SqlCommand
    Dim index As Integer = 0

    con5.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
    con5.Open()
    cmd5.Connection = con5
    cmd5.CommandType = CommandType.StoredProcedure
    cmd5.CommandText = "ProductTypesSearch"

    Dim dt1 As New DataSet
    cmd5.Parameters.Add(New SqlParameter("ProductID", SqlDbType.Int)).Value = 1
    cmd5.Parameters.Add(New SqlParameter("CollectionID", SqlDbType.Int)).Value = 2

    Dim da1 As SqlDataAdapter = New SqlDataAdapter(cmd5)
    da1.Fill(dt1)
    Dim cell As HtmlTableCell
    Dim row As HtmlTableRow
    Dim table1 As New HtmlTable
    row = New HtmlTableRow()
    Dim numells As Integer = 6

    dv = New DataView(dt1.Tables(0))
    Dim tablestring = ""
    If dv.Table.Rows.Count > 0 Then
        For Each dr As DataRowView In dv
            Dim crossover As String = dr("CrossoverID").ToString()
            Dim picid As String = dr("Description").ToString()
            Dim picdescrip As String = dr("DesignColor").ToString()
            cell = New HtmlTableCell()

            For j As Integer = 0 To 5
                cell.InnerHtml = "<table width-'100%'>"
                cell.InnerHtml += "<tr><td><div class='product'><td><a href='ProductBreakdown2.aspx?p=" + crossover + "'</a>"
                cell.InnerHtml += "<div class='product'><img src='Images/WebsiteProductImages/" + picid + ".png' width='100' height='100'>"
                cell.InnerHtml += "<div class = 'test'>" + picdescrip
                cell.InnerHtml += "</td></tr></div></div></table>"

                row.Cells.Add(cell)
            Next

            table1.Rows.Add(row)
        Next

        Me.Controls.Add(table1)
    End If
End Sub
4

2 回答 2

0

这里有几件事可能会导致问题。首先,你为什么要把table//标签放入对象中?ASP.Net 背后的想法是它将为您生成所有必要的 HTML。trtdInnerHtmlCellTable

其次,在将row变量添加到table对象后,您不会重新初始化它。cell(在将变量添加到对象后,您也不会重新初始化该变量row,但这没有影响,因为InnerHtml在每次循环迭代中如何更改属性。)

为了使这项工作,row = New HtmlTableRow()向下移动到第一行,For Each dr As DataRowView In dv以便在每次迭代时创建一个新对象。

....
Dim table1 As New HtmlTable
Dim numells As Integer = 6

dv = New DataView(dt1.Tables(0))
Dim tablestring = ""
If dv.Table.Rows.Count > 0 Then
    For Each dr As DataRowView In dv
        row = New HtmlTableRow()
        Dim crossover As String = dr("CrossoverID").ToString()
        ....

此外,正如 Karl Anderson 所提到的,循环For j As Integer = 0 To 5实际上为您提供了 6 次迭代(0、1、2、3、4 和 5)。这会给你 6 个细胞而不是你想要的 5 个。

于 2013-08-23T12:39:43.583 回答
0

由于以下逻辑,您正在循环一次额外的时间:

Dim numells As Integer = 6

For j As Integer = 0 To numells - 1

这相当于For j As Integer = 0 To 5,即 6 次迭代而不是 5 次。

将您的声明更改为:

Dim numells As Integer = 5

现在您的For循环将迭代 5 次而不是 6 次。

更新:

您的代码不会在循环的每次迭代中创建新行For Each dr As DataRowView In dvHtmlTableRow您每次都需要创建一个新对象,如下所示:

For Each dr As DataRowView In dv
    ' Each time through this loop we need a new row object
    row = New HtmlTableRow()

    Dim crossover As String = dr("CrossoverID").ToString()
    Dim picid As String = dr("Description").ToString()
    Dim picdescrip As String = dr("DesignColor").ToString()
    cell = New HtmlTableCell()

    For j As Integer = 0 To 5
        cell.InnerHtml = "<table width-'100%'>"
        cell.InnerHtml += "<tr><td><div class='product'><td><a href='ProductBreakdown2.aspx?p=" + crossover + "'</a>"
        cell.InnerHtml += "<div class='product'><img src='Images/WebsiteProductImages/" + picid + ".png' width='100' height='100'>"
        cell.InnerHtml += "<div class = 'test'>" + picdescrip
        cell.InnerHtml += "</td></tr></div></div></table>"

        row.Cells.Add(cell)
    Next

    table1.Rows.Add(row)
Next
于 2013-08-23T02:33:01.133 回答