ASP.NET中使用多个runat=server form

ASP.NET 在同一个页面不支持多个 runat=server forms,要解决这个问题,可以把每个 form 放在一个单独的 panel 控件中,这样用户就可以简单地通过单选按钮在不同 panel 间切换。
代码如下:
2FormExample.aspx

1@ Page language="c#" Codebehind="2FormExample.cs" AutoEventWireup="false"   
2Inherits="_3leaf_app.C2FormExample" 
 1<html><head>
 2<meta content="HTML 4.0" name="vs_targetSchema"/>
 3<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 4<meta content="C#" name="CODE_LANGUAGE"/></head>
 5<body>
 6<form id="Form1" method="post" runat="server">
 7<p>Lookup by   
 8<asp:radiobutton autopostback="True" checked="True" groupname="g1" id="RadioButton1" runat="server" text="First Name"></asp:radiobutton>
 9<asp:radiobutton autopostback="True" groupname="g1" id="RadioButton2" runat="server" text="Last Name"></asp:radiobutton></p>
10<p></p>
11<p>
12<asp:panel id="Panel1" runat="server" visible="True">   
13First Name :   
14<asp:textbox id="TextBox1" runat="server"></asp:textbox>
15<asp:requiredfieldvalidator controltovalidate="TextBox1" errormessage="*" id="RequiredFieldValidator1" runat="server"></asp:requiredfieldvalidator>
16<asp:button id="Button1" runat="server" text="Submit"></asp:button>
17</asp:panel>
18<asp:panel id="Panel2" runat="server" visible="False">   
19Last Name :   
20<asp:textbox id="TextBox2" runat="server"></asp:textbox>
21<asp:requiredfieldvalidator controltovalidate="TextBox2" errormessage="*" id="RequiredFieldValidator2" runat="server"></asp:requiredfieldvalidator>
22<asp:button id="Button2" runat="server" text="Submit"></asp:button>
23</asp:panel>
24<p></p>
25<p>
26<asp:label id="Label1" runat="server"></asp:label>
27</p>
28</p></form>
29</body></html>

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;

///

1<summary>   
2/// Summary description for C2FormExample.   
3/// </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();
}

///

1<summary>   
2/// Required method for Designer support - do not modify   
3/// the contents of this method with the code editor.   
4/// </summary>

private void InitializeComponent()
{
RadioButton1.CheckedChanged += new System.EventHandler (this.RadioButton1_CheckedChanged);
Button1.Click += new System.EventHandler (this.Button1_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 Button1_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;
}
}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus