views:

158

answers:

4

As that web-standards geek I am, I dislike the default <form runat="server"> that surrounds my entire webpages. I've seen many ASP.NET based webpages that don't have these, so it seems like it can be removed without taking away any functionality. How?

+2  A: 

The form element is essential for ASP.NET to work correctly. It is necessary to post the user-input back to the webserver.

I suggest you take a look at the ASP.NET viewstate to understand how this works..

The only scenario in which you can remove that element, is when using purely static HTML inside the .aspx file.

driAn
+4  A: 

There will have to be a <form runat="server"> if you wish to use controls 'n stuff. Otherwise postbacks are impossible, as is viewstate and the rest of the stuff that .NET depends upon.

Are you sure you've seen what you've thought you've seen? Perhaps these pages only contained static content? Or perhaps they were user-controls? Or yet another common possibility - perhaps it was the Master Page that had the <form runat="server">?

Vilx-
AFAIK StackOverflow is built with ASP.net, and i see no form-element surrounding everything inside the body here.
Arve Systad
StackOverflow uses ASP.NET MVC which does not use server controls or postback events.
CodeMonkey1
This is ASP.NET MVC, not ASP.NET webforms.
Vilx-
Ah, that explains things. Guess I'll have to take a look at MVC then. Thanks :-)
Arve Systad
+3  A: 

In scenarios where there is no <form runat="server"> tag, you are most likely looking at a site built in ASP.NET MVC, as one of the commenters mentioned. ASP.NET MVC does not use server controls, Viewstate or the other features provided by server-side forms, instead using a closer-to-HTML model with relatively simple inline scripting.

On the plus side, you're much closer to straight HTML (in fact, you ARE essentially straight HTML), so it's easier to perform Javascript operations, integrate custom UI widgets, etc.

On the (potentially) minus side, if you have a heavy investment in ASP.NET WebForms controls and technologies, those are not typically out-of-box compatible with the new MVC framework. Also, there is a learning curve associated with it. (I'm just getting into it now.)

For more on ASP.NET MVC, see the official site. As others have mentioned, SO is built on this technology.

John Rudy
+1  A: 

If you don't need to take advantage of web controls, viewstate etc, you can code ASP.NET pages with standard HTML forms and still pick up the values the are passed back through the Request object. Similarly, you can write to the page via the Response object (explicitly or using the <%= MyVar %> format) so there's nothing to stop you having a dynamic website without <form runat="server">. In essence, you get something rather like classic ASP in terms of the interaction with the pages/forms but you have the full .NET framework and all the usual good C#/VB.NET stuff in the back end.

Obviously there's a question as to whether or not this is a sensible approach as you lose many of the benefits of ASP.NET (although you gain a degree of control over the output which you don't have with web controls). If you don't want to use <form runat="server">, then MVC would seem like the best way to go. The only reason we've used the web forms model without <form runat="server"> is in migrating classic ASP apps where we wanted to get a quick port to ASP.NET and then revise the code to a more natural .NET style afterwards.

So, with some limitations, it is entirely possible but not necessarily recommended.

And, of course, it doesn't affect compliance with web standards at all as the runat="server" attribute is stripped out before the HTML is sent to the browser - it only appears in the source code.

Simon Forrest