Merging a Code-Behind File into Its APX File

Procedure

1. Remove the Inherits attribute from the Page directive.

2. Copy the contents of the class (NOT INCLUDING the public class line) into a <script runat='server' language='C#'> block.

3. Convert all the using statements in the code-behind file into <%@ Import namespace="value" %> directives in the ASPX file.

Example

Before (separate files)

ASPX Code

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" 
    Inherits="Acme.Web.Reports.ReportBrowser"
    %>

ASPX.CS Code

namespace Acme.Web.Reports
{
    public partial class ReportBrowser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        . . .
        }
    }
}

After (single ASPX file)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" 
    %>

. . .

<%@ Import namespace="System" %>
<%@ Import namespace="System.Collections.Generic" %>
<%@ Import namespace="System.Linq" %>
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Web.UI" %>
<%@ Import namespace="System.Web.UI.WebControls" %>
. . .

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script runat="server" language="C#">
protected void Page_Load(object sender, EventArgs e)
{
. . .
}
</script>