关于你的问题:
如何在一个母版页的 ASP.net 页面的页眉和正文中实现两种表单,或者如何在只有一个表单的两个地方显示相同的表单?
如果你需要选择:专注于第二部分。
Web 窗体中的一般方法是在您的页面中只包含一个<form>,这是 .NET 的默认 <form>,它围绕您的 .aspx/.master 页面的所有内容。
作为HTML状态的规则:You cannot have two nested forms in an HTML page。
这意味着如果你想在你的页面中有多个<form>标签,你将不得不在.NET默认的<form>之外使用它。
基本上,这意味着 .NET 之外的所有表单都不会成为 View State 的一部分,并且您将无法使用 ASP.NET Web 控件。
但是,如果您仍在考虑第一种方法,您可以在此处阅读更多信息:
Can we use multiple forms in a web page?
您可以在这里看到一个非常好的实现示例:
Using multiple forms on an ASP.NET web forms page
在两个地方显示相同的表单
基本上,它是Web Forms的重要组成部分,被多次使用。
您可以通过将任意数量的<asp:Button> 元素关联到不同的Click 事件来创建任意数量的表单。
在master中制作两种形式,一种在header中,一种在body中:
将表单内容放在两个部分并在后面的代码中使用两个不同的提交按钮处理程序(参见下面的示例)
放入您的MasterPage 多个ContentPlaceHolder 元素。为您希望从 .aspx 文件加载内容的每个位置使用一个。
在您的 .aspx 中使用对应的 ContentPlaceHolderIDs 引用 ContentPlaceHolder 元素
在此示例中,您可以在 Header 部分看到一个表单,在 Body 部分看到另一个表单:
MasterPageTwoSections.master
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageTwoSections.master.cs" Inherits="MasterPageTwoSections" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<style>
header {
background-color:red;
}
.body {
background-color:green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<header>
<asp:ContentPlaceHolder id="HeaderPlaceHolder" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
<h5>
This is form #1 from the master
</h5>
<asp:TextBox runat="server" ID="txtFirstForm"></asp:TextBox>
<asp:Button runat="server" ID="btnFirstFormSubmit" OnClick="btnFirstFormSubmit_Click"
Text="Submit first form" />
</header>
<section class="body">
<asp:ContentPlaceHolder id="BodyPlaceHolderBeforeForm" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
<h5>
This is form #2 from the master
</h5>
<asp:TextBox runat="server" ID="txtSecondForm"></asp:TextBox>
<asp:Button runat="server" ID="btnSecondFormSubmit" OnClick="btnSecondFormSubmit_Click"
Text="Submit first form" />
<asp:ContentPlaceHolder id="BodyPlaceHolderAfterForm" runat="server">
<%--Placeholder for the pages--%>
</asp:ContentPlaceHolder>
</section>
</div>
</form>
</body>
</html>
MaterPageTwoSections.master.cs
注意两个提交处理程序:
using System;
public partial class MasterPageTwoSections : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnFirstFormSubmit_Click(object sender, EventArgs e)
{
}
protected void btnSecondFormSubmit_Click(object sender, EventArgs e)
{
}
}
FirstPage.aspx
注意Content2指的是MasterPage中的HeaderPlaceHolder。Content3和Content4指的是BodyPlaceHolderBeforeForm和BodyPlaceHolderAfterForm
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageTwoSections.master"
AutoEventWireup="true" CodeFile="FirstPage.aspx.cs" Inherits="FirstPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderPlaceHolder" Runat="Server">
<p>
This header content is from the FirstPage.aspx
</p>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyPlaceHolderBeforeForm" Runat="Server">
<p>
This body content is from the FirstPage.aspx
</p>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="BodyPlaceHolderAfterForm" Runat="Server">
<p>
This body content is from the FirstPage.aspx
</p>
</asp:Content>