30

So far, all the web pages I met contain at most one <form> tag. Why not multiple ones? I can not think of reasons why multiple forms can't coexist within the same web page.

Also, to be specific to ASP.NET - why are all the server controls are placed within the <form> tag? Why not place them somewhere else?

Plus,

I noticed that in an .aspx file, the <form> tag has the runat=server attribute, while a normal server control such as Button also has one. So it seems the <form> is also a server control. But strangely enough, I cannot find it in the Visual Studio Toolbox.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
smwikipedia
  • 61,609
  • 92
  • 309
  • 482

6 Answers6

25

There can be multiple forms, with hacks.

It is indeed a shortcoming of WebForms. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).

The reason all server controls are placed inside <form> tag is to allow the WebForms engine to recognize them, load their values & save their values from/to the ViewState. Almost all infrastructure of control management in WebForms is based on the idea that a tag contains everything you access from the code-behind.

Community
  • 1
  • 1
Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • If it is such an obvious shortcoming, why the ASP.NET guy still designed it this way? Any special considerration? Or compromise? – smwikipedia Sep 26 '11 at 00:08
  • 20
    When they designed ASP.NET, it was a totally different world of web and assumptions. No Ajax, unawareness to bandwidth (thus everything is serialized via the ViewState which can grow extremely large). MS tried to bring the model of VB forms to the web - everything is drag&drop onto a form, and the controls "magically" remember what their value was before submit. All that requires a managed form element container. – Ofer Zelig Sep 26 '11 at 06:53
4

As pointed out, this is one of the shortcomings of WebForms. I do want to point out, additionally, that with cross-page posting and validation groups, you can typically reach your desired behavior (for most "multi-form" solutions).

Paul Zaczkowski
  • 2,848
  • 1
  • 25
  • 26
  • An alternative to validation groups would be to add the `formnovalidate` property to a button that does not need to be restricted by required fields. – Captain Jun 02 '21 at 19:30
2

Regarding the additional question: the <form runat="server"> is parsed as HtmlForm class behind the scenes, which inherits from HtmlControl like any other HTML element with runat="server".

Unlike any other HtmlControl though, there can exist only one instance per page and it does not appear in the toolbox as it's added automatically to every new Form you create, so it's quite pointless.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

Yes, it can be done - by creating a custom HtmlForm object and toggling the forms as needed. I've just answered a similar question here (with code):

Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?

Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
0

many non server forms - you can , but only one runAt Server form

i also found this :

A server-side form tag is the tag which has a runat="server" attribute. If this attribute is missing, then it's a typical HTML form tag. The conclusion is that you are allowed to use multiple form tags on a page, as long as only one has the runat="server" attribute. The disadvantage of the form that doesn't have this attribute, is that view state won't work (meaning form values will disappear when using the back/forward browser buttons). It's a small price to pay if you really need multiple forms on a page.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    One of the major concepts in ASP.NET (WebForms) is that the entire site is contained within a single form. Whenever a postback is performed, every input on the page is posted. The reason for this is because of ViewState, a hidden form field in ASP.NET WebForms which contains all the state information for every control on the page. This viewstate input needs to be posted on every single postback in order for all the state of the controls to be reloaded properly – Royi Namir Sep 25 '11 at 08:48
  • @smwikipedia, See my answer above. – Ofer Zelig Sep 25 '11 at 08:57
-5
  1. Take master page & set design.
  2. Take one form in master page.
  3. Second form take in contain place holder.
  4. In contain place holder in only for write form tag (not use)
  5. Add aspx page & design second form but not write form tag only for control put
  6. Take button click event fire code write

This is proper way of two form

sashkello
  • 17,306
  • 24
  • 81
  • 109
Hardik
  • 1
  • 4
    I'm sorry I don't normally downvote for bad english, but this is really incoherent. It may even be a correct answer, but it's impossible to tell. – akousmata Aug 25 '14 at 14:16