Advertisment

Introducing ASP.NET

author-image
PCQ Bureau
New Update

Although started as a set of hyperlinked yet static pages, the Web has grown to become a provider of information and a place for exchanging data and performing transactions with databases, files and other things. Much of this is impossible with the standard computer language of the Web–HTML. HTML cannot talk to databases or read files; it only displays content on the client. This is why Web developers use server-side programming languages or technologies like CGI, ASP, Perl and PHP.

Advertisment

ASP (Active Server Pages) has been one of the most popular technologies that allow developers to create complete Web ‘applications’, rather then static Web ‘sites’ quickly and effectively. ASP normally runs only on the Windows platform with IIS, but using add-on programs like ChiliSoft’s ChiliASP and HalcyonSoft’s InstantASP, it can run on non-MS OSs and Web servers too.

ASP.NET is the new offering for Web developers from the Microsoft stables. It is not simply the next-generation of ASP; in fact, it is a completely re-engineered and enhanced technology that offers much, much more than traditional ASP and can increase productivity significantly. With this issue we are starting a series of articles on what these offerings are and how you can use them. For those of you who program in ASP or something similar (say PHP), you’ll also find comparative statements between ASP.NET and the traditional methods in this series.

So what’s really new in ASP.NET? We’ll take a look at many new features as we proceed in the series, but here are a few important ones to whet your appetite. For one, ASP.NET has been re-written from ground up to be an integral part of the new .NET initiative from Microsoft. It is a core part of .NET and conforms to all its specifications. ASP.NET can be programmed in many different languages. Microsoft itself offers four: Visual Basic (instead of VBScript), C#, C++ and Jscript, plus an upcoming J#. Solutions from other vendors allow for Perl, Cobol, SmallTalk, etc. Other highlights of ASP.Net are as follows.

Advertisment

Increased performance. Traditional ASP (and for that matter Perl and PHP) are interpreted at runtime. That is, every time an ASP page is requested, the Web server loads and runs through the page line by line. On the other hand, ASP.NET is complied on the first hit and all subsequent page hits are rendered from a cache of the compiled pages. This brings about a significant performance boost. The .NET compiler also takes into account the amount of memory, the specific CPU and other hardware resources on the server for optimizing the compile output.

Easier deployment. All that is required to get an ASP.NET application running on a Web server is simply copying the files (using standard file copy or FTP) to the correct directory. Even external components in DLLs etc, are automatically registered and used as required.

Server controls. This allows developers to quickly add common functionality by using controls that are created on the server end. For example, common tasks such as displaying a table of data, an interactive calendar or user form validation can be quickly achieved in very few lines of code. Another advantage is that these controls intelligently render on the client browser after detecting the type and version. Higher browsers will get additional benefits, such as

DHTML.

Advertisment

Web services. A very important aspect of ASP.NET, Web services allow data interchange over the World Wide Web using standard technologies like HTTP and XML. We will look into Web services in detail in a future article in this series.

These were, of course, only a few of the new enhancements available in ASP.NET. We will take a detailed look at them over the coming months. 

Writing your first ASP.NET page



Let us get headlong into code. Without breaking away from tradition, let us create the standard “Hello, World” program in ASP.NET. Open up Notepad or your favorite text-editor and type in the following. 

Advertisment

<%@ Page Language="VB"%>









PCQ #1.1: Hello, World











Hello, World









Save this file in your Webroot as Test1.aspx. Note the difference in the extension form normal ASP. The ASPX extension signifies that the page is an ASP.NET page. It also allows traditional ASP and ASP.Net pages to co-exist in the same application without issues.

To view the output of this program, you must first have the .NET framework installed on the machine. If it is, simply point your browser to the ASPX file you just saved and see the output. It’s, of course, nothing great–just a page that displays “Hello, World”. But there are a few important things to understand in the code above. Let’s look at them one-by-one.

Advertisment

<%@ Page Language="VB"%>

This line of code specifies page-level attributes or parameters that affect the way the page is parsed or compiled by the .NET framework. In this example, we’re setting the language being used in the page to “VB” (Visual Basic).

Hello, World

Advertisment

This looks just like the normal html tag. However, it has a new attribute, runat= “server”. This specifies that the control used here is to be executed on the server and only return HTML code is to be output. Of course, in this case, this will be the same as the control itself. This particular one is an example of an HTML Server Control.

Let’s create one more page, one with a bit more code and some action to it. Create a new file and enter the following code into it. Save it as test2.aspx and run it. Take a look at the screenshots to see what it does. 

<%@ Page Language="VB"%>









PCQ #1.2: Hello, <name></name>











Advertisment




Enter your name here please: 





















Let’s dissect this piece of code too. I’ll only introduce the new aspects here.



This specifies that the page contains an ASP.NET WebForm. This is the basic unit through which interactivity with the server is done. 












This is what is known as a “Web Control”. Note the “ASP:” prefix to the tag textbox. It specifies the “namespace” or the collection of classes (to put it very simply) from which this control is obtained. The ASP prefix maps to the

System.Web.UI.WebControls namespace and is automatically included in every ASP.Net page. However, you can create your own Web Controls and use them, but in this case, you’ll need to include the appropriate namespace yourself. We’ll see how this is done in a later part of the series. The “textbox” simply renders an input box on the client browser.


onclick="GetName"/>

Similar to the above, this renders a submit button on the client with the text as “Send”. It also handles the onClick event and runs a subroutine called GetName. However, unlike traditional HTML/ASP programming, this onclick does not get fired at the client end when the button is pressed; instead it gets sent back to the server and is handled there.

Renders an empty span tag at the client.

This is the real ASP.Net code. The script here runs at the server end. In this page, the only subroutine is the one that is executed when the button on the page is pressed. All it does is that it gets the name entered in the textbox, adds a message to it and displays it in the empty label area. The parameters to the function are required by the signature (or template) of the onClick event handler for the button.

So, you have written your first couple of ASP.Net pages. Keep in mind that this was only an introductory article that breezed through some basic concepts. In future parts of this series, we’ll take a detailed look at WebForms, Web Controls, writing your own controls, event handling, database connectivity and Web Services, to name a few. 

Vinod Unny is a Technology Consultant at Enterprise InfoTech

Advertisment