Karan Ramchandani
Karan Ramchandani

Reputation: 305

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>

Upvotes: 1

Views: 13177

Answers (2)

Harshith C
Harshith C

Reputation: 113

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

enter image description here

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 156978

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

Upvotes: 3

Related Questions