Neat ASP.NET Trick: Multiple Forms on a Page
Tinkering around today, and I figured out a weird trick with ASP.NET 1.1. If you try to add 2 forms to an ASP.NET page, you get a nice error:
A page can have only one server-side Form tag.
Apparently, that message should be augmented to read:
A page can have only one visible server-side Form tag.
The form has a .Visible property, which we can toggle back and forth. The default value is true, and only the HTML for the visible form is rendered to the client. If a form's .Visible property is false, it can still coexist on the page, it just is not rendered to the client. This could be interesting for implementing something similar to the wizard control in ASP.NET 2.0.
<%@ Page language="c#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<script runat=server language="C#">
private void Page_Load(object sender, System.EventArgs e)
{
Form1.Visible = !Form1.Visible;
Form2.Visible = !Form2.Visible;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >
<asp:TextBox id="Textbox2" runat="server">Form1</asp:TextBox>
<asp:Button Runat=server ID="Button1" NAME="Button1" Text="Click to see Form2"/>
</form>
<form id="Form2" method="post" runat="server" visible="false" >
<asp:TextBox id="TextBox1" runat="server">Form2</asp:TextBox>
<asp:Button Runat=server ID="Button2" NAME="Button2" Text="Click to see Form1"/>
</form>
</body>
</HTML>
Comments
Anonymous
October 20, 2005
Out of curiosity why would you want this? Now yes I have multiple forms on a page however I put them in Panel so for example.
<form id="Form1" method="post" runat="server" >
<asp:Panel id="panel1" runat="server" >
<asp:TextBox id="Textbox2" runat="server">Form1</asp:TextBox>
<asp:Button Runat=server ID="Button1" NAME="Button1" Text="Click to see Form2"/>
</asp:panel>
<asp:Panel id="panel2" runat="server" visible="false">
<asp:TextBox id="TextBox1" runat="server">Form2</asp:TextBox>
<asp:Button Runat=server ID="Button2" NAME="Button2" Text="Click to see Form1"/>
</asp:panel>
</form>
This way you could still have one form. If you wanted different properties on the one form this makes it easier to dynamically adjust the enctype and other properties of the form from code behind. You could also only use one button then use the onCommandEvent and pass command arguments to determine what the one button is supposed to be doing.
So I guess I am asking as to why you would want to use two forms over something like panels and one form.Anonymous
July 02, 2014
Putting two logical forms into just one real form dramatically increases difficuty of HTML5 validation, for example.Anonymous
September 30, 2014
thank you so very much!!!!!! it worked!!!!!Anonymous
September 30, 2014
no its working properly!! form 2 becomes invisible!!! I want to see form 1 as well as form 2 on my page!!Anonymous
September 30, 2014
Just give up. Its not possible. WebForms needs the form on the whole page to handle post backs. The only way to avoid this is to not use postbacks, but at that point, you may as well not use WebForms. Want a search form that redirects to a search page using get parameters? Postback and redirect. Want a contact form that posts to an external api? No can do. Postback and proxy. want a payment form that posts variables to ebanking gateway? Don't even bother. Postback and proxy. Or, don't use Webforms. It's 2014.Anonymous
May 05, 2015
Whai if one form is in master page and other in current page and using that master page?Anonymous
May 06, 2015
This post is 10 years old. Web Forms have undergone significant advances since then, I would look to other means to solve the problem. If you need this low level of control, then reconsider your technology choice and look at ASP.NET MVC.