62,271
社区成员
发帖
与我相关
我的任务
分享
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;
namespace WebApp {
public partial class HtmlTableSample : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
NewTableA();
form1.Controls.Add(A);
}
HtmlTable A;
int aRows = 4;
int aCols = 4;
private void NewTableA() {
A = new HtmlTable();
for (int j = 0; j < aRows; j++) {
// Create a new row and add it to the Rows collection.
HtmlTableRow row = new HtmlTableRow();
// Provide a different background color for alternating rows.
if (j % 2 == 1)
row.BgColor = "Gray";
// Iterate through the cells of a row.
for (int i = 0; i < aCols; i++) {
// Create a new cell and add it to the HtmlTableRow
// Cells collection.
HtmlTableCell cell = new HtmlTableCell();
if (i == 0 && j == 0) {
cell.Controls.Add(NewTableB());
}
else {
cell.Controls.Add(new LiteralControl("row " +
j.ToString() +
", cell " +
i.ToString()));
}
row.Cells.Add(cell);
}
// Add the row to the HtmlTable Rows collection.
A.Controls.Add(row);
}
}
private HtmlTable NewTableB() {
int bRows = 2;
int bCols = 2;
HtmlTable B = new HtmlTable();
for (int j = 0; j < bRows; j++) {
// Create a new row and add it to the Rows collection.
HtmlTableRow row = new HtmlTableRow();
// Provide a different background color for alternating rows.
if (j % 2 == 1)
row.BgColor = "Blue";
// Iterate through the cells of a row.
for (int i = 0; i < bCols; i++) {
// Create a new cell and add it to the HtmlTableRow
// Cells collection.
HtmlTableCell cell = new HtmlTableCell();
cell.Controls.Add(new LiteralControl("row " +
j.ToString() +
", cell " +
i.ToString()));
row.Cells.Add(cell);
}
// Add the row to the HtmlTable Rows collection.
B.Controls.Add(row);
}
return B;
}
}
}
HtmlTable A = new HtmlTable();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell htc = new HtmlTableCell();
HtmlTable B = new HtmlTable();
htc.Controls.Add(B);
htr.Cells.Add(htc);
A.Rows.Add(htr);