Dynamic html table : Table « HTML Controls « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DynamicTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DynamicTable : System.Web.UI.Page
{
     protected void Page_Load(object sender, System.EventArgs e)
  {
    HtmlTable table1 = new HtmlTable();

    table1.Border = 1;
    table1.CellPadding = 3;
    table1.CellSpacing = 3;
    table1.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);
      }
      table1.Rows.Add(row);
    }
    this.Controls.Add(table1);
  }
}








4.14.Table
4.14.1.Dynamically create a HTML table (C#)
4.14.2.Dynamic html table
4.14.3.InnerHtml