
Using multiple forms on an ASP.NET web forms page
Posted on June 21, 2009 by Frederik Vig in ASP.NETI came across this simple technique while watching Building great standards based websites for the big wide world with ASP.NET 4.0.
The cool thing about being able to have multiple forms on the same page is that you don’t have to use JavaScript to fix the default button problem, or use unnecessary ASP.NET web controls or logic on the server (like the Response.Redirect method). The drawback, for some, is that you cannot have the forms inside the form (web form) with the runat=”server” attribute, which means that you cannot use all of the ASP.NET web controls.
Example

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset>
<legend>ASP.NET web form (POST)</legend>
<asp:Label runat="server" AssociatedControlID="txtSearch">Name: </asp:Label><asp:TextBox runat="server" ID="txtSearch" /><asp:Button runat="server" ID="btnSend" Text="Search" OnClick="btnSend_Click" />
</fieldset>
</form>
<form method="get" action="Search.aspx">
<fieldset>
<legend>Regular HTML form using GET</legend>
<label for="name-text">Name: </label><input type="text" id="name-text" name="q" /><input type="submit" value="Search" />
</fieldset>
</form>
<form method="post" action="Search.aspx">
<fieldset>
<legend>Regular HTML form using POST</legend>
<label for="name-text2">Name: </label><input type="text" id="name-text2" name="q" /><input type="submit" value="Search" />
</fieldset>
</form>
</body>
</html>
|
using System;
public partial class _Default : System.Web.UI.Page
{
protected void btnSend_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("Search.aspx?q={0}", txtSearch.Text));
}
}
|
Search.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="lblResult" />
</div>
</form>
</body>
</html>
|
using System;
using System.Web;
public partial class Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["q"]))
{
lblResult.Text = HttpUtility.HtmlEncode(Request.QueryString["q"]);
return;
}
// For the regular html form POST method
lblResult.Text = HttpUtility.HtmlEncode(Request.Form["q"]);
}
}
|
Related Posts:
Frederik Vig
Post Author
I'm a partner and ASP.NET developer for the Norwegian web agency Geta. I mainly write about things I work with, which are: .NET and web technologies
Other posts by Frederik Vig →
Tatham Oddie says:
Post Author June 22, 2009 at 07:24Hi Frederik,
Thanks for watching the video!
You note that not being able to use some server controls is a drawback. Many of them do actually work outside of your server form, with a simple tweak.
A good example is the DropDownList control. It’s a very useful control, even in client forms, because you can data bind the items collection and the selected value easily.
To allow it to render, you need to add a simple override in your page:
protected override VerifyRenderingInSearchForm(Control control)
{
return true;
}
This will then allow you to place those controls anywhere in your markup.
Hope that helps,
Tatham Oddie
http://tath.am
Frederik Vig says:
Post Author June 22, 2009 at 09:56Thanks for sharing Tatham!
Frederik
Tatham Oddie says:
Post Author February 1, 2010 at 23:09Oops – I just got an email from a reader who found a few mistakes in my comment. The method is called VerifyRenderingInServerForm, not VerifyRenderingInSearchForm as I mistyped.
The method is on the page.
http://msdn.microsoft.com/en-au/system.web.ui.page.verifyrenderinginserverform.aspx
Just override it with an empty method body, for example:
public override void VerifyRenderingInServerForm(Control control) { }
The default base implementation throws an exception if the control is not within the server form. This override prevents that behaviour.
Tatham Oddie
http://tath.am
nba-nfljerseys says:
Post Author October 19, 2010 at 05:44I wanted to thank you for this great read!
Saeed Neamati says:
Post Author January 30, 2012 at 14:21This is fine, as long as you keep HTML form tags separated. If you want to embed a form into another form, (which is of course logically wrong, but sometimes you may encounter it because you are in the middle of a project, and this is one of the very few ways remained), then you can’t do it.
Lelala says:
Post Author July 23, 2012 at 11:41Many Thanks for that, i thought i’m stuck 🙂
Mark Hagen says:
Post Author March 14, 2018 at 21:53I have built a master page database driven responsive menu but it requires
now I want to use form on my page.
using
Error
A page can have only one server-side Form tag.
Any Ideas??