我有一个数据源和一个用VB.net代码用HTML创建的表。我已经为表创建了单元格,但是我遇到了创建多个单元格和计数器的问题。我希望每行有六个单元格,行数不受限制。我知道我必须添加一个计数器来跟踪我添加了多少个单元格,但我不知道该怎么做。我也只使用以下代码为我的表获取一个单元格。请帮忙,让我知道我没有加入。
Dim strTable as New StringBuilder()
strTable.Append("<table Style='border:2;border-width: 1px;'>")
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()
strTable.Append("<tr>")
For i As Integer = 5 - 1 To 0 Step -1
strTable.Append(String.Format("<td>'<a href=Breakdown.aspx?p=" + crossover + "'</a>"))
strTable.Append(String.Format("<img src='Images/" + picid + ".png' width='100' height='100'<br/>"))
strTable.Append(String.Format(picdescrip + "</td>"))
Next
strTable.Append("</tr>")
Next
strTable.Append("</table>")
divTable.InnerHtml = Convert.ToString(strTable)
End Sub
答案 0 :(得分:1)
以下几行:
Dim strTable as New StringBuilder()
strTable.Append("<table Style='border:2;border-width: 1px;'>")
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()
strTable.Append("<tr>")
For j As Integer = 0 To 4
strTable.Append(String.Format("<td>{0}</td>"), crossover))
Next
strTable.Append("</tr>")
Next
strTable.Append("</table>")
divTable.InnerHtml = strTable
请注意以下事项:
StringBuilder.Append()
次调用添加它们。答案 1 :(得分:0)
在你的例子中,我不明白为什么你需要跟踪细胞数量,因为现在你只需要添加一个包含来自交叉数据的细胞,而细胞是基于名称的巫术意味着你将需要输入它们。
没有strTable声明,但是多次添加表格,以下是我要开始的内容:
' If dv.Table.Rows.Count > 0 Then - Not needed since we will get no data from For Each '
Dim sbTable as New System.Text.StringBuilder() ' Faster then concating many strings '
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()
sbTable.Append("<tr><td>" & crossover & "</td></tr>") ' Add one line, Separate <tr>, <td> & data & </td>, </tr> to separate lines to add multiple cells '
Next
If sbTable.Length > 0 Then divTable.InnerHtml = "<table style=""border:2;border-width: 1px;"">" & sbTable.ToString() & "</table>"
如果多次添加,StringBuilder比String更快 使用&amp;要在VB.NET中添加字符串,这可以确保将其添加为字符串而不是其他内容。
编辑:确定所以你想要一个网格,你不清楚你想要输出的方式,因为你的例子中生成的HTML无效并且在几个地方被剪切掉了。但无论如何我会尝试一下。 不幸的是,VB注释的标记在StackOverflow上无法正常工作('注释结束时可以解决这个问题。)
Dim strTable as New StringBuilder()
Dim itmCounter As Integer = 0
Dim rowIsOpen As Boolean = False
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()
' For every 5 items create a new row. '
If itmCounter Mod 5 = 0 Then
' Since we want new row, first close any open row '
If rowIsOpen Then strTable.Append("</tr>")
' Start a new row and mark row as open so we can keep track '
strTable.Append("<tr>")
rowIsOpen = True
End If
strTable.Append("<td><a href=""Breakdown.aspx?p=" & crossover & """>")
strTable.Append("<img src=""Images/" + picid + ".png"" width=""100"" height=""100"" /><br />"))
strTable.Append(picdescrip & "</a></td>")
itmCounter += 1
Next
' Make sure we close any open rows '
If rowIsOpen Then strTable.Append("</tr>")
If sbTable.Length > 0 Then
divTable.InnerHtml = "<table style=""border:2;border-width: 1px;"">" & _
sbTable.ToString() & _
"</table>"
End If
If itmCounter Mod 5 = 0 Then
部分用于每5个项目关闭并打开一个新的tr
。
这是必需的,因为For Each dr
是更改项目的内容。 (而不是循环同一个项目5次)
我还确保HTML是有效的,但是您可能仍然希望在值中添加某种htmlEncode,以确保不会从不正确的值生成无效的html。 (双“”在结果中只输出一个“