Today, Dwight takes us through the ASP.NET features that you can apply immediately to your projects. If you're reluctant to learn .NET because of it's complexities, then this article serves as a great introduction to all the features you'll need to get up and running on .NET programming.
Contributed by Dwight Peltzer Rating: / 15 April 26, 2004
One of our primary obligations as developers is finding new and better ways of developing Web services to meet the consumer’s constantly evolving needs. As new technologies emerge, investigating new tools and learning to use them to both optimize your time and improve your methods of building web-oriented, enterprise applications and web services is challenging and exciting.
When Microsoft.NET released the .NET Framework, many developers felt overwhelmed by the vastness and power of this innovative set of technologies that facilitates interoperability between programming languages and supports Web services. The Framework plays host to a multi-faceted and component-based collection of namespaces containing classes that support application integration. Because classic ASP is a universally accepted technology of choice for building web services, it seems logical to begin with ASP.NET in learning .NET.
A typical first concern for most programmers might be: “what benefits do I receive by migrating to ASP.NET from classic ASP? Classic ASP provides practically everything I need to build Web services.” However, like every existing technology, there are drawbacks to each. For example, classic ASP is interpreted and impacts on efficient performance. Does ASP.NET address this issue? Another question immediately comes to mind: how many familiar classic ASP features have Microsoft retained in the new release?” This question raises concerns for developers because re-orienting oneself to another mindset is a time consuming, critical issue. Fortunately, ASP.NET has resolved these issues and many others to the benefit of all developers. ASP.NET retains many classic ASP features such as Request and Response, as well as the familiar Application.Session, and Server objects. You can still use the <SCRIPT RUNAT=”Server”>, or the ubiquitous <% %> ASP script delimiter. Perhaps the best feature allows classic ASP and ASP.NET to run side-by-side. This is great news because you can continue to program in your familiar environment using everything you feel comfortable with, while learning and enjoying the new enhancements contained within ASP.NET. The learning curve is well worth the effort.
Frequently, I lecture to both academic and IT professionals who are reluctant to learn .NET because of its complexities. They are baffled about where to begin learning this vast technology. Although this question is difficult to answer because it addresses many issues, let’s begin by examining the .NET Framework, version 1.0. The Common Language Runtime (CLR) acts as a container (similar to IBM’s Websphere) providing many low-level services such as automatic memory management, garbage collection, connection pooling, and security. The Common Type Specification (CTS) defines the common data types shared by all programming languages targeting the .NET Framework, whereas the Common Language Specification (CLS) provides guidelines to third party vendors on how to adhere to its specifications in order to receive support from the Framework. It is essential to understand their role before you can become fully productive. You may want to refer to some of the many informative articles and books written on this subject.(1)
So let’s focus on ASP.NET features we can apply immediately to our projects. If you are like me, it is exciting to jump in and see some immediate results.
It is easy to write ASP.NET pages, in either Notepad.exe, or the newly released Visual Studio.NET. Notepad is free and provides an alternative to Visual Studio. Let’s use Notepad and write our first valid ASP.NET page.
<html> <head> <title> My first valid ASP.NET web page</title> </head> <body> <p>Hello, ASP.NET Web Services developer! </p> </body> </html>
Save this file with an .aspx extension in an IIS virtual directory you have previously created on your local drive. Then type “http://localhost/ASP.NETTest/ASP.NETHello.aspx.” Assuming your Web server is properly installed, you should see the following results in your Internet Explorer browser:
Hello. ASP.NET Web Services developer
This page may not be very exciting, but you now have a perfectly valid ASP.NET page.
.NET Framework’s Architectural Structure
NET is component- based and contains a large collection of abstract interfaces and classes. They provide the supporting structure for creating reusable components, designed especially for rendering elements on a web form. ASP.NET, the Internet segment, is one of five primary components included in the Framework in addition to the CLR, CTS, CLS, and ADO.NET.
One of the innovative features in ASP.NET supports running controls on the server side rather than on the client. This results in improved performance by allowing the developer to control server-side controls programmatically. Additionally, your Microsoft Intermediate Language (MSIL) code is compiled rather than interpreted as was the case with classic ASP pages. This improves performance substantially.
The framework supports Web services through namespaces hosting classes such as System.UI.Web Controls. The classes in System.Web.UI are grouped as a hierarchical tree beginning with the Control class. This is the parent class from which all other controls are inherited. They include buttons, text boxes, and drop-down lists.
Separating business logic from the presentation layer receives support from the runtime by providing the new code-behind feature. You begin by deriving from the Page class. Then place your code in an .aspx file and insert a reference to another file containing the business logic just as demonstrated below:
The following code example contains HTML, text, and several lines of VB .NET code. The Web form has two textbox controls named FirstName and LastName. The form also has two RequiredFieldValidator controls that prevent you from submitting the form to the server without entering data in the FirstName and LastName Textbox controls.
Note: the <asp: TextBox tag informs the compiler that this ASP.NET page contains a text box control for execution on the server. A typical ASP.NET page looks like this:
Notice: If the <form> element is not present on the page, both web controls and HTML controls will not be able to participate in page post backs, nor will they be able to save their state in the page’s ViewState. They will continue to function otherwise.
It is important to utilize one of the key benefits ASP.NET offers, namely preserving state between client page requests. Always add the <form> element to the Web Forms page. If a post back event occurs, any state stored in hidden fields rendered to the form is retrieved and sent back to the server for rendering the new page to the browser. The user views the newly rendered .aspx page as though it were the original page. In reality, they are two individually unique pages.
ASP.NET pages begin as code in a text file with an .aspx extension. They reside within an IIS virtual directory created somewhere on your drive. All pages are instantiation classes derived from the Page class. The text file is transformed to an ASP.NET page when the client submits a request to the server. Then, the server renders the page to the client browser. All ASP.NET functionality lies primarily with the Page class.
Two separate methods exist for inheriting from the Page class. The first method facilitates adding the @Page directive to an .aspx file. The directive automatically makes available all page properties and methods for any code written on the page. The second method supports the code-behind feature and inherits from the Page class associated with a particular page by specifying either the Src or Inherits attribute.
The best feature is that it allows ASP.NET to combine the code in the Web form’s .aspx file with the code in the code-behind class file. It then compiles both files to a single merged file called an assembly.
Examining a Page’s life cycle is the next step. By doing so, we will add to our knowledge of ASP.NET pages. Each ASP.NET page contains a server-side <form> tag. This tag instructs the page to post back to itself when a client submits the form. The Form type events include Load, Draw, Render, and Unload.
ASP.NET controls also render JavaScript to the client, enabling actions such as selecting a specified item from a drop-down list, causing a post-back to the server. The ASP.NET runtime renders a hidden field to the page permitting it to preserve state between client requests. Because ASP.NET is event-driven, client and page interaction allows the page to be re-constructed on the server. It also facilitates code execution in response to events raised by users and any changes occurring in the hidden fields. Here is the page lifecycle processed step-by-step. The figure displayed here illustrates an ASP.NET’s page lifecycle.
Figure 1The ASP.NET lifecycle
The initial event begins with an HTTP URL client request for rendering a specified page to the client’s browser.
The Load event fires next. The CLR uses reflection to examine the .aspx page and determine whether the page is called for the first time, or represents a post back through user interaction with a button or some other page control.
If the event is a first-time request, the code is converted to a class.
The class compiles to an assembly and is stored in a valid Internet Information Server virtual directory where the page’s location can be determined.
If the page is posted back, ASP.NET restores any data residing in hidden fields (ViewState) and passes the information to the server.
The control event triggering the post back fires. At this stage, all control events are initiated. The change events fire first; those events are stored in the browser and execute only when the client sends the page back to the server. After a control event fires, the page is rendered to the browser.
Before Page_Unload unloads the page from server memory, a final event performs any cleanup tasks before the unload method disposes the page.
Many other features are supported in ASP.NET by the Common Language runtime. They include Web Forms, the new programming model for ASP.Net, Server Controls, Web Services, Caching, and Configuration improvements.
In the next article we will focus on the enhanced session-state management model in ASP.NET, as well as taking a look at Page Directives. We’ll meet again in the next article. Happy Explorations in ASP.NET.