ASP.NET Tutorial

ASP.NET is part of the .NET Framework and is a techonology that allows for the dynamic creation of documents on a web server when they are request via HTTP.

The .NET Framework

The .NET in ASP.NET comes from the .NET Framework. It is a set of objects and blueprints from Microsoft for building applications. The .NET framework provides the functionality of ASP.NET.

Common Language Runtime

The Common Language Runtime, or CLR, manages the execution of code. It executes and maintains the code written. When code is written for the .NET Framework, it is compiled into something called Microsoft Intermediate Language (MSIL) which is a type of byte code. When this code is executed for the first time, the CLR compiles it into a computer's native language, also known as machine code, and then executes it. The MSIL is what is distributed and the idea is that one can distribute it on to any type of computer with the .NET CLR installed and it will run. Code that works with the CLR is called managed code. .NET programs can be written in any language supported by the CLR and they all compile into the same binary machine code.

HTTP: the stateless protocol

Web browsers and Internet servers communicate with each other through the HyperText Transfer Protocol (HTTP). It is a stateless protocol meaning that the browser connects, downloads what it needs, and disconnects. The connection does not persist to allow for further communication the way a private network does. The most common requests that browsers make with the server are GET and POST requests. GET requests typically do not change the state of the server while POST requests typically do. Opening the home page of a site is an example of a GET request. Adding items to a shopping cart typically would be added via a POST request. But, ASP.NET hides all this (and more) from the programmer using an event-driven programming model.

ASP.NET Pages

ASP.NET pages are text files. In fact, they are special HTML files — processed HTML files. ASP.NET pages have the .aspx file extension. This is how the server they are installed on knows how to interpret them as ASP.NET files versus just plain HTML files. Here is an example ASP.NET page that fires the Page_Load event whenever it is accessed by a browser:

<%@ Page Language="VB" %>

<script runat="server">
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
		Label1.InnerHtml = "Hello World!"
	End Sub
</script>

<html>
<head><title>Hello World Example</title></head>
<body>
	<span id="Label1" runat="server"></span>
</body>
</html>
ASP.NET Hello World!

All ASP.NET pages contains a Page directive similar to <%@ Page Language="VB" %>. This one tells .NET that the Visual Basic.NET language will be used. The code block declaration <script runat="server"> tells ASP.NET that one wants the code inside this block to be executed by the server — as a server-side script — as opposed to being executed by the browser.  The code in this block is compiled into MSIL and then, when run for the first time, the CLR compiles it into native, machine code. The HTML portion of the ASP.NET page begins at the <html> tag. This is what is sent to the browser along with the output from the code executed in the code block. In this case, the code outputs "Hello World!".

There is an HTML SPAN tag declared as <span id="Label1" runat="server">. The runat="server" attribute tells ASP.NET that this to be available as an object to the script being run server-side. The id="Label1" attribute is the name of the object and how it is referenced in the server-side script, in this case by the line Label1.InnerHtml = "Hello World!". This object is known as a Server Control and its server-side properties are somewhat similar to its client-side properties exposed to JavaScript.

ASP.NET pages have one and only one <form> element with the runat="server" attribute specified. This type of ASP.NET page is called a web form and it is the most common. Under the covers, web forms always submit an HTTP POST request to implement their event-driven model. Here is an example of one firing a Button_Click event on the server whenever the button is clicked in the browser:

<< [Page 1 of 2] >>