在ASP.NET中利用HtmlTable動態建立表格

來源:互聯網
上載者:User

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.csusing 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);        }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.