ASP.NET Primer

In this lesson of the Silverlight tutorial, you will learn...
  1. Why the .NET Framework was created
  2. Brief details about the two most common .NET Framework programming languages
  3. How to create Web applications by using ASP.NET

This lesson will illustrate the .NET Framework and ASP.NET.

The .NET Framework

Microsoft introduced the .NET Framework in 2000 as its new approach to software development. The .NET Framework borrowed ideas from the best practices in the software industry as well as brought new ideas to the table in an effort to meet developer needs and requests. The .NET Framework was designed in an attempt to consolidate all Microsoft development technologies into an all-encompassing and standardized solution. The .NET Framework includes thousands of classes that contain properties, events, and methods that offer a broad scope of functionality. Practically all Microsoft technologies have been designed using the .NET Framework or have been redesigned to integrate natively with the .NET Framework.

As an example, Microsoft SQL Server 2005, one of the leading relational database management systems on the market, was designed to integrate natively with the .NET Framework. As a result, SQL Server 2005 provides not only powerful relational database management functionality and features but can be fully extended by utilizing assemblies created using the .NET Framework. To illustrate the proliferation and influence of the .NET Framework in the industry, other leading relational database management systems in the industry, including IBM's DB2 and Oracle, were redesigned to follow suit by making external calls to assemblies created using the .NET Framework.

Tools

In addition to all Microsoft development technologies being consolidated into the .NET Framework, all Microsoft development tools have been or are being consolidated into a single integrated development environment (IDE), Visual Studio. Visual Studio is currently in version 2008 and is used for development of all .NET Framework functionality and applications. Many other development tools, such as the Enterprise Manager and Query Analyzer that were available in SQL Server 2000, now utilize Visual Studio, through the SQL Server Management Studio and the Business Intelligence Development Studio, for all development and management tasks. The figure below shows Visual Studio 2008.

Versions

Multiple versions of the .NET Framework have been released. The first version, version 1.0, as mentioned above, was released in 2000. Version 1.0 offered extensive functionality and each version thereafter has extended that functionality or introduced new technologies. The .NET Framework version 1.1 was released in 2003 and offered enhanced mobile device development, networking integration, and security features.

The .NET Framework version 2.0 was released in 2005 and brought about some significant enhancements and new concepts in .NET development. In the realm of ASP.NET alone, version 2.0 brought master pages, themes, new security-related controls, and better standards compliance.

The .NET Framework version 3.0 introduced some ground-breaking technologies including Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Workflow Foundation (WF), and Windows CardSpace. WPF is used to deliver phenomenal user interfaces and is the technology that the Windows Vista UI is built upon. WCF replaces .NET remoting and adds to the functionality previously available in the .NET Framework for developing distributed applications. WF is used to model complex workflows and business processes. Windows CardSpace is a new paradigm in managing sensitive and private user information.

The most recent version of the .NET Framework (as of the time of this writing) is the .NET Framework version 3.5. Version 3.5 bridges the gap between application logic and data storage through the introduction of Language Integrated Query (LINQ). Ajax was integrated into ASP.NET in version 2.0 but was available as an additional download. In the .NET Framework version 3.5, support for Ajax is now built into ASP.NET as ASP.NET AJAX. The .NET Framework technologies are also fully integrated into Visual Studio 2008.

Common Language Runtime (CLR)

A foundational (and, arguably, the primary) component of the .NET Framework is the Common Language Runtime (CLR). Code written by using a .NET-compliant programming language is compiled into an assembly. The CLR is responsible for loading, compiling, and managing the execution and lifetime of an assembly.

Programming Languages

There are currently (as of the time of this writing) approximately 113 programming languages and language compilers that are considered .NET-compliant. The two most commonly used .NET programming languages are Visual Basic and Visual C# from Microsoft. Microsoft is introducing other very popular up-and-coming programming languages as well including F# and IronPython.

Visual Basic for .NET may seem like an easy choice for developers that are migrating from earlier versions of Visual Basic. However, with the advent of .NET, Visual Basic was completely overhauled to overcome many of the shortcomings of earlier versions of Visual Basic in comparison to more powerful programming languages. When Visual Basic was redesigned for .NET, it was made a fully object oriented programming language on the same level as other programming languages in the C family of languages. Hence, migrating to Visual Basic for .NET maybe just as large of a hurdle as migrating to C#.

C# has been the primary at Microsoft since the .NET Framework was released with a few exceptions. C# has also become one of the primary programming languages used in the industry. C# was created as a brand new programming language that was designed specifically for use with the .NET Framework. C# is a powerful language that borrows from C++ but is most similar to the Java programming language in syntax.

