简体   繁体   中英

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

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

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>

Are you adding your buttons to the Page afterwards? Also, if you do not specify an ID to your buttons, they will be given one automatically in the form of ctl02

Try this

<!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>

Assuming you are using a webUsercontrol with .ascx file extension, and you created a webform with a .aspx extension where you want to use it t test or run your .ascx module online. Make sure you place the .ascx file inside the horizontal box by dragging the .acsx module from your solution explorer into the .aspx Design page. The horizontal box creates a default form for you. Now run the .aspx file, assuming you already set it as the start page.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM