简体   繁体   中英

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

I have this error - Control 'txtUserName' of type 'TextBox' must be placed inside a form tag with runat=server. I am unable to get rid of it, my text box is placed inside a form tag with runat=server.

The page which I am getting this on is my 'reset password' page I want the users to be able to enter their username and once they press the reset password button it takes them to a page which allows them to change their password.

Here is the code that I am currently working with.

<head runat="server">
    <title></title>
</head>
<body>
   <div style="font-family:Arial">
    <table style="border: 1px solid black; width:300px">
        <tr>
            <td colspan="2">
                <b>Reset my password</b>
            </td>
        </tr>
        <tr>
            <td>
                User Name
            </td>    
            <td>
                <form action="ChangePassword.aspx" method="get"> 
                <asp:TextBox ID="txtUserName" Width="150px" runat="server" 
                ontextchanged="txtUserName_TextChanged">
                </asp:TextBox>

                </form>
            </td>    
        </tr>
        <tr>
            <td>

            </td>    
            <td>
                <asp:Button ID="btnResetPassword" runat="server" 
                Width="150px" Text="Reset Password" onclick="btnResetPassword_Click" />
            </td>    
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
            </td>    
        </tr>
    </table>
</div>
</body>
</html>

As the error message says, you need to add a runat="server" in the form tag. This is just something ASP.NET demands to work.

The way ASP.NET handles events and postbacks forces this requirement. It tries to keep the page's state inside the actual page (called the 'view state'), and on form submission it sends it back. This was invented to make the stateless web stateful.

Just put in the runat="server" inside the form tag and you will be set.

<form runat="server" action="ChangePassword.aspx" method="get"> 

Add < form> tag like this..., it will work..

在此处输入图片说明

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