5
<table id="tableContent" border="1" runat="server">
    <tr>
        <td colspan="3">
        Record 1
        </td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>
    <tr>
        <td>m</td>
        <td>n</td>
        <td>o</td>
    </tr>
    <tr>
        <td colspan="3">
            <input id="Button1" type="button" value="button" />
        </td>
    </tr>
</table>

上記のテーブルを c# で動的に作成する必要がありますが、試していますが取得できませんでした

protected void Page_Load(object sender, EventArgs e)
{

    HtmlTableRow row = null;
    HtmlTableCell cell = null;

    for(int i = 0; i < 5; i++)
    {
        row = new HtmlTableRow();
        cell = new HtmlTableCell();
        tableContent.Controls.AddAt(i, row);
        row.Controls.AddAt(i, cell);
        cell.InnerText="1";
    }
}
4

3 に答える 3

9

このコードを試して、テーブルを作成できます。

まず、このマークアップを aspx ページに次のように配置します

<table id="tableContent" border="1" runat="server"></table>

次に、このコードを Page_Load のように試してください

protected void Page_Load(object sender, EventArgs e)
{
    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();

    cell.ColSpan =3;
    cell.InnerText = "Record 1";
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    cell.InnerText = "1";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "2";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "3";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    cell.InnerText = "a";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "b";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "c";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);


    row = new HtmlTableRow();
    cell = new HtmlTableCell();
    cell.InnerText = "m";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "n";
    row.Cells.Add(cell);

    cell = new HtmlTableCell();
    cell.InnerText = "o";
    row.Cells.Add(cell);

    tableContent.Rows.Add(row);

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    HtmlInputButton input = new HtmlInputButton();
    input.ID = "Button1";
    input.Value = "button";

    cell.ColSpan = 3;
    cell.Controls.Add(input);
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);
}

または、次のようにセル値を 2D 配列に格納することで、これを試すことができます

protected void Page_Load(object sender, EventArgs e)
{
    String[,] cellValues = { { "1", "2", "3" }, { "a", "b", "c" }, { "m", "n", "o" } };

    HtmlTableRow row = new HtmlTableRow();
    HtmlTableCell cell = new HtmlTableCell();

    cell.ColSpan = 3;
    cell.InnerText = "Record 1";
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);

    for (int i = 0; i < cellValues.GetLength(0); i++)
    {
        row = new HtmlTableRow();
        for (int j = 0; j < cellValues.GetLength(1); j++)
        {
            cell = new HtmlTableCell();
            cell.InnerText = cellValues[i, j];
            row.Cells.Add(cell);
        }
        tableContent.Rows.Add(row);
    }

    row = new HtmlTableRow();
    cell = new HtmlTableCell();

    HtmlInputButton input = new HtmlInputButton();
    input.ID = "Button1";
    input.Value = "button";

    cell.ColSpan = 3;
    cell.Controls.Add(input);
    row.Cells.Add(cell);
    tableContent.Rows.Add(row);
}
于 2012-10-28T07:41:57.860 に答える