Introduction
Traditional ASP pages are written with HTML elements, interspersed with server side code in <% %> code blocks. The web page is contained in a single file. ASP.NET introduces a new programming model for Web Forms to separate the code and visual elements in different files. Let us look at the options for organizing code available with ASP.Net and their advantages and disadvantages.
Two Models for Web Forms Pages
Web Forms consist of two parts: visual elements and code logic. Web Forms pages support two models : Code-behind Web Forms and Single File Web Forms.
In the Single File Web Forms model, the code and the visual elements are included in the same file. The code is contained in <script> blocks in the same file that contains the visual elements. This is similar to the ASP programming model where the HTML elements and <% %> tags containing server side code are contained in the same file.
In the code-behind programming model, the visual elements are contained in a .aspx file and the code is included in a separate class file. This results in a separation of code and UI resulting in web pages which are easy to maintain. This programming model is similar in some ways to the model used by Visual Basic.
Details of Single-File Web Forms Model
The Single-File Web Forms are created as .aspx pages containing both the HTML layout elements and the server-side code in <script> blocks. The .aspx file derives from the Page class. The Single-File Web Form is functionally similar to Code-Behind Web Forms and you can use the same controls and code as for Code-Behind pages. The user browses to the Web Forms page and the page is run using server side code and returns HTML to the client. When the page is deployed, the source code is deployed along with the page, however the source code is not visible to the user as only the results are rendered when the page runs.
This programming model has limited support in Visual Studio .Net
Compilation Process for Code-Behind pages
A single Web Forms page consists of 2 files when created using the Code-Behind model : a .aspx file containing the UI elements and a .aspx.vb or .aspx.cs file which contains the server side code for the .aspx page.
Visual Studio.Net uses the Code-Behind model by default.
The code behind file derives from the Page class which is indicated in code as follows:
public class WebForm1 : System.Web.UI.Page
The .aspx file derives from the code-behind file and this is indicated using the Inherits attribute of the @Page directive. The Src attribute of the @Page directive indicates the class file to dynamically compile when the page is requested.
Visual Studio.Net does not use the Src attribute. Instead, it precompiles the code-behind classes and then uses the Inherits attributes.
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
The code-behind files for all Web Forms in the project are compiled into the dll file produced by the project. The first time a user requests for a Web Forms page, ASP.Net automatically generates a .Net class that represents the page and compiles it into a second dll file. Whenever the Web Forms page is subsequently requested, the same dll is executed. The Web Forms page is deployed without any source code.
Join the conversation! Your thoughts help the community grow.