简体   繁体   中英

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

Control 'MainContent_forenameTxt' of type 'TextBox' must be placed inside a form tag with runat=server. [And similar errors] This error has been bugging me for a few days now. I've narrowed the problem down to an issue being within my Master Page. For some reason, ASP TextBoxes, Buttons etc.. require to be in a form (Which shouldn't necessarily be the case) however my Program decided that it wants to be. So, this issue began when I placed the forms in, stating I could only have one with a runat="server". After much googling and questioning I was given this piece of code:

        public override void VerifyRenderingInServerForm(Control Control)
    {
        //base.VerifyRenderingInServerForm(Control);
    }

Which, from what I can see, does absolutely nothing. It never gets called, and has nothing in it, however makes the page display - so I have absolutely no idea what it does.

So continuing on, it began to bug me, then I realised that my Buttons weren't actually doing anything, except 'refreshing' the page essentially. (Refer to near the bottom of page, with the onClicks

<%@ Page Title="Registration" Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.Master" CodeFile="registration.aspx.cs" Inherits="registration" %>

<div class="contact-bg2">
 <div class="container">
     <div class="booking">
         <h3>Register</h3>
         <p>
         </p>
         <div class="col-md-8 booking-form">
             <h5>Forename</h5>
             <asp:TextBox ID="forenameTxt" runat="server"></asp:TextBox>
             <h5>Surname</h5>
             <asp:TextBox ID="surnameTxt" runat="server"></asp:TextBox>
             <h5>Telephone</h5>
             <asp:TextBox ID="telephoneTxt" runat="server"></asp:TextBox>
             <h5>Mobile</h5>
             <asp:TextBox ID="mobileTxt" runat="server"></asp:TextBox>
             <h5>Address</h5>
             <asp:TextBox ID="addressLn1Txt" placeholder="Line 1" runat="server"></asp:TextBox>
             <asp:TextBox ID="addressLn2Txt" placeholder="Line 2" runat="server"></asp:TextBox>
             <asp:TextBox ID="addressLn3Txt" runat="server" placeholder="Line 3"></asp:TextBox>
             <h5>Postal Code</h5>
             <asp:TextBox ID="postalTxt" runat="server"></asp:TextBox>
             <h5>Username</h5>
             <asp:TextBox ID="registerUsernameTxtbox" runat="server"></asp:TextBox>
             <h5>Password Confirm Password</h5>
             <asp:TextBox ID="registerPasswordTxtbox" runat="server" type="password"></asp:TextBox>&nbsp;&nbsp;
              <asp:TextBox ID="passwordConfirmTxt" runat="server" type="password"></asp:TextBox>
             <h5></h5>
             <asp:Button ID="submitBtn" runat="server" Text="SUBMIT" OnClick="submitBtn_Click" />
             <asp:Button ID="resetBtn" runat="server" Text="RESET" OnClick="resetBtn_Click" />
     </div>
         </div>
     </div>

    </asp:Content>

Code Behind:

        protected void submitBtn_Click(object sender, EventArgs e)
    {


            //If there is any blank fields, these if statements will be run.
            try
            {
                //If the fields entered have all had an input, this if statement will submit it to the database.
                if (forenameTxt.Text != "" && surnameTxt.Text != "" && telephoneTxt.Text != "" && addressLn1Txt.Text != "" && addressLn2Txt.Text != "" && postalTxt.Text != "" && registerUsernameTxtbox.Text != "" && registerPasswordTxtbox.Text != "" && passwordConfirmTxt.Text != "")
                {

                    DLQueries reposit = new DLQueries();

                    //Refers to 'addCustomerAccount' in droversLodgeRepository 
                    reposit.addCustomerAccount(registerUsernameTxtbox.Text.ToString(),
                      registerPasswordTxtbox.Text.ToString(), forenameTxt.Text.ToString(),
                     surnameTxt.Text.ToString(), mobileTxt.Text.ToString(), telephoneTxt.Text.ToString(),
                        addressLn1Txt.Text.ToString(), addressLn2Txt.Text.ToString(),
                        addressLn3Txt.Text.ToString(), postalTxt.Text.ToString());

                    //Redirects to homepage

                }
                else
                {
                    Response.Redirect("homepage.aspx");
                }

            }

            catch (Exception)
            {
            }

    }
    protected void resetBtn_Click(object sender, EventArgs e)
    {
                    forenameTxt.Text = "";
                    surnameTxt.Text = "";
                    telephoneTxt.Text = ""; 
                    addressLn1Txt.Text = "";
                    addressLn2Txt.Text = "";
                    postalTxt.Text = "";
                    registerUsernameTxtbox.Text = ""; 
                    registerPasswordTxtbox.Text = "";
                    passwordConfirmTxt.Text = "";
    }

Now from that alone, I never saw any issues (unless someone else can see any).

