ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Migrating to ASP.NET
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @

Make Your Scripts Filename Independent
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Using Components from ASP
Back Contents Next

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:




      <TITLE>Wrox Blox Building Supplies</TITLE>




   <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>



   <P>Click here to calculate the number of bricks you will need: 

      <INPUT TYPE="submit" VALUE="Calculate">







The code for this example and the component it uses is available from our web site at 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; 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 Calculator has one property, BrickType, which specifies the type of brick. It also has one method, HowManyBricks(),which requires the WallHeight and WallLength as parameters in order to perform its calculation. Given the type of brick and the size of the wall, it will work out the number of bricks we need to build the wall.

Using the Brick Calculator Component

When the user clicks on the Calculate button of the HTML form, the values are sent to our WroxBloxResult.asp page. This is the page that uses the Brick Calculator component and returns the result of its HowManyBricks() method to the browser. The page starts by creating an instance of the component so that we can use it from the page:



      Dim objBrickCalc

      Set objBrickCalc = Server.CreateObject("BrickCalc.wsc")



Here, we create an object variable in the first line, then set this variable to an instance of the component we want to create. In this case, the ProgID is BrickCalc.wsc. Recall that the instance of the component created is called an object.


People often use the terms component and object interchangeably, but in strict terms an object is an instance of a component.


Now we collect the wall dimensions from the form to submit to the object's HowManyBricks() method. We set the BrickType property of the object to the value of the select box, and write the dimensions of the wall back to the browser:



      WallLength = Request.Form("WallLength")

      WallHeight = Request.Form("WallHeight")


      objBrickCalc.BrickType = Request.Form("BrickType")


      Response.Write "<FONT FACE=arial SIZE=6>" & _

                     "Wrox Blox Building Supplies BrickCalc Result</FONT><BR><BR>"


      Response.Write "Calculation for a wall " & WallHeight & _

                     " foot high and " & WallLength & " foot long."



Having created an instance of the component in the ASP page, we can treat the object just as we would any other object in ASP. So we can reference the methods our component implements using dot notation:


   objectname.methodname(parameter1, parameter2, ..., parameterN)


You should be familiar with this notation already, because it's exactly the same as the one we use when using the ASP intrinsic objects, and all other COM components in ASP. For example, when we want to write something back to the browser in ASP, we are using the Write() method of the Response object.


In our case, however, we have the objBrickCalc variable holding the reference to our component, so we can access the methods of the component using:


   objBrickCalc.methodname(parameter1, parameter2, ..., parametern)


On this occasion, the component only has one method, so we'll be using:


   objBrickCalc.HowManyBricks(WallHeight, WallLength)


We want to save the return value of the method in a variable strResult so that we can use it in the ASP page. To do this, we just make sure that the variable we want to use in the ASP page is set to the above line, simply by using:



      strResult = objBrickCalc.HowManyBricks(WallHeight, WallLength)



Finally, we write the value of the variable strResult, which holds the return value of the component's HowManyBricks() method, to the browser:



      Response.Write "<BR><BR> You will need " & strResult & " bricks."



And that's it. Here is the result:



So, we have created an instance of our Brick Calculator component, and used it from script in our page. Obviously it's a very simple example, and it's a task that could have been achieved using script within the form. But it does show you how to use components from your ASP script, and how we talk to objects via their methods and properties.


Having seen how to use components from our ASP pages, let's take a look at some of the common types of component, and how they might fit into our applications.


©1999 Wrox Press Limited, US and UK.
Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers