0

ボタンを作成してパネルに追加しています

        Panel p = new Panel();
        p.ID = "b_con";
        p.Attributes.Add("runat", "server");
        this.Controls.Add(p);
        Button b = new Button();
        b.Attributes.Add("value", "reply");
        b.Attributes.Add("id", Convert.ToInt32(r["Message_ID"]).ToString());
        b.Attributes.Add("class", "button");
        b.Click += new System.EventHandler(button_Click);
        p.Controls.Add(b);

このエラーが発生しました Control 'ctl01' of type 'Button' must be place within a form tag with runat=server.

4

2 に答える 2

3

追加するページには、これが必要です。また、パネルはフォーム タグ内に配置する必要があります。

<body>
<form runat="server">

<!-- Panel must be added within here -->
<div id="messages_con">
    <asp:Panel id="b_con" runat="server" />
</div>

</form>
</body>

したがって、追加するパネルは Html コードにある必要があります。ページに存在しないパネルにボタンを追加しようとしています。代わりに、次のように Html でパネルを参照します (上記を参照)。

Panel p = b_con;
//p.ID = "b_con";
//p.Attributes.Add("runat", "server"); -- Not necessary
this.Controls.Add(p);
Button b = new Button();
b.Attributes.Add("value", "reply");
b.Attributes.Add("id", Convert.ToInt32(r["Message_ID"]).ToString());
b.Attributes.Add("class", "button");
b.Click += new System.EventHandler(button_Click);
p.Controls.Add(b);
于 2012-10-04T20:18:34.893 に答える
0

Form.Controls.Add(p); を使用します。

これを使用してフォームタグにパネルを追加できます。

于 2012-10-05T06:48:40.850 に答える