繁体   English   中英

在具有一个母版页的ASP.net页中实现两种形式,或在两个位置显示相同的形式

[英]Implement two forms in an ASP.net page with one master page, or display the same form at two places

如何在带有一个母版页的ASP.net页的主体中实现两种形式,一种在页眉中,另一种在正文中,或者如何只用一种表单在两个地方显示相同的表单?

关于你的问题:

如何在带有一个母版页的ASP.net页的主体中实现两种形式,一种在标题中,另一种在主体中, 或者如何只用一种形式在两个地方显示相同的形式

如果您需要选择:专注于第二部分。

Web窗体中的常规方法是在页面中仅包含一个<form> ,这是.NET的默认<form> ,它围绕.aspx / .master页的所有内容。

作为HTML状态的规则: HTML页面中不能有两个嵌套表单

这意味着,如果要在页面中包含多个<form>标记,则必须在.NET的默认<form>之外使用它。

基本上,这意味着.NET之外的所有形式都不会成为视图状态的一部分,并且您将无法使用ASP.NET Web控件。

但是,如果您仍在考虑第一种方法,则可以在此处阅读有关此方法的更多信息:

我们可以在网页中使用多种形式吗?

您可以在此处看到一个很好的示例来实现它:

在ASP.NET Web表单页面上使用多个表单

在两个地方显示相同的表格

基本上,它是Web窗体的重要组成部分,已被多次使用。

通过将任意数量的<asp:Button>元素与不同的Click事件相关联,可以创建任意数量的表单

要在母版中制作两种形式,一种在标头中,一种在主体中:

  1. 将表单内容放在两部分中,并在后面的代码中使用两个不同的提交按钮处理程序 (请参见下面的示例)

  2. 在您的MasterPage放入多个ContentPlaceHolder元素。 对于要从​​.aspx文件加载内容的每个位置,请使用一个。

  3. 在您的.aspx中,请参考具有相应ContentPlaceHolderIDContentPlaceHolder元素

在此示例中,您可以在“标题”部分中看到一种形式,而在“正文”部分中看到另一种形式:

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引用母版页中的HeaderPlaceHolder。 Content3Content4引用BodyPlaceHolderBeforeFormBodyPlaceHolderAfterForm

<%@ 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM