简体   繁体   中英

Control 'ctl00_TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server

When a form with a run at server is added there will be two forms with runat server and another error occurs. Can some one give me an idea. Thankx in advance.

The details of the error are as follows.

Control 'ctl00_TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'ctl00_TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): Control 'ctl00_TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +2052287
System.Web.UI.WebControls.TextBox.AddAttributesToRender(HtmlTextWriter writer) +49
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +17
System.Web.UI.WebControls.TextBox.Render(HtmlTextWriter writer) +17
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
System.Web.UI.Control.Render(HtmlTextWriter writer) +7
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20
System.Web.UI.Page.Render(HtmlTextWriter writer) +26
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2558

Version Information: Microsoft .NET Framework Version:2.0.50727.1873; ASP.NET Version:2.0.50727.1433

The error states the problem

Correct

<form id="frm" runat="server">
    <asp:TextBox id="txt" runat="server" />
</form>

Incorrect

<asp:TextBox id="txt" runat="server" />
<form id="frm" runat="server">
</form>

Seeing the tag "master-pages" included in the question, this error may pop up when you are trying to dynamically add text boxes (or for that matter, any control) in a content page which uses a master page.

Only the master page contains a form with runat="server" set. The content pages only have content place holders.

To dynamically add controls to a content page, include a panel in your desired contentplaceholder in your content page and then add controls to it in your code. For example,

In your aspx file for the content page, add a panel like

<asp:Panel ID="Panel1" runat="server">
</asp:Panel>   

Then, in your code behind,

        Dim txtbox As New textbox()
        Panel1.Controls.Add(txtbox)

Error happens because you placed your control with runat="server" outside the form tag (with runat="server" too). ASP.NET requires to have all server controls inside one form with runat="server" attribute.

I ran into this too. What I did, was just added a PageWrapper , and all the site's code went inside that. For example:

<body>
 <div ID="PageWrapper">
    <form ID="form1" runat="server">
        <div ID="Header">...</div>
        <div ID="MainContent">...</div>
         ...
    </form>
 </div>
</body>

Something like that. Hope this helps.

With standard ASP.Net, you can only have one form. Thats a limitation of ASP.Net. If youre going to be doing stuff that requires 2 or more <FORM> tags, you'll need MVC or something along the lines of.

You did not say where this control is on your form.

If this error is a result of another control being inside a GridView control like I had, then this may help you, too:

Control xxx of type 'LinkButton' must be placed inside a form tag with runat=server

Error is Control 'txtTo' of type 'TextBox' which must be placed inside a form tag with runat=server.

ERROR is This Code.

      <tr>
          <td class="style1">
                <strong>To </strong>
            </td>
            <td class="style2">
                <strong>: </strong>
            </td>
            <td class="style6">
                &nbsp;
                <asp:TextBox ID="txtTo" runat="server" Width="475px"></asp:TextBox>
            </td>
            <td></td>
        </tr>

控件和表单标签都必须有runat="server"

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