通过C#创建html表的麻烦

时间:2011-07-19 10:44:59

标签: c# html

我正在尝试通过c#创建一个html表。我打开了一个新的Web应用程序,并在default.aspx.cs文件中输入以下内容

-

    Label text1 = new Label();

    text1.Text = "there are large number of people who were trapped in the avalanche ";

    cell1.Controls.Add(text1);

    rw1.Cells.Add(cell1);

    tbl.Controls.Add(rw1);

    TableRow rw2 = new TableRow();
    TableCell cell2 = new TableCell();

    cell2.Width = 450;

    Label text2 = new Label();

    text2.Text = "the blue wild fox jumped over the fence and ran away never to return again ";
    cell2.Controls.Add(text2);

    rw2.Cells.Add(cell2);
    tbl.Controls.Add(rw2);

}

我的要求是我希望表格的每个单元格都有3种不同的颜色。我在代码中的d中添加了2行2个单元格。它很容易添加1种颜色作为细胞的背景颜色。但是,我想添加3种颜色作为单元格的背景颜色,这样,如果单元格的宽度为100px,则单元格的前30px应为绿色,第二个30px应为黄色,最后40px应为粉红色。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

您可以使用三种颜色的图像作为单元格的背景。

答案 1 :(得分:0)

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;
using System.IO;

protected void Page_Load(object sender, EventArgs e)
{
HtmlTable dTable = new HtmlTable();
dTable.CellPadding = 2;
dTable.CellSpacing = 0;
dTable.Border = 1;
dTable.BorderColor = "#cccccc";



int tRows;
int tCells;

for (tRows = 0; tRows < 5; tRows++)
{
    HtmlTableRow dTRow = new HtmlTableRow();
    for (tCells = 0; tCells < 4; tCells++)
    {
        HtmlTableCell dTCell = new HtmlTableCell();
        dTCell.InnerText = "Row:: " + Convert.ToString(tRows + 1) + " Col:: " + Convert.ToString(tCells + 1);
        dTRow.Controls.Add(dTCell);
    }
    dTable.Controls.Add(dTRow);
}

Panel1.Controls.Add(dTable);
}