在.net中利用Web控件Table动态生成表格
.aspx 文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="t1.aspx.cs" Inherits="t1" %>
<!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>无标题页</title>
</head>
<body>
<form method="post" runat="server">
<asp:Label style="Z-INDEX: 101; LEFT: 41px; ; TOP: 33px" runat="server">Asp.Net Table控件动态生成表格操作实例</asp:Label>
<asp:Table style="Z-INDEX: 104; LEFT: 35px; ; TOP: 89px" runat="server" GridLines="Both" BorderColor="#FFC080" BorderStyle="Dotted" BorderWidth="1px" CellPadding="0" CellSpacing="0" Height="153px" Width="344px">
</asp:Table>
</form>
</body>
</html>
.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 t1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int I, J;
for (I = 0; I < 6; I++)//6行
{
TableRow Row = new TableRow();
for (J = 0; J < 4; J++)//4列
{
TableCell Cell = new TableCell();
Cell.Text = "Column=" + J.ToString();
if (J == 3)//第4列添加按钮控件
{
Button btnA = new Button();
btnA.Text = "Column=3";
Cell.Controls.Add(btnA);
}
Row.Cells.Add(Cell);
}
Table1.Rows.Add(Row);
}
}
}
效果图: