在具有一个母版页的ASP.net页面中实现两个表单,或者在两个位置显示相同的表单

时间:2016-05-14 10:19:18

标签: c# asp.net

如何在一个主页面的ASP.net页面的标题和其他主体中实现两个表单,或者如何在只有一个表单的两个地方显示相同的表单?

1 个答案:

答案 0 :(得分:1)

关于你的问题:

  

如何在一个主页面的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窗体的重要组成部分,并且多次使用。

您可以根据需要将尽可能多的<asp:Button>元素与不同的Click事件相关联,从而创建任意数量的表单

要在主文件中创建两个表单,一个在标题中,一个在正文中:

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

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

  3. 在.aspx中,请参阅ContentPlaceHolder元素以及相应的ContentPlaceHolderID s

  4. 在此示例中,您可以在“标题”部分中看到一个表单,在“正文”部分中看到另一个表单,如您所愿:

    <强> 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。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>
    
相关问题