在 ASP.NET 中使用多个 runat=server form
2024-07-10 12:57:56
供稿:网友
asp.net 在同一个页面不支持多个 runat=server forms,要解决这个问题,可以把每个 form 放在一个单独的 panel 控件中,这样用户就可以简单地通过单选按钮在不同 panel 间切换。
代码如下:
2formexample.aspx
<%@ page language="c#" codebehind="2formexample.cs" autoeventwireup="false" inherits="_3leaf_app.c2formexample" %>
<html><head>
<meta name=vs_targetschema content="html 4.0">
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#"></head>
<body>
<form method="post" runat="server" id=form1>
<p>lookup by
<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></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;
/// <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.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;
}
}
}