Use multiple runat = server form in ASP. NET

Source: Internet
Author: User

ASP. NET does not support multiple runat = server forms on the same page. To solve this problem, you can place each form in a single panel control, in this way, you can simply switch between different panels using single-choice buttons.
The Code is as follows:
2FormExample. aspx

<% @ Page language = "c #" Codebehind = "2FormExample. cs" AutoEventWireup = "false" Inherits = "_ 3leaf_app.C2FormExample" %>
<Html> <Meta name = vs_targetSchema content = "HTML 4.0">
<Meta name = "GENERATOR" Content = "Microsoft Visual Studio 7.0">
<Meta name = "CODE_LANGUAGE" Content = "C #"> <Body>

<Form method = "post" runat = "server" ID = Form1>
<P> Lookup
<Asp: RadioButton id = RadioButton1 runat = "server" Text = "First Name" AutoPostBack = "True" groupname = g1 checked = True> </asp: RadioButton>
<Asp: RadioButton id = RadioButton2 runat = "server" Text = "Last Name" AutoPostBack = "True" groupname = g1> </asp: RadioButton> </p>
<P> </p>
<P>
<Asp: Panel id = Panel1 runat = "server" visible = True>
First Name:
<Asp: TextBox id = TextBox1 runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator id = RequiredFieldValidator1 runat = "server" ErrorMessage = "*" ControlToValidate = "TextBox1"> </asp: RequiredFieldValidator>
<Asp: Button id = Button1 runat = "server" Text = "Submit"> </asp: Button>
</Asp: Panel>
<Asp: Panel id = Panel2 runat = "server" visible = False>
Last Name:
<Asp: TextBox id = TextBox2 runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator id = RequiredFieldValidator2 runat = "server" ErrorMessage = "*" ControlToValidate = "TextBox2"> </asp: RequiredFieldValidator>
<Asp: Button id = Button2 runat = "server" Text = "Submit"> </asp: Button>
</Asp: Panel>
<P> </p>
<P>
<Asp: label id = Label1 runat = "server"> </asp: label>
</P>
</Form>

</Body>


2FormExample. cs

Namespace _ 3leaf_app
{
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;

/// <Summary>
/// Summary description for C2FormExample.
/// </Summary>
Public class C2FormExample: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Button Button2;
Protected System. Web. UI. WebControls. RequiredFieldValidator RequiredFieldValidator2;
Protected System. Web. UI. WebControls. TextBox TextBox2;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. WebControls. RequiredFieldValidator RequiredFieldValidator1;
Protected System. Web. UI. WebControls. TextBox TextBox1;
Protected System. Web. UI. WebControls. Label Label1;
Protected System. Web. UI. WebControls. Panel Panel2;
Protected System. Web. UI. WebControls. Panel Panel1;
Protected System. Web. UI. WebControls. RadioButton RadioButton2;
Protected System. Web. UI. WebControls. RadioButton RadioButton1;

Public C2FormExample ()
{
Page. Init + = new System. EventHandler (Page_Init );
}

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
}
}

Protected void Page_Init (object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP + Windows Form Designer.
//
InitializeComponent ();
}

/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
RadioButton1.CheckedChanged + = new System. EventHandler (this. RadioButton1_CheckedChanged );
Button1.Click + = new System. EventHandler (this. button#click );
RadioButton2.CheckedChanged + = new System. EventHandler (this. RadioButton2_CheckedChanged );
Button2.Click + = new System. EventHandler (this. Button2_Click );
This. Load + = new System. EventHandler (this. Page_Load );
}

Public void Button2_Click (object sender, System. EventArgs e)
{
Label1.Text = "You want to search on last name ";
}

Public void button#click (object sender, System. EventArgs e)
{
Label1.Text = "You want to search on first name ";
}

Public void RadioButton2_CheckedChanged (object sender, System. EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = true;
}

Public void RadioButton1_CheckedChanged (object sender, System. EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = false;
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.