【问题标题】:Implement two forms in an ASP.net page with one master page, or display the same form at two places用一个母版页在一个 ASP.net 页面中实现两个表单,或者在两个地方显示相同的表单
【发布时间】:2016-05-14 10:19:17
【问题描述】:

如何在一个母版页的 ASP.net 页面的页眉和正文中实现两种表单,或者如何在只有一个表单的两个地方显示相同的表单?

【问题讨论】:

  • 你到底想做什么?如果您添加一些代码可能会有所帮助。您可以在 asp.net 网络表单中拥有多个按钮,每个按钮都针对代码隐藏中的不同点击事件,这实际上与拥有多个表单相同。或者,您总是可以只使用没有 runat="server" 属性的普通 html 表单。您仍然可以在目标页面的代码隐藏中使用 Request.Form 检索值

标签: c# asp.net


【解决方案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 Forms的重要组成部分,被多次使用。

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

在master中制作两种形式,一种在header中,一种在body中:

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

  2. 放入您的MasterPage 多个ContentPlaceHolder 元素。为您希望从 .aspx 文件加载内容的每个位置使用一个。

  3. 在您的 .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。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>

【讨论】:

  • 我想要两个
    标签,一个在标题中,另一个在主体中。当我实现它时抛出一个错误“一个asp.net页面只能有一个带有runat ='server'的表单”。
  • @shakti 这是因为在 Web 表单中您根本无法使用 &lt;form&gt; 标记。一个aspx 和/或母版页已经包含&lt;form&gt; 标记,该标记围绕所有内容。当您将&lt;form&gt; 标签放入其中时,它会导致嵌套的&lt;form&gt; 情况,这在html 中是被禁止的。我会编辑我的答案并把它也放上去
  • @Idanb 这并不完全正确。正确的是,您只能拥有一个带有服务器端控件的表单,并且通常的做法是将所有内容都包含在 aspx 页面的正文部分中。但是,如果您愿意,您可以拥有多个表单,只要其中一个具有 runat="server" 属性,显然您只能在其他表单中使用客户端 html 元素
  • @John 据我了解,这不是问题所在。我的回答提到了正常的 Web 表单情况,您希望将所有内容都放在好的 ol 表单中,并享受框架的功能。然而,你是对的。有一条小路。我编辑了我的答案以使其更加清晰并给予它荣誉。谢谢你的来信
  • @Idanb 最初的问题有点模棱两可,就像这个网站上的很多问题一样。您在答案中付出的努力值得一票
猜你喜欢
  • 2015-05-13
  • 2012-07-19
  • 2014-07-22
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多