Must be Placed Inside a Form Tag With runat=server

2019-04-20 02:57发布

问题:

I have been attempting this all morning with no results. I can't seem to figure out what I'm doing wrong. I have checked out the two links (among many other unhelpful links) and have yet to solve my issue. This is a WebUserControl...

Receiving the following error: Control 'HeadContent_CareersViewPosting_txtFirstName' of type 'TextBox' must be placed inside a form tag with runat=server.

Already attempted the suggestions here, here and here and no results. Still received the exact same message. Some new suggestions would be greatly appreciated!

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Careers View Posting.ascx.cs" Inherits="ascxStagingApplication.Careers.Careers_View_Posting" %>
<asp:Panel ID="pnlResume" runat="server">
    <table ID="tblMain" runat="server">
        <tr>
            <td>
                <asp:Label ID="lblFirstName" runat="server" Text="* First Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblLastName" runat="server" Text="* Last Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblEmail" runat="server" Text="* Email"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
            </td>

        </tr>
        <tr>
            <td>
                <asp:Label ID="lblResume" runat="server" Text="* Resume"></asp:Label>
            </td>
            <td>
                <asp:FileUpload ID="fupResume" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit"/>
            </td>
        </tr>
    </table>
</asp:Panel>

The user control is currently being placed onto a dummy webpage for testing. Here is the 'dummy' page code.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Page Careers View Posting.aspx.cs" Inherits="ascxStagingApplication.Careers.Page_Careers_View_Posting" %>

<%@ Register Src="~/Careers/Careers View Posting.ascx" TagPrefix="uc1" TagName="CareersViewPosting" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <uc1:CareersViewPosting runat="server" id="CareersViewPosting" />
</asp:Content>

回答1:

In ASPNet webforms - everything needs to run within a form tag.

All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts:

<form runat="server">

...HTML + server controls

</form>

In your dummy page try the following to allow the server controls to run.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Page Careers View Posting.aspx.cs" Inherits="ascxStagingApplication.Careers.Page_Careers_View_Posting" %>

<%@ Register Src="~/Careers/Careers View Posting.ascx" TagPrefix="uc1" TagName="CareersViewPosting" %>
<form runat="server">
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <uc1:CareersViewPosting runat="server" id="CareersViewPosting" />
</asp:Content>
</form>

Also - check that your ~/Site.Master file contains the <form runat="server"> if not -a s it would be fairly typical for that to be the place to have your all enclosing form tag.

You could read more here: http://www.w3schools.com/aspnet/aspnet_forms.asp



回答2:

If you put right of runat="server" but still error, please try this code.

 public override void VerifyRenderingInServerForm(Control control)
    {
         /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
    }

cr. from Rohit Rao

sorry for my bad skill English.



回答3:

All server controls must appear within a <form> tag, and the <form> tag must contain the runat="server" attribute.

All the Asp.net controls are server controls,so these should be placed within form tag with runat="server" attribute, like this

<form runat="server">

place server controls here...

</form>


回答4:

>Button bt = new Button();
>bt.ID = "dd";
>bt.Text = "Click Me";
>this.Form.Controls.Add(bt);


回答5:

you can add

<form runnat="server">
 // add content placeholder
</form>