HtmlTable

HtmlTable.aspx

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

<!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>
        <b>HtmlTable例程<b>
<hr>
        <form id="Form1" runat=server>
        <font face="Verdana" size="-1">
            <p>
            <table id="Table1" CellPadding=5 CellSpacing=0 Border="1" BorderColor="black" runat="server" />
            <p>
            行数:
            <select id="Select1" runat="server">
                <option Value="1">1</option>
                <option Value="2">2</option>
                <option Value="3">3</option>
                <option Value="4">4</option>
                <option Value="5">5</option>
            </select>

            <br>
            列数:
            <select id="Select2" runat="server">
                <option Value="1">1</option>
                <option Value="2">2</option>
                <option Value="3">3</option>
                <option Value="4">4</option>
                <option Value="5">5</option>
            </select>
        
            <input id="Submit1" type="submit" value="生成表" runat="server">

        </font>
        </form>
</body>
</html>

HtmlTable.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 HtmlTable : System.Web.UI.Page
{
        void Page_Load(Object sender, EventArgs e)
        {
            int row = 0;
            int numrows = Int32.Parse(Select1.Value);//将数字的字符串表示形式转换为它的等效 32 位有符号整数
            int numcells = Int32.Parse(Select2.Value);

            for (int j = 0; j < numrows; j++)
            {
                HtmlTableRow r = new HtmlTableRow();//建立HtmlTaleRow对象
                // 设定背景颜色
                if (row % 2 == 1)
                    r.BgColor = "Gainsboro";
                row++;
                for (int i = 0; i < numcells; i++)
                {
                    HtmlTableCell c = new HtmlTableCell();//建立HtmlTanleCell对象
                    c.Controls.Add(new LiteralControl("行 " + j.ToString() + ", 列 " + i.ToString()));
                    r.Cells.Add(c);
                }
                Table1.Rows.Add(r);
            }
        }
}

posted on 2009-04-24 09:44  alon  阅读(310)  评论(0编辑  收藏  举报

导航