using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
public partial class Chapter04_CreateTableByCode : System.Web.UI.Page
{
    // 使用Html服务器控件创建一个5行4列的表格
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlTable table = new HtmlTable();
        table.Border = 1;
        table.CellSpacing = 3;
        table.CellPadding = 3;
        table.BorderColor = "red";
 
        HtmlTableRow row;
        HtmlTableCell cell;
        for (int i = 1; i <= 5; i++)
        {
            row = new HtmlTableRow();
            row.BgColor = i % 2 == 0 ? "lightyellow" : "lightcyan";
            for (int j = 1; j <= 4; j++)
            {
                cell = new HtmlTableCell();
                cell.InnerHtml = "Row : " + i.ToString() + "<br />Cell: " + j.ToString();
                row.Cells.Add(cell);
            }
            table.Rows.Add(row);
        }
        this.Controls.Add(table);
    }
}

效果:

使用HtmlControl动态创建一个表格

相关文章:

  • 2021-08-10
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2021-08-15
猜你喜欢
  • 2021-07-10
  • 2021-11-30
  • 2021-09-15
  • 2021-06-26
  • 2021-07-20
相关资源
相似解决方案