Search code examples
asp.netformsvisual-studiomaster-pages

How can I avoid a nested form situation?


Ok basically I have a master page which has all the design in a Form with runat server as follows:

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

Now I am trying to do a page using this master page and in this page I am initializing a CKEditor which apparently needs to be in a form with runat server to be able to function, as follows:

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

The Form however with the CKEditor in it is not allowing to be initialized because it is becoming a Nested Form, since the master page itself is bound in a form.

How can I prevent this please?


Solution

  • Your CKEditorControl will inherit the form from the master page.

    First, move your asp:ContentPlaceHolder from the head tag:

    <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>
    ...
    

    Now you can remove the form2:

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