Assemblies

Code written using a .NET-compliant programming language is initially compiled into IL in the form of either an executable file (.exe) or a dynamic link library (.dll). The two outputs differ in their execution behavior and are referred to based on the manner in which they communicate with other applications, which is, in most scenarios, a Web server. Applications that run in a Windows environment are referred to as "processes". Every process may have multiple "threads" of execution operating in the process.

An executable file (.exe) is called directly when the application is started and it is loaded into separate memory that is dedicated to the application. Because an executable file is executed in a memory space separate from any other processes that may call it, it is referred to as an "out-of-process" application. Out-of-process applications are typically very stable because they are isolated from other applications and the operating system should anything go wrong.

A dynamic link library (.dll) cannot be called directly to be executed but must be loaded into memory by a host application, such as a Web server. As such, a dynamic link library is loaded into the same memory space as the process that loads and hosts it and it is referred to as an "in-process" application. In-process applications must be thoroughly debugged so as to not crash the host application should a bug be encountered. Communications between a host application and an in-process application are faster than communication with an out-of-process application due to the shared memory.

Microsoft Intermediate Language (MSIL)

When code written using any .NET-compliant language is compiled, a common text-based result, called Intermediate Language (IL), is rendered. If IL is generated by code written using a Microsoft .NET programming language, the IL is referred to as MSIL. Since IL is in a text-based, common format, once created, it can be deployed to any operating system supported by the CLR. The first time the application represented by the IL is executed, the CLR will compile the IL to native code (machine language for that operating system using a just-in-time compilation system. Development environments similar to the .NET Framework interpret code deployed in this manner instead of compile it. Compiled code typically provides a tenfold increase in performance as compared to interpreted code.

Currently, the CLR is only available for Microsoft Windows. However, there is an open source version of the CLR, called Mono (the Mono Project), that is available for Linux.

The figure below illustrates the process of creating code using a .NET-compliant programming language through the CLR compilation.

.NET Framework Base Class Library (BCL)

At a level higher than .NET-compliant programming code being compiled, the programming languages have access to all of the classes, interfaces, delegates, properties, events, and methods and other types and functionality provided by the .NET Framework. This functionality is contained in the Base Class Library (BCL). The BCL may also be referred to as the Framework Class Library (FCL) depending upon the documentation source. The functionality contained in the BCL is categorized into namespaces. A high-level overview of the BCL through the .NET Framework version 2.0 is illustrated in the figure below.

The classes contained in the BCL may be explored and searched by using the Visual Studio 2008 Object Browser as shown in the figure below.

Common Type System (CTS)

Virtually all programming languages manage data at some point. The primary reason that communication between applications created using legacy languages was difficult was that each language stored data being managed using data types that were particular, and normally exclusive, to a single language. Hence, an integer in one language may not represent an integer in another language and, in order to communicate between languages, data being managed by each language needed to be converted to common data types.

The .NET Framework remedied the programming language communication quagmire by introducing a common set of data types that must be utilized by all programming languages that proport to be .NET-compliant. Thus all .NET-compliant programming languages support a common set of data types and can easily intercommunicate. The common set of data types utilized in the .NET Framework is called, not surprisingly, the Common Type System (CTS).

The beauty of creating applications using the .NET Framework is that a developer can develop applications using any .NET-compliant language or a combination of languages. All .NET-compliant languages should intercommunicate seamlessly due to all languages utilizing the CTS and all languages generating common IL.

Programming Language Overview

As stated above, there are approximately 113 .NET-compliant programming languages and compilers as of the time of this writing. Hence, coverage of all .NET-compliant languages is beyond the scope of this course and, as well, is not the focus of this course. Furthermore, thorough coverage of the primary programming language utilized throughout this course is also beyond the scope of this course. Prerequisite knowledge of the programming language will be assumed throughout the course although examples given will be thoroughly documented and references to exhaustive programming language resources will be given.

Additionally, as stated above, due to the nature of the .NET Framework and the CLR, all .NET-compliant programming languages must emit standardized IL. Thus, beyond the point of actually writing the code, a choice of programming language is almost trivial and is really a developer's preference for syntax. Note that there are some slight differences in the IL emitted by some of the programming languages but the differences are generally negligible.

With that said, three .NET programming languages are illustrated below. Two of the programming languages illustrated, C# and Visual Basic, are the two most commonly used .NET programming languages while the third, F#, is a brand new language from Microsoft Research that is quickly gaining popularity amongst developers.

C#

C#, referred to as Visual C#.NET in its first version and now referred to as C# 3.0, was specifically created for use in .NET and has been Microsoft's default .NET language from its inception. C# is extremely powerful, fully object oriented, and very popular. C# was created by Anders Hejlsberg, who is a staple of the software development community and also led the development of the Delphi development environment. C# and the .NET Framework, due to their incredible popularity and acceptance in the industry, were adopted as ECMA and ISO standards thus bolstering their industry acceptance.

A Simple Example

The following simple C# program is a console application created using Visual Studio 2008. It is designed to run at the command prompt and it the simplest type of application to create using a .NET programming language. The example shown below is a minute sampling of the C# programming language syntax.

Code Sample: ASPNETPrimer/Demos/SimpleCS/SimpleCS/Program.cs

using System;

namespace SimpleCS
{
    class Program
    {
        static void Main(string[] args)
        {

            // Create an array of players.
            string[] players = new string[4] {"Kurt Warner","Anquan Boldin","Larry Fitzgerald", "Edgerin James"};

            // Display a report header.
            Console.WriteLine("FANTASY PLAYER REPORT");
            Console.WriteLine("==================================");

            // Iterate through the array contents
            // and display the report.
            foreach (string player in players) {

                Console.WriteLine("Player: " + player);
            }
        }
    }
}
Code Explanation

As noted in the code comments, the code sample above (and that used in the following code examples, illustrates a simple C# namespace and class declaration that, in the Main startup method, creates an array of strings and displays the results of the array at the command prompt. The output of the sample program is show in the figure below.

Visual Basic

Visual Basic, referred to as Visual Basic.NET in its first version and now referred to as Visual Basic 2008, was completely redesigned with the advent of the .NET Framework. In fact, Visual Basic was rewritten as a .NET programming language using C#. Earlier versions of Visual Basic were often times referred to as a "toy" language by developers in more advanced languages such as C++. However, Visual Basic made performing many tasks that were complex and cumbersome to complete in more advanced languages very simple to complete in Visual Basic, and, as a result, Visual Basic became one of the most commonly used programming languages in the industry. Even so, due to attempts to be completely backward compatible with earlier versions of Visual Basic and Microsoft Windows, versions of Visual Basic prior to .NET were overburdened with a memory-consuming API, were not object oriented, and lacked many of the more advanced features offered by other programming languages.

Visual Basic .NET (and now Visual Basic 2008) is a new animal and a first class citizen and leading language in the gamut of .NET Framework programming languages. Visual Basic is now lightweight, streamlined, and optimized, fully object oriented, and able to take advantage of all of the features any of the other .NET programming languages can offer. Furthermore, Visual Basic has gained popularity and now offers some features that other languages lack.

A Simple Example

The code sample below completes the exact same task and output as the C# example above. The code is illustrated here to give a sampling of the Visual Basic syntax in .NET as compared to C# syntax.

Code Sample: ASPNETPrimer/Demos/SimpleVB/SimpleVB/Module1.vb

Module SimpleVB

Sub Main()

  ' Create an array of players.
  Dim players() As String = New String() {"Kurt Warner", "Anquan Boldin", "Larry Fitzgerald", "Edgerin James"}

  ' Display a report header.
  Console.WriteLine("FANTASY PLAYER REPORT")
  Console.WriteLine("==================================")

  ' Iterate through the array contents
  ' and display the report.
  For Each player As String In players

    Console.WriteLine("Player:" + player)
  Next
End Sub
End Module

ASP.NET

Active Server Pages, now referred to as classic ASP, was Microsoft's first real attempt at a dynamic Web development technology and it became widely used and incredibly popular. However, as with any early technology, classic ASP did have shortcomings and presented Web developers with challenges due to advances in Web development and hosting architecture that were not foreseen or anticipated when classic ASP was introduced.

When Microsoft was designing the .NET Framework, it completely revamped classic ASP to serve as the Web development facet of the .NET Framework. At that time, Microsoft redesigned classic ASP into ASP.NET. ASP.NET retained some of the good aspects of classic ASP while including a plethora of enhancements that catapulted ASP.NET beyond all competing Web development technologies. ASP.NET is now in version 2.0 and is considered the leading Web development technology available.

ASP.NET is hosted on a Windows Server through Microsoft Internet Information Server (IIS). The ASP.NET engine (part of the CLR) is loaded when a page with an .aspx extension (the ASP.NET extension) is requested by a client. The ASP.NET page is processed on the server and the resulting HTML is sent to the client. The figure below illustrates the ASP.NET architecture.

ASP.NET 2.0 is fully XHTML-compliant and WAI-compliant, by default, when authored by using Visual Studio 2008. ASP.NET pages, by default, are comprised of two files, a file that contains the markup for the page and is assigned a .aspx extension and a second file that contains code that responds to events in the markup page and is assigned an extension based on the programming language that the code contained in the file is written in (.cs for C#, .vb for Visual Basic). The second file that contains the programming language code is associated with the file that contains the page markup through a declaration at the top of the file that contains the markup. The second file that contains the programming language code is referred to as the "code behind" file because, for evident reasons, it contains code for the markup and exists in the background, behind the markup file. The code that is contained in the code behind file is processed on the server prior to the resulting markup being sent to the user's browser, hence it is called server-side code. In contrast, code, such as JavaScript, that is processed by the user's browser on the client's machine is called client-side code.

ASP.NET is mentioned in this module as a supporting technology for Microsoft Silverlight. It is important to understand how to develop Web content by using ASP.NET and Visual Studio 2008 in order to become a well-rounded Silverlight developer. However, coverage of ASP.NET, beyond a short example, is beyond the scope of this course. Knowledge of ASP.NET is a prerequisite to this course and it is assumed that the student is a capable ASP.NET developer.

ASP.NET Example

The ASP.NET example in this module will utilize the same example used in the previous module but will upgrade the example to be built by using ASP.NET instead of HTML and JavaScript. The example will use C# as its server-side programming language as all examples in this course do.

Client-side vs. Server-side

The previous module illustrated loading an XML file and displaying information by using JavaScript. JavaScript is interpreted and executed by the client's browser and, as such, is referred to as client-side scripting. In the arena of ASP.NET, the bulk of code written will respond to user initiated events and reside on the server. Code compiled and executed on the server is referred to as server-side code. Client-side script and server-side code reside in two worlds separated by the abyss of the Web. A Web client and a Web server do not directly communicate but communicate through a series of requests and responses, a messaging system, sent over the Web. A server does not maintain constant state of a client and, hence, the Web is referred to as a stateless environment.

There are some Web application behaviors that benefit from being executed through code residing on the server whilst some behaviors must be executed through client-side script. An example of a behavior that must be executed through a client-side script is the display or evaluation of date and time values due to the fact that a Web client may reside anyplace in the world, in a different time zone, and even a different day than an associated Web server.

Working with XML

The ASP.NET version of the example used in the previous module will differ from that used in the previous module in that it will work with the XML through server-side C# code residing in the code behind file. The C# code that works with the XML is shown in the code snippet below.

Code Sample: ASPNETPrimer/Demos/MyFantasyPicks/Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My First ASP.NET Page</title>
    <link href="External.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        div.myDiv
        {
            font-weight: bold;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="myDiv" style="font-family: Sans-Serif;">

        <script src="Greeting.js" type="text/javascript"></script>

    </div>
    <hr />
    <asp:Button ID="btnDisplayPicks" runat="server" Text="Display My Fantasy Picks" OnClick="btnDisplayPicks_Click" />
    <div>
        <asp:Label id="lblMyPicks" runat="server" />
    </div>
    <hr />
    <div style="text-align: center;">

        <script type="text/ecmascript">
<!--
  // Display the current date and time.
  document.write("copyright &copy; 2007 | the current date and time is " + new Date());
//-->
        </script>

    </div>
    </form>
</body>
</html>
Code Explanation

The code above performs the same exact steps as the JavaScript in the previous module. However, the code now resides in the ASP.NET code behind and utilizes the .NET Framework System.Xml namespace classes instead of the ActiveX XML DOM control. In the code above, the button click event handler, btnDisplayPicks_Click, can be created in a variety of ways. First, you can manually create it in the code behind file by typing the code. You can also simply double click the button control while viewing it in the Visual Studio 2008 Design view. Finally, you can select the button control and, from the Properties sheet, display the list of events for the selected button and double click the Click event entry.

The resulting page is also identical to that in the previous module as shown in the figure below.

Lab: Online Training Tool Version 2.0

In this exercise, you will create a simulated online training utility by using:

  • Visual Studio 2008
  • XHTML
  • CSS
  • XML
  • JavaScript
  • ASP.NET

We'll create the simulated training tool to produce the same result as that produced in the previous module. However, we'll utilize ASP.NET designer features to simplify the creation process. The training utility will consist of a single HTML page that displays a single course with multiple topics stored in an XML file.

ASP.NET Primer Conclusion

In this lesson of the Silverlight tutorial, you

  • Reviewed the .NET Framework
  • Created simple programs using two .NET Framework programming languages
  • Upgraded an online training tool by using ASP.NET
To continue to learn Silverlight go to the top of this page and click on the next lesson in this Silverlight Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.