The Code Behind • Server programs are written in a separate file known as the code behind the page • By separating the programming logic and presentation layer, the application becomes easier to maintain • Only Server controls can interact with the code behind the page – Written in any ASP.NET compatible language such as Visual Basic .NET, C#, Perl, or Java – Filename is the same as the WebForm filename – Add a file extension that identifies the language • Visual Basic .NET use .vb (mypage.aspx.vb) • C# use .cs (mypage.aspx.cs)
Code Behind file • The location of the code behind the page is determined via a property that is set on the first line in the page using the @Page directive <%@ Page Language="vb" Codebehind="WebForm1.vb" Inherits=“MyFirstApp.WebForm1"%>
• The @Page directive allows you to set the default properties for the entire page such as the default language • The CodeBehind property identifies the path and filename of the code behind file • The Inherits property indicates that the code behind the page inherits the page class • This page class contains the compiled code for this page
Compiling the Page Class • The compiled code behind the page is the class definition for the page – A class is a named logical grouping of code – The class definition contains the functions, methods, and properties that belong to that class
• In Visual Studio .NET the process of compiling a class is called building – When you build the application, you compile the code into an executable file – Visual Studio .NET compiles the code behind the page into an executable file and places the file in the bin directory
Page Class Events • The Page Class consists of a variety of methods, functions, and properties that can be accessed within the code behind the page • The first time a page is requested by a client, a series of page events occurs • The first page event is the Page_Init event which initializes the page control hierarchy • The Page_Load event loads any server controls into memory and occurs every time the page is executed
Page class events • Page_init • Page_load • Server_Controls • Page_prerender • Page_Unload
Web Services â&#x20AC;˘ Web Services also provide a means to expose .NET functionality on the web but Web Services expose functionality via XML and SOAP (cf: function calls over the web)
Web Services • If your business partner is Course Technology and you want to query that company’s product catalog from your Web site, you could: – Post a link – Scrape a Web site (use a program to view a Web site and capture the source code) – Provide a Web Service to their catalog application
• Web Services are used to create businessto-business applications – Web Services allow you to expose part or all of your programs over the Internet. The Web Service source file has the extension .asmx – A public registry known as UDDI contains registered public Web Services. Third party Web Services are
How ASP.NET works • When .NET is installed, IIS is configured to look for files with the .aspx extension and to use the ASP.NET module (aspnet_isapi.dll) to handle them. • ASP.NET parses the .aspx file and arranges it in a predefined class definition and generates an asp.net page object. • The page object generates html that is sent back to IIS and then the browser. • NOTE: only .aspx files are parsed (if it is pure html don’t save it as an aspx file as it will slow down the server.
ASP.NET samples • Page directives: <%@ page language = “VB” debug="true" trace="true“ %> • <script language = “VB” runat=“server”> VB.NET code declarations ……….. </script> • Message.aspx <html> <head> <title>Inserting ASP.NET code Example</title> </head> <body> Line1: First HTML Line<br /> Line2: Second HTML Line<br /> Line3: Third HTML Line<br /> </body> </html>
â&#x20AC;˘ Message2.aspx <script language="VB" runat="server"> Sub Page_Load() Response.Write ("First ASP.NET Line<br />") Response.Write ("Second ASP.NET Line<br />") Response.Write ("Third ASP.NET Line<br />") End Sub </script> <html> <head> <title>Inserting ASP.NET code Example</TITLE> </head> <body> Line1: First HTML Line<br /> Line2: Second HTML Line<br /> Line3: Third HTML Line<br /> </body> </html>
â&#x20AC;˘ Message3.aspx html> <head><title>Inserting ASP.NET code Example</title></head> <body> Line1: First HTML Line<br /> Line2: Second HTML Line<br /> Line3: Third HTML Line<br /> <script language="VB" runat="server"> Sub Page_Load() Response.Write ("First ASP.NET Line<br />") Response.Write ("Second ASP.NET Line<br />") Response.Write ("Third ASP.NET Line<br />") End Sub </script> </body> </html>
Render or inline code block â&#x20AC;&#x201C; interweave1.aspx
<html> <head> <title>Interweaving ASP.NET code and HTML Example</title> </head> <body> Line1: First HTML Line<br /> <% Response.Write ("First ASP.NET Line<br />") %> Line2: Second HTML Line<br /> <% Response.Write ("Second ASP.NET Line<br />") %> Line3: Third HTML Line<br /> <% Response.Write ("Third ASP.NET Line<br />") %> </body> </html> NOT RECOMMENDED.
Interweave2.aspx A Server control script language="VB" runat="server"> Sub Page_Load() Message.Text="The ASP.NET line" End Sub </script> <html> <head> <title>Inserting ASP.NET code Example</TITLE> </head> <body> First HTML Line<br/> <asp:label id=Message runat="server"/> <br /> Second HTML Line<br/> </body>
Web application project files AssemblyInfo.vb
Info about the compiled project file stored in /bin and named project.dll
Global.asax
Event handler commands visible to all web forms in a project
Global.asax.resx
Define application resources such as text strings, images. Can change without recompiling project.
Global.asax.vb
Asp.net code for application events eg session.start
Project.sln
Stores links to all project files
Project.suo
VS.NET IDE configuration info for the proj.
Project.vbproj
Configuration and build settings for project files.
Web application project files cont. Project.vbproj.webinfo
URL to project web server
Project.vsdisco
Enables search for web services
Styles.css
Project style sheet
Web.config
Project and folder configuration information
Webform.aspx
Web form .aspx file;Html
Webform.aspx.resx
Resources in corresponding web form
Webform.aspx.vb
Code written for the form (code behind)
Bin\project.dll
Compiled project output file (assembly)
Bin\project.pdb
Debugging information used by developer
Viewing the Assembly â&#x20AC;˘ Create a simple class, compile the class into an assembly, then view the class using the IL Disassembler â&#x20AC;˘ Open Notepad and type the code shown: ' hello.vb - displays hello world ' Created 06/01/2002 Imports System Public Module Hello Sub Main() Dim s1 As String = "1 - Hello World" Console.WriteLine(s1) End Sub End Module ' Run this at the command line ' vbc hello.vb
Using the ILDASM to View the Assembly and Classes
Using the ILDASM to view the assembly and classes
Examples • quickstart – webforms – – – – – – –
Intro4 shows VIEWSTATE Intro6 shows a click event Intro7 shows a usercontrol with a calander Intro8 shows a db connection Intro9 & 10 show asp.net templates Intro11shows validation controls Intro13 shows code behind pages
• Server directives eg trace and debug – trace
The lab environment. • Each machine is set up to be an IIS server – http://localhost:1900/….. • You create your web projects with Visual Studio.Net. VS.NET will create a subdirectory in c:/inetpub/wwwroot for your project. You must copy this subdirectory when moving to another machine or home. • URL – http://localhost:1900/MyfirstApp/homepage.aspx
• Alternative to VS.Net is webmatrix • Some samples on another machine – http://interdev.csse.monash.edu.au/cse2030/ Interdev is not accessible outside the Monash network.
Feature
ASP.NET Vs PHP PHP
ASP.NET
HTML
Yes
Yes
CSS
Yes
Yes
‘php Templates’
Yes
UserControls
ServerControls (buttons,grids etc)
No
Yes
Javascript
Yes
Yes + Validation controls
Database Conn
Yes
Yes
Cookies & Sessions
Yes
Yes
VIEWSTATE
No
Yes
POSTBACK
No
Yes