So now, I assumed that the issue is on the masterpage. I've went through the masterpage, comparing it with an old website I built and couldn't find the issue.

Head:

 <head id="Head1" runat="server">
<title>Page.Title</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Pinyon+Script' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Quicksand:400,700' rel='stylesheet' type='text/css'>
 <link href="css/bootstrap.css" rel='stylesheet' type='text/css'/>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>

<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/jquery.min.js"></script>
</head>

Body:

    <body>
    <div class="header">
     <div class="top-header">
         <div class="container">
             <div class="logo">
                    <a href="index.html"><img src="images/logo.png"/></a>
             </div>
             <span class="menu"> </span>
             <div class="m-clear"></div>
             <div class="top-menu">
                <ul>
                     <li class="scroll"><a href="homepage.aspx">HOME</a></li>
                     <li><a class="scroll" href="facilities.aspx">FACILITIES</a></li>
                    <li><a class="scroll" href="pricing.aspx">PRICS</a></li>
                     <li><a class="scroll" href="contactUs.aspx">CONTACT US</a></li>
                </ul>
                <script>
                    $("span.menu").click(function () {
                        $(".top-menu ul").slideToggle(200);
                    });
                </script>
             </div>
             <div class="clearfix"></div>
          </div>
      </div>
</div>


        <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix"> 
            <asp:ContentPlaceHolder runat="server" ID="MainContent" >
                </asp:ContentPlaceHolder>
        </section>
    </div>
    </>

    <!---->
    <div class="fotter">
        <div class="container">
            <h3>143 City Located Hotels World Wide</h3>
            <h4>"Hospitality, Quality & Good Locations. We only provide you with the best hotels" - John Deo</h4>
            <i class="man"></i>
        </div>
    </div>
    <!---->

    <div class="fotter-info">
        <div class="container">
            <div class="col-md-5 details">
                <div class="hotel-info">
                    <h4>ABOUT THIS HOTEL</h4>
                    <p>This hotel is a 4 Star Hotel found on the Isle of Mull, in Scotland. </p>
                    <p><b>In order to book for this hotel, you will require to create an account online!</b></p>
                </div>
                <div class="news">
                    <h4>LATEST NEWS</h4>
                    <h5>Grand Hotel Joins DeluxelHotels</h5>
                    <a href="#">15 AUG</a>
                    <h5>Happy Chirstmas To Everyone</h5>
                    <a href="#">15 AUG</a>
                    <h5>Best Places To Visit 2014</h5>
                    <a href="#">15 AUG</a>
                    <h5>Various Offers</h5>
                    <a href="#">15 AUG</a>
                </div>
                <div class="clearfix"></div>
            </div>
            <div class="col-md-7 details">
                <div class="join">
                    <h4>JOIN DELUXEHOTELS</h4>
                    <p>
                        TBA
                    </p>
                    <p>TBA</p>
                    <a href="aboutUs.aspx">READ MORE</a>
                </div>
                <div class="member">
                    <h4>MEMBERS AREA</h4>
                    <form id="Form1" runat="server">
                        <p>Username</p>
                        <asp:textbox id="txtUsername" runat="server"></asp:textbox>
                        <p>Password</p>
                        <asp:textbox id="txtPassword" textmode="Password" runat="server" />
                        <asp:button id="logoutBtn" runat="server" text="LOGOUT" visible="false" onclick="logoutBtn_Click" />
                        <asp:button id="loginBtn" runat="server" text="LOGIN" onclick="loginBtn_Click" />
                        &nbsp;&nbsp;&nbsp;&nbsp; 
                     <asp:button id="registerBtn" runat="server" text="REGISTER" onclick="registerBtn_Click" />
                   </form>
                </div>
                <div class="clearfix"></div>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
        </body>

Now just to add on as a side-note.

I'm currently using a BootStrap UI Template for my website. I've took highlights of pieces of code which would/could be causing the problem

Things that I have noticed/tried: - Buttons on MasterPage work fine. - Buttons on 'registration' page don't work (ie reset and submit) - The parameter on the registration page was too big, so placed all details in that method instead - MasterPage MainContent - Placing a Form within this. - Removing all forms - Create a new testpage which doesn't use MasterPage (buttons do work) - Compared with my other website

I'm no expert at fixing issues, and have struggled to find this error, which you'll most likely find much more quickly than myself. I'd like to apologise for such a large issue/question, but I'd like to get this sorted and not repeat my problem again in the future.

Thanks for looking at this.

I never found the issue. However, I found a workaround until I can find a sufficient alternative.

I started a new Project in Visual Studio, and decided to pick out every piece of information which would be an absolute necessity for the Website to still look relatively decent, and work.

[This means, I have one form over a contentplaceholder, which is holding everything, including another contentplaceholder where the information from every other page is in that placeholder].

I am still getting a few of the errors (which are easily fixable), however the override class is now, not being used due to some debugging finds, I found that it was overriding the button onClicks.

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