Using multiple forms on an ASP.NET web forms page

Using multiple forms on an ASP.NET web forms page

Posted on June 21, 2009 by Frederik Vig in ASP.NET

I 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

Three forms on an ASP.NET web forms page

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

Displaying result of multiple forms on ASP.NET web forms page

<%@ 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: