ASP.NET: A page can have only one server-side Form tag


A student in a Microsoft ASP.NET web development class recently asked the following question:

 I have a Perl webpage that has multiple forms on it, like this:

    <form id=forma1></form>
    <form id=form2></form>

I have tried to do the same thing in ASP.NET and C# with the runat=”server” attribute in the forms, and cannot get it to run.

That’s an excellent question. There is bad news and good news; first, the bad. Unfortunately, ASP.NET “owns” the form so that only one *VISIBLE* server-side form can be on any one .aspx file’s Web Form at a time.

Luckily, there are several workarounds or solutions which might give you what you want.

(1) Here is an article <> by Kirk Evans titled “Neat ASP.NET Trick: Multiple Forms on a Page”. There are many references at other web sites as well which convey the fact that you can’t have more than one HTML <form> with the runat=”server” on the same page, however Kirk Evans presents an example where you could Switch Between Two Forms, as opposed to some other authors who simply say it is impossible.

(2) You could have one ASP.NET <form id=”form1″ runat=”server”></form> and another one or more without the runat=”server” such as <form id=”form2″></form>, however you can’t effectively have ASP.NET elements which are processed on the server-side in the second or subsequent forms. It depends on your needs and what you wanted to do with those forms with respect to client-side and server-side processing as to whether that solution is viable.

(3) You could use User Controls with .ascx files and have more than one of them composed on the same ASP.NET Web Form (.aspx file). This technique is pretty straight forward, and is one of my preferred methods for solving this sort of problem.

(4) You could use multiple buttons which act as submit buttons for different sets of controls/fields on the same form, but the C# (or other) code behind the markup treats the fields in distinct sets so that it is almost like having two forms.

(5) You could use update panels with ASP.NET AJAX.

I hope that helps. To all you in the blogosphere, do these options make sense?


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.