|
Beginning ASP Components, page 3 Author: Richard Anderson Contents
Using Components from ASP The majority of this book is about creating your own components for use in your ASP applications, and in this section we'll use a custom-built component in an ASP page. We'll begin by looking at how to call components from ASP. It's likely that you'll have done something like this before during your time as an ASP developer, but you may not have completely understood the detail of what your code was up to. Let's put that right with a quick example. Did You Know You Were Using COM Components? Whether you realized it or not, the Server, Request, Response, Application, and Session objects are in fact COM components. They are known as ASP's intrinsic or built-in objects-they are hosted in a COM server called asp.dll, which you'll find on your machine if you have IIS or PWS installed. There are also a number of other COM components that come with ASP, such as the Ad Rotator and the Content Rotator. The components available to you will depend upon which version of ASP you are running. In ASP pages, the ASP intrinsic objects are treated a bit differently from other COM components. The ASP intrinsic objects are there and ready-to-use in the page-the ASP environment takes care of this for us. When we want to use other components on our ASP pages, we have to create them explicitly using the Server object's CreateObject() method. We also have to pass the CLSID or the ProgID of the object to the CreateObject() method, so that it creates an object of the appropriate type for us to use in our script (we will meet ProgIDs later in this chapter). Using Our First Component Later in the chapter, we will build a component called BrickCalc.wsc. Its job is to calculate how many bricks we would need in order to build a wall. At this point, we'll talk about how that component is used in a simple user interface called wroxblox.asp. This page asks the end user to specify the size of the wall they want to build. It also allows users to specify the type of brick (breeze block or house brick), using a drop-down list box. This information is held in an HTML form:
<HTML>
<HEAD>
<title>Wrox Blox Building Supplies</TITLE>
</HEAD>
<BODY>
<P><FONT FACE="Arial" SIZE="6">Wrox Blox Building Supplies BrickCalc.</FONT></P>
<FORM ACTION="WroxBloxResult.asp" METHOD="post">
<P>Length of wall in feet: <INPUT ID="WallLength" NAME="WallLength" ></P>
<P>Height of wall in feet: <INPUT ID="WallHeight" NAME="WallHeight" ></P>
<P>Select Type of Brick:
<SELECT ID="BrickType" NAME="BrickType" STYLE="HEIGHT: 22px; WIDTH: 131px">
<OPTION SELECTED VALUE="BreezeBlock">Breeze Block</OPTION>
<OPTION VALUE="HouseBrick">House Brick</OPTION>
</SELECT>
</P>
<P>Click here to calculate the number of bricks you will need:
<INPUT TYPE="submit" VALUE="Calculate">
</P>
</FORM>
</BODY>
</HTML>
The code for this example and the component it uses is available from the Wrox Press web site at http://webdev.wrox.co.uk/books/2882. You'll also find a compiled version of the component there. Before you use this component, however, you need to install the Windows Script Component download that's available from https://www.microsoft.com/scripting; this will be discussed in more depth when we see how to create the component later in the chapter. Here is what the WroxBloxForm.asp page looks like: When the user clicks on the Calculate button, the values are sent to our WroxBloxResult.asp page. Before we take a look at that page, let's just see what the component allows us to do. The Brick Calculator Component The Brick Calculator component BrickCalc.wsc is a Windows Script Component (hence the .wsc file extension). Windows Script Components (WSC) is a Microsoft technology that allows us to create COM components using VBScript or JScript. This type of component is excellent for speedy prototyping of components, because-as we will see later in the chapter-the interface is generated by a Wizard, and the methods are implemented using script. The Brick Calcul |