How to get rid of the default form element from an .aspx page?

Like you, the web standards developer, I don't like the standard <form runat="server"> that surrounds my web pages. I have seen many ASP.NET-based web pages that do not have them, so it seems that they can be deleted without interrupting any functionality. How?

+2
source share
4 answers

If you want to use controls, you will need <form runat="server"> . Otherwise, postbacks are impossible, as is the viewstate, and the rest of the stuff that .NET depends on.

Are you sure you saw what you thought you saw? Perhaps these pages only contain static content? Or maybe they were control users? Or another common feature - perhaps it was the main page with <form runat="server"> ?

+5
source

In scenarios where there is no <form runat="server"> , you are most likely looking at a site built in ASP.NET MVC, as one of the commentators mentioned. ASP.NET MVC does not use server controls, Viewstate, or other functions provided by forms on the server side, but instead uses a more HTML-based model with relatively simple built-in scripts.

On the positive side, you are much closer to direct HTML (in fact, you are essentially direct HTML), so it’s easier to perform Javascript operations, integrate user interface widgets, etc.

On the (potentially) negative side, if you have a large investment in ASP.NET WebForms controls and technologies, they are usually not compatible with the new MVC framework. In addition, there is a learning curve associated with it. (I'm just wasting it now.)

For more information on ASP.NET MVC, see the official site. As others have noted, SO is based on this technology.

+3
source

A form element is required for ASP.NET to function properly. You must send user input back to the web server.

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

The only scenario where you can remove this element is to use purely static HTML inside the .aspx file.

+2
source

If you don't need to use web controls, viewstate, etc., you can copy ASP.NET pages with standard HTML formats and still receive values ​​that are passed back through the Request object. Similarly, you can write to a page through a Response object (explicitly or using the format <%= MyVar %> ) so that nothing <%= MyVar %> you from having a dynamic website without <form runat="server"> . In essence, you get something like classic ASP in terms of interacting with pages / forms, but you have a complete .NET environment and all the usual good C # / VB.NET stuff at the end.

Obviously, the question arises as to whether this is really a sensible approach, since you are losing many of the benefits of ASP.NET (although you get some degree of control over the results that you don't have with web controls). If you do not want to use <form runat="server"> then MVC will seem to be the best way. The only reason we used the web form model without <form runat="server"> was to port the classic ASP applications, where we wanted to get a quick port for ASP.NET, and then reconsider the code in a more natural style. NET

Thus, with some limitations, this is entirely possible, but not necessarily recommended.

And, of course, this does not affect compliance with web standards in general, since the runat="server" attribute is deleted before the HTML code is sent to the browser - it appears only in the source code.

+1
source

All Articles