【问题标题】:something wrong with my asp button while adding it to a panel.. ASP & C#将我的 asp 按钮添加到面板时出现问题.. ASP & C#
【发布时间】:2012-10-04 20:17:25
【问题描述】:

我正在创建一个按钮并将其添加到面板中

        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);

我收到了这个错误 'Button' 类型的控件 'ctl01' 必须放在带有 runat=server 的表单标签内。

【问题讨论】:

  • 面板是服务器控件。您无需添加runat="server"。 Button 具有 IdText 属性。
  • 在什么时候将面板添加到页面?
  • 您的问题解决了吗?

标签: c# asp.net


【解决方案1】:

您要添加它的页面需要有这个。而且您的面板必须位于表单标签内。

<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);

【讨论】:

  • 问题是我在后面的代码中创建了整个东西.. 我怎样才能在后面的代码中制作 runat 服务器表单?
  • 你是说你没有要放这个的网页吗?
  • 我有一个网页,但它的所有内容都来自后面的代码,因为面板和按钮取决于数据库结果。我可以改变我用来执行此操作的方式,但我是 ASP 的新手,显然我不知道如何.. :
  • 所有内容是什么意思?你确实有一个 标签,是吗?
  • 我有一个 body 标签 .. 里面有
    我使用 InnerHtml
    从我的后台代码更改 div 内容
【解决方案2】:

使用 Form.Controls.Add(p);

通过使用这个我们可以在表单标签上添加面板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多