ASP.NET and PerlASPX
As part of the .NET environment Microsoft offers ASP.NET technology for developing Web Applications. ASP.NET represents a new fresh design compared to classic ASP. The programming model has significantly changed. It is possible to fully separate the presentation code from data processing code. ASP.NET comes with many built-in server controls that you can access programmatically on the server side.
Standard distribution of ASP.NET supports programming in three .NET languages:
|The PerlASPX product by ActiveState adds the PERL language to the ASP.NET family.|
The PerlASPX product by ActiveState adds the PERL language to the ASP.NET family.
Prior to installing PerlASPX, please, make sure you meet the following requirements:
- .NET redistributables
- PDK 4.0 or better
- IIS Web server
For choosing the correct version of PerlASPX, please consult ActiveState (http://www.ActiveState.com
ASP.NET Web Forms
Web Forms are stored on the Web server in the files with .aspx extensions. There you'll place HTML tags, server-side code blocks, ASP.NET server controls, etc.
Let's look at a simple Web Form that outputs the current time with the help of the localtime PERL function (Listing 5
). Every time you reload the page in your browser, the time is updated.
Line 2 is the Page directive where you may specify different attributes of the page. In this example we've set the language that will be used for the page codePERL. With the help of the <% ... %> tags we've injected server-side code to display current time in the HTML content (lines 11-15). As you can see, ASP.NET pages are completely compatible with standard HTML.
Now here's a more accurate way to design the WebTime page. We'll add a Label server control for displaying time and override the OnLoad method that handles the Load event of the Page class (see Listing 6
). We'll enclose the server code by the <SCRIPT RUNAT="server"> . . . </SCRIPT> tags.
Inside the =for interface block we've declared the OnLoad method (lines 6-8) that our .aspx page inherits from the Page class and overrides it. We've provided the implementation for the OnLoad in lines 9-15. Note that the POD block must start from the first column of the line. Hence, we cannot indent =for interface and =cut. Line 22 of the code adds Label server control lblTime to the page. We'll access this control's Text property in the code of OnLoad and set it to the current time.
Managing Shippers Sample
To demonstrate various ASP.NET techniques combined with the PERL language, we've created a shippers management ASP.NET Web Form. We wrote it in the code-behind style, i.e., separating the presentation from the code. This Web application illustrates the following ASP.NET features:
- Code-behind style
- Using Web server controls
- Data access and Data binding
- Handling server control events
shows the Web Form in Internet Explorer (we added the last two shippers using the AddShipper program that we presented in a previous part of this article).
Figure 1: The shippers.aspx Web form in Internet Explorer.
Figure 2: The definition page for the Web service. SOAP information is also provided.
Let's see the .aspx page code that is responsible for presentation layer (Listing 7
The Page directive (lines 2-3) specifies the name of the code behind file (SRC attribute) and the name of the class from which our page inherits (Inherits attribute). We've enclosed most of the content with the <form RUNAT="server"> ... </form> tags (lines 11 and 65). Since many server controls such as Button may appear only inside these tags. Lines 12-59 define a data-bound Repeater control and templates for it. The data binding occurs in the code-behind file. In the Web Form we extract the shippers' data calling the DataBinder.Eval method and display it inside the table using a special data binding syntax (<%# ... %> tags). For example, lines 38-39 display the company name of the shipper.
For each row of shippers we'll place a Button server control to remove the shipper from the database. Instead of the popular Click event we handle the Command event for these buttons. We bind the CommandArgument property of the Button control to the ShipperID (lines 48-50). This way we'll know which button caused the Command event and what row to delete from the database. We assigned the RemoveShipper method to handle the event (line 52).
Line 64 adds another Button server control. We don't handle any event on it. However, this control will cause post-back to the Web Form and this way we'll refresh the data displayed.