Now that we have created page methods and properties by using the PageObject DTC, we can access those methods and properties from another page, either in server script or client script. To use the properties or methods of another page, you must create a reference from the current page to the page that contains the PageObject DTC and the methods and properties that we want to use. This process is similar to creating a reference to a Microsoft ActiveX component—we are telling the application where the other component/page is located. The following steps show you how to create a reference to another page containing a PageObject DTC:
Figure 4-10. The PageObject DTC must have a reference to other pages that your application will use as objects.
The Client and Server
The Create URL dialog box shown in Figure 4-11 assists you in preparing the reference to the correct PageObject DTC.
Figure 4-11. You can use the Create URL dialog box to search for the page and create the reference.
You can set other properties on the Create URL dialog box to control what happens when a property or method on the page is
Now that you have a reference to the page containing the methods and properties, you can use them in your application. For this example, the following code is contained in the file ASP Page7.asp. The OrderNumber property and the TotalInventory method are contained in the ASP Page4.asp file. You can access both the property and the method by using the following syntax:
<%
ASP_Page4.setOrderNumber = "98-001"
ASP_Page4.navigate.TotalInventory("10")
%>
I used the navigate method in this example because the method I want to use (that is, TotalInventory ) is exported as a Navigate method. Figure 4-12 shows a portion of the editor with this code and the IntelliSense display for the remote page. To use either the navigate or execute method, you should use IntelliSense to display the object and methods for you. Then you can type either navigate or execute and then select the appropriate method from the IntelliSense list. This will ensure that you have both the correct method of execution and the correct syntax.
Figure 4-12. The IntelliSense list for the ASP Page4.asp page.
The PageObject run-time scripting object that underlies the PageObject DTC has several methods and properties that are useful in applications. These properties and methods can be used for various purposes in your application.
What's going on behind the scenes when you use the Scripting Object Model? When you insert a DTC on a page or enable the SOM for a page, Visual InterDev
<%@ Language=VBScript %> <% ' VI 6.0 Scripting Object Model Enabled %> <!--#include file="_ScriptLibrary/pm.asp"--> <% if StartPageProcessing() Then Response.End() %> <FORM name=thisForm METHOD=post>
This code calls the
StartPageProcessing
routine in the pm.asp file.
StartPageProcessing
The
thisPage
page object also contains the
firstEntered
property, which can be used to determine whether a user is visiting a page with a browser or whether the page is being executed as the result of some type of posting action. This is handy when you are referencing a page repeatedly and want to determine when the
The startup code also creates an HTML form and
Figure 4-13 shows the Script Outline window and the file ASP Page6.asp with an empty thisPage_onenter event handler. I created this event handler by opening the Server Objects & Events folder in the Script Outline window, clicking the plus sign beside thisPage to open it, and then double-clicking onenter to insert an empty thisPage_onenter event in ASP Page6.asp.
Figure 4-13.
The Script Outline window is the
Now that we have the
onenter
event handler, we can use the PageObject object to determine how the user entered the page. Figure 4-14
Figure 4-14.
The debug features of Visual InterDev 6 make it easy to understand how the various
You can also use the location property to obtain the URL of the current page:
<%
Response.Write "URL: " & thisPage.location
%>
The
thisPage
object also provides the
onbeforeserverevent
event, which is fired before posting a page for server processing. You can use this event to check values and other
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function ASP_Page9_onbeforeserverevent() {
alert(document.thisForm.txtNumber.value)
if (document.thisForm.txtNumber.value == "")
thisPage.cancelEvent = true;
}
//-->
</SCRIPT>
This JScript code runs in the client browser and executes before any server events. The first line in the event code displays the value from the HTML text box txtNumber . Then the if statement checks the value—if it is zero, it cancels the server processing by setting the cancelEvent property of thisPage . The result is the cancellation of a needless round-trip to the server.
As you have seen in this chapter, the Scripting Object Model adds a powerful set of capabilities to the Web development environment. You can perform all types of tasks, work with objects, and use object syntax with pages.
The Script Outline window is a key feature of this object environment: it shows a list of all the scriptable objects on the page along with the events that you can use to script against. The Script Outline window displays the objects in a page only when you have a page containing script open in the code editor.
The Script Outline window contains both client and server objects. You can see some objects that show up in both categories. For instance,
thisPage
will show as both a server object and a client object. Although it shows up in both client and server, the events that are available for
thisPage
are specific to one or the other. You saw this earlier when we used the
onenter
event in server script and the
onbeforeserverevent
event for client script. You should use the Script Outline window and the IntelliSense command completion feature wherever possible to cut down on the introduction of possible
You can now use command completion in script in HTML and ASP files as mentioned in the last paragraph. To use command completion, type the first part of the name for an object and press Ctrl+Space. This will drop the IntelliSense list. You can also display the IntelliSense list at any time by pressing Ctrl+J.