如何避免嵌套表单情况?

时间:2015-09-17 12:23:35

标签: asp.net forms visual-studio master-pages

好吧,基本上我有一个母版页,其中包含runat服务器的Form中的所有设计,如下所示:

<head runat="server">
  <title></title>
  <asp:ContentPlaceHolder ID="head" runat="server">
  </asp:ContentPlaceHolder>
</head>
<body>
  <form id="form1" runat="server">

    <table style="font-family: Arial">
      <tr>
        <td colspan="2" style="width: 800px; height: 80px; background-color: #3399FF; text-align: center">
          <h1>WebSite Header
          </h1>
        </td>
      </tr>
      <tr>
        <td style="height: 500px; background-color: #66FFFF; width: 150px">
          <h3>Menu</h3>
        </td>
        <td style="height: 500px; background-color: #9999FF; width: 650px">
          <h3>content</h3>
          <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
              <p>
                &nbsp;</p>
            </asp:ContentPlaceHolder>
          </div>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="background-color: #3399FF; text-align: center">
          <b>Website Footer</b>
        </td>
      </tr>
    </table>


  </form>
</body>
</html>

现在我正在尝试使用这个母版页做一个页面,在这个页面中,我正在初始化一个CKEditor,显然需要使用runat服务器才能运行,如下所示:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="CkEditor.aspx.cs" Inherits="WebApplication5.CkEditor" %>

<%@ Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

  <form id="form2" runat="server">
    <div>
      <div>
        <CKEditor:CKEditorControl ID="CKEditor1" runat="server">
        </CKEditor:CKEditorControl>
      </div>
      <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
      <asp:Label ID="lblText" runat="server" Text=" "></asp:Label>
    </div>
  </form>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

然而,表单中包含CKEditor的表单不允许初始化,因为它正在成为嵌套表单,因为母版页本身是以表单绑定的。

我该如何防止这种情况?

1 个答案:

答案 0 :(得分:1)

您的CKEditorControl将从母版页继承该表单。

首先,将asp:ContentPlaceHolder移至head标记:

<head runat="server">
<title></title>
<!--DON'T PUT IT HERE-->
</head>
<body>
   <form id="form1" runat="server">
      <asp:ContentPlaceHolder ID="head" runat="server">
      </asp:ContentPlaceHolder>
...

现在您可以删除form2

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<!--<form id="form2" runat="server">-->
  <div>
    <div>
      <CKEditor:CKEditorControl ID="CKEditor1" runat="server">
      </CKEditor:CKEditorControl>
...