如何使用一个母版页实现两个表单,一个在 ASP.net 页的正文中,另一个在正文中实现,或者如何在只有一个表单的两个位置显示相同的表单?
关于你的问题:
如何使用一个母版页在页眉中实现两个表单,另一个在 ASP.net 页的正文中实现两个表单,或者如何在只有一个表单的两个位置显示相同的表单?
如果你需要选择:专注于第二部分。
Web 窗体中的常规方法是在页面中只有一个<form>
,即 。NET 的默认<form>
,它围绕 .aspx/.master 页的所有内容。
作为 HTML 状态的规则:一个 HTML 页面中不能有两个嵌套表单。
这意味着,如果您想在页面中有多个<form>
标签,则必须在.NET 的默认<form>
。
基本上它意味着所有形式之外的.NET 的 ONE 将不是视图状态的一部分,您将无法使用 ASP.NET Web 控件。
但是,如果您仍在考虑第一种方法,则可以在此处阅读有关它的更多信息:
我们可以在一个网页中使用多个表单吗?
你可以在这里看到一个非常好的实现它的例子:
在 ASP.NET Web 表单页面上使用多个表单
在两个位置显示相同的表单
基本上,它是 Web 窗体的重要组成部分,并且被多次使用。
通过将任意数量的<asp:Button>
元素关联到不同的Click
事件,您可以创建任意数量的表单。
要在主控形状中创建两个表单,一个在标题中,一个在正文中:
将表单内容放在两个部分中,并在代码隐藏中使用两个不同的提交按钮处理程序(请参阅下面的示例(
放入您的
MasterPage
多个ContentPlaceHolder
元素。对于要从.aspx文件加载内容的每个位置,请使用一个。在您的.aspx中,引用具有相应
ContentPlaceHolderID
s 的ContentPlaceHolder
元素
"正文"部分看到另一个窗体,如您所愿:
MasterPageTwoSection.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>
母校页面两节.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)
{
}
}
首页.aspx
注意 Content2
指的是母版页中的标题占位符。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>