VB.Net 后端代码中的 HTML 表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18393439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:45:59  来源:igfitidea点击:

HTML Table in VB.Net back end code

asp.netsqlvb.net

提问by user2709389

I have a table that I have created in the VB.net code behind. I have a connection to a database and it returns records to put into my html table. What I'm having issues with is I want to have 5 cells per row and an unlimited amount of rows. Everytime I try and do something that only has the 5 cells per row it's returning something that I don't need. Please help!! Here is the code that I'm using.

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

回答by Karl Anderson

You are looping one extra time, because of this logic:

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

Dim numells As Integer = 6

For j As Integer = 0 To numells - 1

That equates to For j As Integer = 0 To 5, which is 6 iterations instead of 5.

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

Change your declaration to this:

将您的声明更改为:

Dim numells As Integer = 5

Now your Forloop will iterate 5 times instead of 6.

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

UPDATE:

更新:

Your code is not creating a new row on each iteration through the For Each dr As DataRowView In dvloop. You need to create a new HtmlTableRowobject each time, like this:

您的代码不会在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

回答by TLS

There are a couple of things going on here that might be causing problems. First, why are you putting table/tr/tdtags into the InnerHtmlof the Cellobject? The idea behind the ASP.Net Tableis that it will generate all of the necessary HTML for you.

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

Second, you aren't reinitializing the rowvariable after you add it to the tableobject. (You also aren't reinitializing the cellvariable after you add it to the rowobject, but that has no impact because of how the InnerHtmlproperty is being changed in each loop iteration.)

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

To make this work, move row = New HtmlTableRow()down to be the first line after For Each dr As DataRowView In dvso that a new object is created on each iteration.

要完成这项工作,请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()
        ....

Also, as mentioned by Karl Anderson, the loop For j As Integer = 0 To 5actually gives you 6 iterations (0, 1, 2, 3, 4, and 5). This would give you 6 cells instead of the 5 that you want.

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