The Code-Behind Approach

The primary goal of the code-behind file is to enable you to write more modular code so that you can keep the code and layout physically separate. By keeping the code and layout separate, you can exploit the native object orientation of the .NET Framework to build a hierarchy of page classes that provide more and more specialized functions. As a result, you also have a little bit of page-level reusability, although you can’t reuse any elements of the layout.

With code-behind, you store directives and the page layout in one .ASPX file, and the code for them in a separate file written in Microsoft Visual Basic or Visual C#. The link to the code-behind file is defined by using an attribute of the @ Page directive.

 <%@ Page Language="C#" Inherits="MyBasePageClass" Src="MyBasePage.cs" %> ...

Get Building Web Solutions with ASP.NET and ADO.NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.