1

(サーバー側で)作成した動的テーブルがあり、colsは(irem、price、pic、textxbox)です。テキストボックスを追加しようとすると、「TextBox」タイプのメッセージコントロール「0」を取得する必要があります。 runat=serverのフォームタグ内

これがコードです(ケース4はテキストボックスを作成する場所です):

  protected void createTable()
{
    int numberOfItems = mylist.Count;



    // Create a new HtmlTable object.
    HtmlTable table1 = new HtmlTable();

    // Set the table's formatting-related properties.
    table1.Border = 1;
    table1.CellPadding = 1;
    table1.CellSpacing = 1;
    table1.BorderColor = "red";

    // Start adding content to the table.
    HtmlTableRow row;
    HtmlTableCell cell;
    for (int i = 0; i < numberOfItems; i++)
    {
        // Create a new row and set its background color.
        row = new HtmlTableRow();
        row.BgColor = "lightyellow";
        row.Height = Convert.ToString(100);


        name = mylist[i].Name;
        price = mylist[i].Price;
        image = mylist[i].ImagePath;




        for (int j = 1; j <= 4; j++)
        {
            // Create a cell and set its text.
            cell = new HtmlTableCell();




            switch (j)
            {
                case 1:
                    cell.InnerHtml = name;
                    break;
                case 2:
                    cell.InnerHtml = Convert.ToString(price);
                    break;
                case 3:
                    cell.InnerHtml = image;
                    break;
                case 4:
                    TextBox txt = new TextBox();
                    txt.ID = Convert.ToString(i);
                    txt.TextMode = TextBoxMode.SingleLine;
                    txt.Attributes.Add("runat", "server");
                    cell.Controls.Add(txt);
                    break;
                default:

                    break;


            }

            // Add the cell to the current row.

            row.Cells.Add(cell);
        }

        // Add the row to the table.
        table1.Rows.Add(row);
    }

    // Add the table to the page.
    this.Controls.Add(table1);

}
4

1 に答える 1

5

あなたの質問に関しては、エラーが言うように、を持っているすべてのコントロールは、タグもrunat=server持っているフォームの中に置かれなければなりません。runat=serverあなたの場合、次の行を使用して、ページのルート要素Textboxとしてテーブル(サーバー側を含む)を追加しています。

this.Controls.Add(table1);

代わりに、タグの要素として追加する必要があります。<form></form>これは、メインフォームにid属性を追加することで簡単に実行できます(たとえば<form runat="server" id="myForm">、aspxページで、次にthis.Controls.Add()使用する代わりに:

myForm.Controls.Add(table1);

または、 aspxページのどこかに空divを置き (例)、そのdivにテーブルを追加します。runat=server
<div id=myDiv runat=server />

myDiv.Controls.Add(table1);
于 2012-12-08T12:39:35.373 に答える