[...]类型的控件[...]必须放在带有runat = server的表单标签内 - 但已经是

时间:2016-07-15 19:22:46

标签: asp.net

ASP.Net表示asp:Button需要在<form runat="server"内。它已经在一个表格内。为什么还有错?

  

Default.aspx的:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>
<html>
    <head>
        <title>Test Case</title>
    </head>
    <body>
        <asp:Placeholder runat="server" ID="ph"/>
        <form runat="server">
            <asp:Button runat="server" text="FOOBAR"/>
        </form>
    </body>
</html>
  

Default.aspx.cs:

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ph.Controls.Add(new Button());
    }
}

错误消息:

  

控制&#39; ctl02&#39;类型&#39;按钮&#39;必须放在带有runat = server的表单标记内。

堆栈跟踪:

  

[HttpException(0x80004005):Control&#39; ctl02&#39;类型&#39;按钮&#39;必须放在带有runat = server的表单标记内。]

     

System.Web.UI.Page.VerifyRenderingInServerForm(Control control)+9745742      System.Web.UI.WebControls.Button.AddAttributesToRender(HtmlTextWriter writer)+62      System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)+20      System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)+20      System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)+66      System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter adapter)+100      System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25      System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+128      System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)+13      System.Web.UI.Control.Render(HtmlTextWriter writer)+12      System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)+66      System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter adapter)+100      System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25      System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+128      System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)+13      System.Web.UI.Page.Render(HtmlTextWriter writer)+29      System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter adapter)+66      System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter adapter)+100      System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+5827

3 个答案:

答案 0 :(得分:2)

通过在Page_Load方法中添加另一个按钮,查看(基于您发布的代码)错误。您将其添加到占位符ph的Controls集合中,不在 form标记内。

尝试将Placeholder标记放在form标记内:

<body>
  <form ID="frm1" runat="server">
    <asp:Placeholder runat="server" ID="ph" />
    <asp:Button runat="server" text="FOOBAR" />
</body>

答案 1 :(得分:0)

  

您是否在之后将按钮添加到页面?另外,如果你这样做   没有为您的按钮指定ID,它们将被给予一个   自动以ctl02的形式

试试这个

<!DOCTYPE html>
<html>
    <head>
        <title>Test Case</title>
    </head>
    <body>
        <asp:Placeholder runat="server" ID="ph"/>
        <form ID="frm1" runat="server">
            <asp:Button ID="bt1" runat="server" text="FOOBAR"/>
        </form>
    </body>
</html>

答案 2 :(得分:0)

假设您正在使用带有.ascx文件扩展名的webUsercontrol,并且您创建了一个扩展名为.aspx的webform,您可以在其中使用它来测试或在线运行.ascx模块。通过将.acsx模块从解决方案资源管理器拖到.aspx设计页面,确保将.ascx文件放在水平框内。水平框为您创建默认表单。现在运行.aspx文件,假设您已将其设置为起始页。

谢谢。