ASP.NET 2.0 文件1: Deafault.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>HtmlTable演示</title> </head> <body> <h1>HtmlTable实例演示</h1> <form id="Form1" runat="server"> <table id="MyTable" cellpadding="5" cellspacing="0" border="1" runat="server" > <tbody> </tbody> </table><p> 输入表格行数: <input type="text" id="MyTextOne" value="1" runat="server"/></p> <p>输入表格单元格: <input type="text" id="MyTextTwo" value="1" runat="server" /></p> <p><input id="Submit1" type="submit" value="产生表格" runat="server" onserverclick="Submit1_ServerClick" /></p> </form> </body> </html> 文件2:Deafault.aspx.cs using System; using System.Data; using System.Configuration; 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 _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Submit1_ServerClick(object sender, EventArgs e) { int row = 0; int numrows = Convert.ToInt32(MyTextOne.Value); // 获得文本框中整型数 int numcells = Convert.ToInt32(MyTextTwo.Value); for (int j = 0; j < numrows; j++) { HtmlTableRow r = new HtmlTableRow(); if (row % 2 == 1)// 设置行的背景色 r.BgColor = "Gainsboro"; row++; for (int i = 0; i < numcells; i++) { HtmlTableCell c = new HtmlTableCell(); //创建单元格对象 c.Controls.Add(new LiteralControl("行: " + (j + 1).ToString() + ", 单元格: " +(i + 1).ToString())); r.Cells.Add(c); //添加该单元格对象 } MyTable.Rows.Add(r);//添加行对 } HtmlTableRow r2 = new HtmlTableRow(); HtmlTableCell c2 = new HtmlTableCell(); c2.Controls.Add(new LiteralControl("吉林大学远程教育学院")); c2.ColSpan = 3; //合并单元格,colspan属性可以实现单元格跨越多列 r2.Cells.Add(c2); MyTable.Rows.Add(r2); } }