A PHP Developer's Guide to ASP.NET 2.0

By Gareth Palidwor

This article is an introduction to ASP.NET for PHP programmers. Although both of these platforms are tuned for implementing web applications, there are a number of useful concepts central to ASP.NET that some PHP programmers may never have seen before: code-behinds, advanced controls, event handling, and a few new forms of state. To introduce and explore these differences, we'll work through generating a simple example application in ASP.NET from the perspective of a PHP programmer.

Introduction

In ASP.NET 2.0, application development is usually done through an Integrated Development Environment (IDE): Visual Studio 2005, or the more specialized (and free) ASP.NET-specific Visual Web Developer (VWD). Although it is possible to write ASP.NET code using a simple text editor, we will use an IDE in this article, as the automated code generation features will allow us to get an application up and running more quickly. For our examples, we'll be using the freely available VWD, which supports ASP.NET 2.0 and includes an integrated web server for application testing. The code examples will be written in C#, which has a syntax closer to that of PHP than the other commonly used ASP.NET language, Visual Basic (VB). Even if you're not familiar with C#, the examples will be simple enough to follow along easily. If you're planning on working in ASP.NET, it makes sense to learn either VB or C#. You'll find most tutorials and examples for the platform are written in these languages.

A Simple Example

To generate a web application, first create a new ASP.NET website in Visual Web Developer (go to File -> New Website, and select ASP.NET Website). At the bottom of this dialog box is a drop-down menu labeled "language:" be sure to change this to C# from the default Visual Basic setting before you hit OK. In .NET terminology, you have just created a solution: a set of resources that will be deployed together on a server. In creating this web project, VWD has automatically generated a minimal set of files necessary for compilation and deployment (see Figure 1). The generation of these files is a convenience of the IDE: you could do it manually with a text editor and a little bit of knowledge. Let's take a look at the files.

Figure 1
Figure 1. Solution Explorer

App_Data is an empty folder generally used for resource files; for example, a local database file, if your application needs one.

Default.aspx is a template web page generated by the application. (see Listing 1). This, coupled with Default.aspx.cs, the code-behind, corresponds to a .php page in PHP. It consists of mostly vanilla HTML, but there are a few ASP.NET-specific wrinkles. The first line is code, as indicated by the <% delimiter, which is the equivalent of the <?php delimiter. The @ symbol indicates that this is a set of page-specific directives to the compiler. These directives are, respectively: that the code on this page will be parsed as C# by default, that the page events won't be automatically wired up (this is outside of the scope of this article), that the file containing code or "code-behind" associated with this page is Default.aspx.cs, and that the _Default class will be inherited from that code-behind. In general, you won't see a huge amount of code on .aspx pages: it tends to be kept in the code-behind.

Listing 1. Template 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>