Multiple forms on one aspx page?

I cant find an article on this anywhere, even though its such a clear question. How do I make multiple forms work on a single page. Just to clear things up, one form is a search form, one is a login, and one is a signup. Please no javascript solutions.

Login sits in the master file with its script there. The other two are in the index. Only one form tag in the master <form id=“form1” runat=“server”>. Please help me on this before I shoot myself in the face.

Take the nested <form> tags out. You only need one.

I use <asp:Panel> around the separate forms. Then you can fire a click event using the enter key by including the DefaultButton attribute in the Panel control. That’s important because if someone hits enter on a second form it will fire the click event on which ever is first on the page if you aren’t using the Panel controls.

Doesnt work. Heres the script. It got ONE form tag, two form inside the form tag and forms are inside their own panels. STILL, if I click the button on the first form, it fires the second one.


<script language="c#" runat="server">
   	protected void loginSubmit_Click(object sender, ImageClickEventArgs e) {
		Response.Redirect("somepage.aspx");
	}
	
	protected void Submit_Click(object sender, ImageClickEventArgs e) {
		Response.Redirect("somepage.aspx");
	}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form id="form1" runat="server">

		<asp:Panel id="loginPanel" DefaultButton="loginSubmit" Runat="Server">	
			<asp:TextBox Width="178px" id="email" runat="server" />
			<asp:CheckBox id="rememberme" runat="server" />
			<asp:TextBox Width="178px" id="password" runat="server" TextMode="Password" />
			<asp:imagebutton id="loginSubmit" onclick="loginSubmit_Click" runat="server" ImageUrl="images/login.gif"/>	
		</asp:Panel>

		<asp:Panel id="signupPanel" DefaultButton="Submit" Runat="Server">				
				<asp:TextBox Width="240px" id="FName" runat="server" />
 				<asp:RequiredFieldValidator id="valTxtFName" ControlToValidate="FName" ErrorMessage="Please enter your first name!" runat="server" /></asp:RequiredFieldValidator>				
				<asp:TextBox Width="240px" id="LName" runat="server" />
 				<asp:RequiredFieldValidator id="valTxtLName" ControlToValidate="LName" ErrorMessage="Please enter your last name!" runat="server" /></asp:RequiredFieldValidator>
				<asp:imagebutton id="Submit" onclick="Submit_Click" runat="server" ImageUrl="images/signup.gif"/>
		</asp:Panel>
		
</form>
</body>
</html>

I copied and pasted your code and it works fine on this end. If you change the second URL you will see that it does redirect to different pages.

Are you talking about the validation firing?

Its two different forms. So if I type something into the two fields of the first form and click “Log In” it shouldnt fire the validation controls of the second form… If I remove the validation controls, sure they both work fine, but why is the first form triggering events of the second?

You need to add ValidationGroup.

        <asp:Panel id="loginPanel" DefaultButton="loginSubmit" Runat="Server" ValidationGroup="first">
            <asp:TextBox Width="178px" id="email" runat="server" />
            <asp:CheckBox id="rememberme" runat="server" />
            <asp:TextBox Width="178px" id="password" runat="server" TextMode="Password" />
            <asp:imagebutton id="loginSubmit" onclick="loginSubmit_Click" runat="server" ImageUrl="images/login.gif" ValidationGroup="first"/>
        </asp:Panel>

        <asp:Panel id="signupPanel" DefaultButton="Submit" Runat="Server" ValidationGroup="two">
                <asp:TextBox Width="240px" id="FName" runat="server" />
             <asp:RequiredFieldValidator id="valTxtFName" ControlToValidate="FName" ErrorMessage="Please enter your first name!" runat="server" /></asp:RequiredFieldValidator>
                <asp:TextBox Width="240px" id="LName" runat="server" />
             <asp:RequiredFieldValidator id="valTxtLName" ControlToValidate="LName" ErrorMessage="Please enter your last name!" runat="server" /></asp:RequiredFieldValidator>
                <asp:imagebutton id="Submit" onclick="Submit_Click" runat="server" ImageUrl="images/signup.gif" ValidationGroup="two"/>
        </asp:Panel>

Done. It works.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidationMethod.aspx.cs" Inherits="ValidationMethod" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form id="form1" runat="server">

		<asp:Panel id="loginPanel" DefaultButton="btnone" Runat="Server">	
			<asp:TextBox Width="178px" id="email" runat="server" />
			<asp:RegularExpressionValidator ID="regExpValEmail" runat="server" ControlToValidate="email"
                                Display="Dynamic" EnableClientScript="False" ErrorMessage="Invalid email!" ValidationExpression="\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"
                                ValidationGroup="one"></asp:RegularExpressionValidator>
			<asp:TextBox Width="178px" id="password" runat="server" TextMode="Password" />
			<asp:RequiredFieldValidator ID="reqValpassword" runat="server" ControlToValidate="password"
                                Display="Dynamic" EnableClientScript="False" ErrorMessage="You must enter your password."
                                ValidationGroup="one"></asp:RequiredFieldValidator>
			<asp:Button ID="btnone" runat="server" Text="Submit" ValidationGroup="one" OnClick="btnone_Click" />	
			
		</asp:Panel>

		<asp:Panel id="signupPanel" DefaultButton="btntwo" Runat="Server">				
			<asp:TextBox Width="240px" id="FName" runat="server" />
 			<asp:RequiredFieldValidator ID="reqValFName" runat="server" ControlToValidate="FName"
                                Display="Dynamic" EnableClientScript="False" ErrorMessage="You must enter your first name."
                                ValidationGroup="two"></asp:RequiredFieldValidator>		
			<asp:TextBox Width="240px" id="LName" runat="server" />
 			<asp:RequiredFieldValidator ID="reqValLName" runat="server" ControlToValidate="LName"
                                Display="Dynamic" EnableClientScript="False" ErrorMessage="You must enter your last name."
                                ValidationGroup="two"></asp:RequiredFieldValidator>
			 <asp:Button ID="btntwo" runat="server" Text="Submit" ValidationGroup="two" OnClick="btntwo_Click" />
		
		</asp:Panel>
		
		
</form>
</body>
</html>

and the validation method page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ValidationMethod : System.Web.UI.Page {

    protected void btnone_Click(object sender, EventArgs e) {

        Page.Validate("one");
		
    }
    protected void btntwo_Click(object sender, EventArgs e) {

        Page.Validate("two");

    }
}

Nice work. :slight_smile:

Just one last question. If I wanted to translate the ValidationMethod code into VB, how would I write it out? I have it like this now, but it gives me error on line “Partial Class ValidationMethod Inherits System.Web.UI”

this is the error "An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. "

My code looks like this

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collection
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Partial Class ValidationMethod Inherits System.Web.UI.Page

	Protected Sub btnone_Click(sender As Object, e As ImageClickEventArgs)
	
        Page.Validate.ValidationGroup("one")
		
	End Sub
    
    Protected Sub btntwo_Click(sender As Object, e As ImageClickEventArgs) 
	 
        Page.Validate("two")
		
    End Sub

End Class

Nevermind actually. Just translated.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collection
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class ValidationMethod : Inherits System.Web.UI.Page

	Protected Sub btnone_Click(sender As Object, e As ImageClickEventArgs)
	
        Page.Validate("one")
		
	End Sub

    Protected Sub btntwo_Click(sender As Object, e As ImageClickEventArgs)
	
        Page.Validate("two")
		
    End Sub

End Class