christopher m. pascucci.net programming codebehind

5
Christopher M. Pascucci .NET Programming CodeBehind

Upload: stanley-hubbard

Post on 14-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Christopher M. Pascucci.NET Programming CodeBehind

Christopher M. Pascucci

.NET Programming.NET Programming

CodeBehindCodeBehind

Page 2: Christopher M. Pascucci.NET Programming CodeBehind

What is a CodeBehind? The ASPX file contains HTML and the ASP tags for the server

controls (WFC). A CodeBehind refers to a separate class file that contains code for

your ASP.NET page. The code is one large class (or partial classes) that is compiled and runs your

ASP.NET page. This code is processed on the server.

The CodeBehind file replaces the need to use inline ASP scripting and script tags Inline scripting <% %> Tags <script language=“vbscript” runat=“server”>

Major advantage is that is allows for a clean separation of the HTML markup (design of the page) and the logic of the page.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Login.aspx.vb" Inherits=“Login" %>

Page 3: Christopher M. Pascucci.NET Programming CodeBehind

CodeBehind When you add an ASPX WebForm page to your

project in Visual Studio it also adds to files attached to the aspx file. The Designer file contains code automatically

generated by the designer when you use the Design mode in Visual Studio

• It has the same name as the aspx file, but adds the file extension designer.vb

Example: Order.aspx.designer.vb (for VB) OR Order.aspx.designer.cs (for C#)

• This code is stored in a separate file to create a less cluttered programming environment in a partial class.

The CodeBehind file contains server-side code & events that run your page.

• It has the same name as the aspx file, but has the file extension .vb

Example: Order.aspx.vb

These two files are compiled into an assembly (.dll file).

Page 4: Christopher M. Pascucci.NET Programming CodeBehind

How an ASP.NET Application is Compiled When an ASPX page is requested for the first time.

1. The CodeBehind class file and any other partial class files are compiled into an assembly (.dll).

2. The ASP.NET runtime will call the VB compiler to compile any other classes that were created.

• A product class that you may have created to store information about a single product.• A cart class that you may have created to store products.• These classes are called components that don’t have any affect on the processing of the page and

are created by the programmer as a utility.

3. ASP.NET creates an instance of the page and raises the appropriate events like Page_Load.

• This only occurs after all files have been compiled into assemblies.

4. Finally, generates the HTML that is passed back to the Web Server for the response.

These steps only occur the first time an aspx page is requested. After that, the page is processed directly from the compiled assemblies (.dll files).

These saved assemblies are reused so the application doesn’t need to be recompiled.

Page 5: Christopher M. Pascucci.NET Programming CodeBehind

Summary The CodeBehind class is automatically instantiated.

The class file (blueprint) becomes an object that can be used.

When the ASPX page has been completely processed and its HTML has been downloaded to the browser , the CodeBehind object is destroyed. All objects within the class will be re-intialized. Anything declared or used in the CodeBehind is gone unless you

programmatically maintain state. When another request is made (postback) the CodeBehind class is instantiate

again and all the objects are initialized. This occurs over and over again for each postback.