使用 WebMatrix 构建表格 [英] Building tables with WebMatrix

查看:22
本文介绍了使用 WebMatrix 构建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WebMatrix(ASP.NET 网页)构建 HTML 表格,但由于 HTML 标签的打开和关闭方式而遇到问题.

I am trying to build a HTML table using WebMatrix (ASP.NET Web Pages) but am having trouble due to the way the HTML tags are opened and closed.

我想要实现的是从记录集创建一个包含三列的表,然后填充任何空列.

What I am trying to achieve is to create a table from a recordset, with three columns, and then fill in any empty columns.

这是我用来研究如何使用 WebMatrix 执行此操作的一些测试代码.

This is some test code I am using to work out how to do this using WebMatrix.

<table>
@{
 int row = 0;
 int col = 0;
 for (int i = 0; i < 20; i++) //20 cells for test purposes
 {
  col++;
  if (col == 4)
  {
   col = 1;
  }
  if (col == 1)
  {
   row++;
   if (row != 1)
   {
    </tr>
   }
   <tr>
  }
  <td>@i</td>
 }
 for (int i = col; i <=3; i++)
 {
  <td>empty</td>
 }
 </tr>
}
</table>

有关如何最好地完成此任务的任何建议.

Any suggestions for how best to accomplish this.

推荐答案

根据修改后的需求更新代码示例:

Updated code sample based on the revised requirement:

@{
    var db = Database.Open("Northwind");
    var data = db.Query("SELECT * FROM Products");
    var total = data.Count();
    var counter = 1;
    var rows = total / 3;
    var spares = total % 3 > 0 ? 3 - total % 3 : 0;
    if(spares > 0){
        rows += 1;
    }
}
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <table style="border:1px solid black">
            @for(var i = 1; i <= rows; i++){
                if( counter % 3 == 1){
                    @:<tr>
                }
                for(var cell = 1; cell <= 3 && counter <= total; cell++){
                    <td style="border:1px solid black">
                        <div>@data.ElementAt(counter-1).ProductId</div>
                        <div>@data.ElementAt(counter-1).ProductName</div>
                        <div>@data.ElementAt(counter-1).CategoryId</div>
                    </td>
                    counter++;
                }
                if(counter > total && spares > 0){
                    for(var j = 1; j <= spares; j++){
                        <td style="border:1px solid black">
                            &nbsp;
                        </td>
                    }
                }
                if(counter % 3 == 0){
                    @:</tr>
                }
            }
        </table>
    </body>
</html>

希望我明白你在这之后的意思.

Hopefully, I've understood what you are after this time.

如果您想从记录集构建表,为什么不这样做呢?下面是一个使用 Northwind 数据库的示例:

If you want to build the table from a recordset, why not do that? Here's an example using the Northwind database:

<table>
    @foreach(var row in data){
       <tr>
           <td>@row.ProductId</td>
           <td>@row.ProductName</td>
           <td>@row.CategoryId</td>
       </tr>
    }
</table>

或者你可以使用 WebGrid 来做这是给你的.

Or you could use the WebGrid to do this for you.

这篇关于使用 WebMatrix 构建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