I always get the following exception when running my site. "Control 'textBox1' of type 'TextBox' must be placed inside a form tag with runat=server." As far as I understand Html.BeginForm does create a <form> tag. After quite some while of research I replaced the asp:TextBox with Html.TextBox.
And now it works. But I also thought that Html.TextBox does create a simple TextBox. So why is it working now? And what is the problem with the first code snippet above?
Additonally, there is some strange behaviour with an uncommented text box. While testing and trying different ways to get rid of that issue I also had the following piece of code.
As one can see, the asp:TextBox is now commented out. But I still get the same exception (HttpException) as described above. Visual Studio (2010) points it to the <% } %> line. When I remove the comment it works. To me this seems to be a defect because comments
shouldn't be processed. But maybe I missed something?
<asp:TextBox is a server control mainly used in asp.net web forms. In asp.net mvc you generally want to avoid server controls and stick with pure html elements. Server controls are used in asp.net webforms and maintain state across
page posts, however in asp.net mvc you avoid server controls and stick with pure html. Html.TextBox does not generate a server control, but instead creates an html text box element. That is why you don't get that error when using Html.TextBox
The reason the comment still throws an error, is because that error is not generated by the web browser, but by the asp.net runtime. The comment you included is a client side comment that is mainly used to comment out html. Only the browser deals
with that comment, not the web server and asp.net runtime. So even though it's commented out, the asp.net runtime will still try to generate a textbox based on that server control during execution.
arens
0 Points
1 Post
Behaviour of Html.BeginForm
Oct 06, 2010 04:46 AM|LINK
Hi All,
I'm having the following piece of code that does not work.
<% using (Html.BeginForm("Funct", "Funct", FormMethod.Post)) { %> <fieldset> <asp:TextBox ID="textBox1" runat="server">Search for...</asp:TextBox> <input type="submit" value="Search" /> </fieldset> <% } %>I always get the following exception when running my site. "Control 'textBox1' of type 'TextBox' must be placed inside a form tag with runat=server." As far as I understand Html.BeginForm does create a <form> tag. After quite some while of research I replaced the asp:TextBox with Html.TextBox.
<% using (Html.BeginForm("Funct", "Funct", FormMethod.Post)) { %> <fieldset> <%= Html.TextBox("textBox1", "Search for...") %> <input type="submit" value="Search" /> </fieldset> <% } %>And now it works. But I also thought that Html.TextBox does create a simple TextBox. So why is it working now? And what is the problem with the first code snippet above?
Additonally, there is some strange behaviour with an uncommented text box. While testing and trying different ways to get rid of that issue I also had the following piece of code.
<% using (Html.BeginForm("Search", "Search", FormMethod.Post)) { %> <fieldset> <%= Html.TextBox("txtSearch_PollId", "Search for...") %> <input type="submit" value="Search" /> </fieldset> <% } %> <!-- <asp:TextBox ID="txtSearch_PollId" runat="server">Search for...</asp:TextBox> -->As one can see, the asp:TextBox is now commented out. But I still get the same exception (HttpException) as described above. Visual Studio (2010) points it to the <% } %> line. When I remove the comment it works. To me this seems to be a defect because comments shouldn't be processed. But maybe I missed something?
Regards,
arens
Html.BeginForm + TextBox
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Behaviour of Html.BeginForm
Oct 06, 2010 05:45 AM|LINK
<asp:TextBox is a server control mainly used in asp.net web forms. In asp.net mvc you generally want to avoid server controls and stick with pure html elements. Server controls are used in asp.net webforms and maintain state across page posts, however in asp.net mvc you avoid server controls and stick with pure html. Html.TextBox does not generate a server control, but instead creates an html text box element. That is why you don't get that error when using Html.TextBox
The reason the comment still throws an error, is because that error is not generated by the web browser, but by the asp.net runtime. The comment you included is a client side comment that is mainly used to comment out html. Only the browser deals with that comment, not the web server and asp.net runtime. So even though it's commented out, the asp.net runtime will still try to generate a textbox based on that server control during execution.
Blog | Twitter : @Hattan