Click to See Complete Forum and Search --> : Always one, page encompassing <form>?


toicontien
01-21-2009, 11:03 AM
While working with some clients who use .NET, I've noticed pages on their sites are wholly contained inside one <form> tag:
<form name="aspnetForm" method="post" action="Default.aspx" id="aspnetForm">

Is this normal for a .NET site, or perhaps ASP .NET? I don't really have a problem to solve. I'm just curious. I'm a front end and PHP developer by trade.

debiguana
01-22-2009, 07:56 AM
Yes. For ASP.NET to properly parse the server-side elements, they must be contained within a <form> element that has a runat="server" attribute. Each server-side control (or more accurately, server-rendered control) must also have a runat="server" attribute and be within this <form> element.

You see these controls in the page as <asp:checkbox>, <asp:textbox>, <asp:gridview>, etc. When the ASP.NET process serves them up, it converts those into the appropriate HTML entities along with the appropriate behavior as defined in the application.

Hope this helps clarify things for you!
-Doug

toicontien
01-22-2009, 09:14 AM
It does a little. The sites I was looking at literally had everything on their page inside a <form>, even when there were no form elements on the page. It was just a page of content: Links, ads, plain text and images. No form fields or submit buttons at all. I was just wondering that, even if no form fields or buttons are placed on the, if the ASP .NET environment puts the whole page inside a <form> by default, or it was just bad coding on the programmer's part.

debiguana
01-22-2009, 10:16 AM
Visual Studio does that by default.
-Doug

felgall
01-22-2009, 08:37 PM
So it is bad practice on the part of the language rather than of those using it.

chazzy
01-23-2009, 07:25 AM
So it is bad practice on the part of the language rather than of those using it.

How so?

toicontien
01-23-2009, 09:41 AM
It's bad practice because it's placing a <form> on the page when one is not needed. Then what happens if you have more than one form on the page? I've seen threads in the JavaScript forum where people are trying to get forms inside forms working with JavaScript, which breaks the basic usability of a page. But simply because the software does it by default doesn't mean you can't change it. I imagine you can, so the blame also lies with the developer.

chazzy
01-23-2009, 06:43 PM
It's bad practice because it's placing a <form> on the page when one is not needed. Then what happens if you have more than one form on the page? I've seen threads in the JavaScript forum where people are trying to get forms inside forms working with JavaScript, which breaks the basic usability of a page. But simply because the software does it by default doesn't mean you can't change it. I imagine you can, so the blame also lies with the developer.

The fact of the matter is that you don't use more than 1 form on a page. Whenever you develop in a MVC environment, you're assuming that each button goes to a different action, and you can delegate how these events are processed to different functions, rather than assuming you'll get an HTTP request incoming. This should result in more secure applications; especially considering that each input field name can be different for each invocation. If a developer is in fact trying to put a form inside a form, they probably don't know how to use the framework properly.

toicontien
01-24-2009, 01:18 PM
The fact of the matter is that you don't use more than 1 form on a page. Whenever you develop in a MVC environment, you're assuming that each button goes to a different action, and you can delegate how these events are processed to different functions, rather than assuming you'll get an HTTP request incoming. This should result in more secure applications; especially considering that each input field name can be different for each invocation. If a developer is in fact trying to put a form inside a form, they probably don't know how to use the framework properly.

Having worked extensively in an MVC environment, I still don't see why each page should only have one form. What if, to take an example from social networking, you are on a page that allows you to add a photo album. Obviously you'll need a form. What if you want a search form on the page too, where you can search the site. This isn't unreasonable and we do that all the time on Motortopia (http://www.motortopia.com/). My complaint is that the <form> literally encompasses the ENTIRE page, not that the page has a form. I was wondering if putting the entire web page inside a <form> was something the framework does by default or if it was just bad coding by the developer.

Some of the problems I've seen on the JavaScript forums, with what appears to be an ASP site, is that the developer isn't literally putting a form inside a form, but that a form is serving two functions instead of just one.

The particular instance I'm speaking of I believe is just a mistake by the developer, rather than the fault of the framework.

chazzy
01-24-2009, 05:04 PM
i can see, from your site, where you can see multiple forms as necessary. When I look I see this:


<form action="/search/all" method="post" onsubmit="return QuickSearch.submit(this);">
<form method="post" action="/user/login/main/y" class="box" id="mainLoginFORM">


Your search form requires javascript, but the login form doesn't. From my perspective, this makes your code look inconsistent (but that's just my opinion, mind you). I would probably argue that this javascript should be in the input button, like this..


<input type="submit" value="Search" onclick="return QuickSearch.submit(document.forms[0]);"/>


i don't see a problem with a form serving two functions; but you just need to make sure you understand what is happening when you do (in these scenarios, .NET is delegating the processing of the form to the function id'd in the associated code behind class).

toicontien
01-25-2009, 10:21 AM
I see what you mean, Chazzy. But to clarify the search form:

It doesn't require JavaScript to function, if I were to link the "Search" nav item to www.motortopia.com/search/all. I keep forgetting to set the href for that link. If it were linked to that page, it wouldn't require JavaScript. JS would only enhance the search (as soon as I fix that link, which I will do on Monday).

I really didn't mean to get into a discussion about MVC. I was questioning whether a page totally encompassed by a form when there are no form fields or buttons is: a) necessary because of some behavior by the framework, or b) just a simple, and most times harmless, oversight by the developer.