50 likes | 145 Views
.NET Programming. CodeBehind. Christopher M. Pascucci. 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.
E N D
.NET Programming CodeBehind Christopher M. Pascucci
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" %>
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 CodeBehindfile 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).
How an ASP.NET Application is Compiled • When an ASPX page is requested for the first time. • The CodeBehind class file and any other partial class files are compiled into an assembly (.dll). • 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. • 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. • 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.
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.