我正在使用visual basic成功循环遍历数组以填充asp.net中的表。我想在填充计数器当前位置的下一行之前,用数组中的前9个项填充第一行。我使用vb只有2天,我的当前循环使用索引0 - 9填充每一行,但理想情况下要填充从10 - 18开始的下一行,依此类推。在C#中,我使用下面的代码完成了这个:
<div class="col-md-10 col-md-offset-1">
<h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3>
<table id="myTable" class="tablesorter" style="width:100%">
<thead class="finger_hover">
<tr>
<th>Sensor</th>
<th>Date Time</th>
<th>Source IP</th>
<th>Source Port</th>
<th>Destination IP</th>
<th>Destination Port</th>
<th>Protocol</th>
<th>Signature</th>
<th>Signature Class</th>
<th>Count</th>
</tr>
</thead>
<tbody>
@{
int i;
for (i = 0; i < alertForTheDay.Count; i++)
{
@:<tr>
@:
<td>@alertForTheDay[i]</td>
@:
<td>@alertForTheDay[i + 1]</td>
@:
<td>@alertForTheDay[i + 2]</td>
@:
<td>@alertForTheDay[i + 3]</td>
@:
<td>@alertForTheDay[i + 4]</td>
@:
<td>@alertForTheDay[i + 5]</td>
@:
<td>@alertForTheDay[i + 6]</td>
@:
<td>@alertForTheDay[i + 7]</td>
@:
<td>@alertForTheDay[i + 8]</td>
@:
<td>@alertForTheDay[i + 9]</td>
@:</tr>
i = i + 9;
}
}
</tbody>
</table>
</div>
此代码使用数组中的下一组项填充下一行。这是vb的代码:
<div class="col-md-10 col-md-offset-1">
<h3 style="text-align:center; padding: 0px;">List of Alerts for Today</h3>
<table id="myTable" class="tablesorter" style="width:100%">
<thead class="finger_hover">
<tr>
<th>Sensor</th>
<th>Date Time</th>
<th>Source IP</th>
<th>Source Port</th>
<th>Destination IP</th>
<th>Destination Port</th>
<th>Protocol</th>
<th>Signature</th>
<th>Signature Class</th>
<th>Count</th>
</tr>
</thead>
<tbody>
@code
Dim increment As Integer = 9
Dim i As Integer
For i = 0 To alertForTheDay.Count Step i + 1
@:<tr>
@:<td>@alertForTheDay(0)</td>
@:<td>@alertForTheDay(1)</td>
@:<td>@alertForTheDay(2)</td>
@:<td>@alertForTheDay(3)</td>
@:<td>@alertForTheDay(4)</td>
@:<td>@alertForTheDay(5)</td>
@:<td>@alertForTheDay(6)</td>
@:<td>@alertForTheDay(7)</td>
@:<td>@alertForTheDay(8)</td>
@:<td>@alertForTheDay(9)</td>
@:</tr>
i = i + 9
Next
End code
</tbody>
</table>
</div>
vb代码使用精确索引填充单元格,即使我单步执行代码并且数字发生更改,单元格也始终使用引用的精确值填充。我相信有一种简单的方法可以做到这一点。请任何帮助表示赞赏。谢谢
答案 0 :(得分:1)
只需将循环更改为10并使用循环的索引器从数组中检索值
For i = 0 To alertForTheDay.Count -1 Step 10
@:<tr>
@:<td>@alertForTheDay(i)</td>
@:<td>@alertForTheDay(i+1)</td>
@:<td>@alertForTheDay(i+2)</td>
@:<td>@alertForTheDay(i+3)</td>
@:<td>@alertForTheDay(i+4)</td>
@:<td>@alertForTheDay(i+5)</td>
@:<td>@alertForTheDay(i+6)</td>
@:<td>@alertForTheDay(i+7)</td>
@:<td>@alertForTheDay(i+8)</td>
@:<td>@alertForTheDay(i+9)</td>
@:</tr>
Next
当然这假设你alertForTheDay包含许多元素总是10的倍数
我已修复的内容: