Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use multiple forms in a web page?

Tags:

forms

asp.net

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.

like image 915
smwikipedia Avatar asked Sep 25 '11 08:09

smwikipedia


People also ask

Can I use multiple form in HTML?

HTML standard dictates no two elements can have the same Ids. You can use Javascript to change the Id/Name dynamically before you hook the connector to the target form.

How forms are used in Web pages?

A webform, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields.

How many form tags are allowed in a asp net web page?

ASP.NET Web Forms technology doesn't allow more than one <form> tags with runat=“server” on a single Web Form page.


4 Answers

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.

like image 55
Ofer Zelig Avatar answered Sep 22 '22 06:09

Ofer Zelig


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).

like image 30
Paul Zaczkowski Avatar answered Sep 24 '22 06:09

Paul Zaczkowski


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.

like image 2
Shadow Wizard Hates Omicron Avatar answered Sep 23 '22 06:09

Shadow Wizard Hates Omicron


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?

like image 1
IrishChieftain Avatar answered Sep 22 '22 06:09

IrishChieftain