VB.Net后端代码中的HTML表

时间:2013-08-23 01:53:28

标签: asp.net sql vb.net

我有一个我在后面的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

2 个答案:

答案 0 :(得分: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 dv循环在每次迭代中创建新行。您需要每次都创建一个新的HtmlTableRow对象,如下所示:

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

答案 1 :(得分:0)

这里有一些可能导致问题的事情。首先,为什么要将table / tr / td个标记放入InnerHtml对象的Cell? ASP.Net Table背后的想法是,它将为您生成所有必要的HTML。

其次,在将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个单元格。