How To Add Multiple <form> Tags on a Page

santosh
0

The post-show how to add multiple form control. If multiple server forms are declared in the same Web form, an exception is thrown. A little-known fact is that a Web form can actually contain as many server-side forms as needed as long as only one at a time is visible. For example, a page with three > tags is allowed, but only one form can actually be rendered. Given the dynamics of page rendering, an exception is thrown if more than one HtmlForm control attempts to render. By playing with the Visible property of the HtmlForm class, you can change the active server form during the page lifetime. This trick doesn’t really solve the problem of having multiple active forms, but it can be helpful sometimes.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleForm.aspx.cs" Inherits="MultipleForm" %>

<!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>Untitled Page</title>
</head>
<body>
   <form id="step0" runat="server" visible="true">
       <h1>
           Welcome</h1>
       <asp:TextBox runat="server" ID="Textbox1" />
       <asp:Button ID="Button1" runat="server" Text="Step #1" OnClick="Button1_Click" />
   </form>
   <form id="step1" runat="server" visible="false">
       <h1>
           Step #1</h1>
       <asp:TextBox runat="server" ID="Textbox2" />
       <asp:Button ID="Button2" runat="server" Text="Previous step" OnClick="Button2_Click" />
       <asp:Button ID="Button3" runat="server" Text="Step #2" OnClick="Button3_Click" />
   </form>
   <form id="step2" runat="server" visible="false">
       <h1>
           Finalizing</h1>
       <asp:Button ID="Button4" runat="server" Text="Finish" OnClick="Button4_Click" />
   </form>
</body>
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 MultipleForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "Welcome";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Title = "Step 1";
        step0.Visible = false;
        step1.Visible = true;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        step0.Visible = true;
        step1.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Title = "Finalizing";
        step1.Visible = false;
        step2.Visible = true;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Title = Textbox1.Text + Textbox2.Text;
        step2.Visible = false;
        Response.Write("<h1>Successfully done.</h1>");
    }

}
Tags

Post a Comment

0Comments

Please do not post any spam link in the comment box😊

Post a Comment (0)