Control […] of type […] must be placed inside a form tag with runat=server – but already is

asp.net

ASP.Net says that asp:Button needs to be inside a <form runat="server". It already is inside a form. Why does it still wrong?


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

Error message:

Control 'ctl02' of type 'Button' must be placed inside a form tag with runat=server.

Stack trace:

[HttpException (0x80004005): Control 'ctl02' of type 'Button' must be placed inside a form tag with 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

Best Solution

It looks (based on your posted code) that the error is resulting from adding another button in the Page_Load method. Your adding it to the Controls collection of the placeholder ph, which is not inside a form tag.

Try placing the Placeholder tag inside the form tag:

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