Using ActiveX Controls in Visual InterDev Web Applications

 

Microsoft Corporation

August 1998

Introduction

ActiveX® controls can provide reusable functionality in your Microsoft® Visual InterDev® Web applications. You can create custom ActiveX controls in Microsoft Visual Basic®, then use the controls in your Web pages. ActiveX controls can run on the client or on the Web server. When you run ActiveX controls on the client, you greatly increase the functionality of the Web page. When you run ActiveX controls on the Web server, you provide multiple Web browsers access to the control functionality.

This paper assumes that you already know how to create ActiveX controls using Visual Basic, so don't look for that information here. Instead, we'll use a simple ActiveX control, DateFunction, to illustrate how to successfully deploy ActiveX controls to the Web server and add the custom control to Web pages. We'll also discuss some of the pros and cons of using ActiveX controls on Web pages.

This paper is broken down into the following sections:

ActiveX Controls and Web Browsers

The DateFunction ActiveX Control Example

Preparing the Control for Download

Preparing the Control for Use on the Server

Adding the DateFunction Control to a Web Page

ActiveX Controls and Web Browsers

ActiveX controls can be accessed in two ways from a Web page. You can have users download the control and run it from their Web browser or you can include the control in a Web page as a server component and run the control from the Web server.

If you choose to run the ActiveX control from the user's browser, you need to take into consideration some issues with downloading ActiveX controls. While ActiveX controls are a great way to create reusable components for use in Web applications, most Internet browsers display some kind of warning to the user before they are allowed to use the control.

For example:

  • The default security settings for the browser may not allow ActiveX controls to be downloaded.
  • If the user lowers the security settings, the browser may still display a warning against "unsafe content" on the page.
  • Additional cautionary dialog boxes may display even when a user attempts to download digitally signed ActiveX controls.

With all of these warnings, users may be understandably hesitant about downloading and using ActiveX controls. You can help alleviate their concern by digitally signing your ActiveX controls. When you digitally sign your controls, you are telling the user who created the control and providing the user a means of contacting you should they discover a problem with the control. For more information about digital signing, see "Digital Signing for ActiveX Components" in the Visual Basic online documentation.

The DateFunction ActiveX Control Example

The Visual Basic online documentation does an excellent job of explaining all of the steps necessary to create an ActiveX control. The purpose of this section is to give you a hypothetical example of an ActiveX control created for use on the Internet. For more detailed information, see "Creating an ActiveX Control" and "Building ActiveX Controls" in the Visual Basic online documentation.

Design the Control

Suppose you need to create an ActiveX control that converts numeric dates into a long string format, such as 1/1/98 to Thursday, January 1, 1998. You intend to use this control on a Web page that allows visitors to determine the day of the week a particular date in history occurred. The Web page consists of a text box that allows visitors to enter a date, a Convert button to call the DateFunction control, a label that will display the results of the control, and a Clear button to reset the text box and label caption.

Develop the Control

You decide to keep the control simple and choose not to include any property pages. By design, the user will never see the control on the page. To developers using the control in Visual InterDev, the control will appear as a two-dimensional square in the HTML editor with the words "Date Function Control" to identify it.

Figure 1. The DateFunction ActiveX control in Visual InterDev

The DateFunction control uses the following function to convert the numeric date:

Public Function DateConvert(strDate As String) As String
length = Len(strDate)
If length <8 Then
   MsgBox ("Enter a number using the following format: mm/dd/yyyy.")
Else
   DateConvert = Format(number, "dddd, mmmm d, yyyy")
End If
End Function

The function checks the length of the numbers passed to it and displays a message box if the numbers do not meet the required length. If the numbers do meet the required length, the function converts the date to a long string format: day of the week, month, day, and full year.

Debug the Control

To test the DateFunction control, you can create a Visual Basic standard .exe project. The user interface on the form in the standard .exe project mimics the UI you intend to use on the Web page in Visual InterDev.

Figure 2. DateFunction control UI form

Once you have verified that the DateFunction control works to your satisfaction, you have two choices depending on where you intend to run the ActiveX control. If you intend to run the control from the user's browser, you must create a .cab file for the control and deploy the file to the Web server. If you intend to run the control from the Web server, you can add the ActiveX control's .ocx file to the Visual InterDev Web application for use in Web pages.

Note   You should always digitally sign each ActiveX control that will be downloaded and used on a Web page. For more information, see "Digital Signing for ActiveX Components" in the Visual Basic online documentation.

Preparing the Control for Download

The Package and Deployment wizard is the fastest way to prepare a custom ActiveX control for download to the user's computer. This wizard is available with the Enterprise Edition of Visual Basic and the Enterprise Edition of Visual Studio®. The Package and Deployment wizard allows you to create the .cab file and other associated files necessary so that the control can be downloaded from the Web server to a user's computer. For more information, see "Deploying ActiveX Components" and "Downloading ActiveX Components" in the Visual Basic online documentation.

Figure 3. Package and Deployment wizard

Note   The Package and Deployment wizard is available from the Add-Ins menu in Visual Basic. You must use the Add-In Manager to add the wizard to the Add-Ins menu. For more information, see "The Package and Deployment Wizard" in the Visual Basic online documentation.

Packaging the DateFunction Control

The Package and Deployment wizard allows you to quickly create the .cab file and support files that the browser uses to download ActiveX controls. The wizard steps you through the process of choosing a package type, the destination of the package, the files to be included in the package, and the safety settings for the package.

To create a .cab file for the DateFunction control:

  1. Launch the Package and Deployment wizard in Visual Basic and choose Package.

  2. In the Packaging Script pane, enter DateFunction for the name of the package script.

  3. In the Package Type pane, choose Internet Package from the Package type list.

    Figure 4. Package and Deployment wizard package type

  4. In the Package Folder pane, create a new folder, DateFunction, to place the .cab file and support files in at the completion of the wizard.

  5. In the Included Files pane, include all of the files listed in the grid in your package.

  6. In the File Source pane, accept the default options and click Next.

  7. In the Safety Settings pane, specify that DateFunction is safe for scripting.

    Caution   Safety settings, such as safe for scripting and safe for initializing, tell the user that the ActiveX control will not perform harmful actions to a user's computer. You must verify that your custom ActiveX control does not cause damage to a user's computer or data as a result of scripting or initialization before you specify that your control is scripting-safe or initializing-safe. For more information, see "Safety Settings for ActiveX Components" in the Visual Basic online documentation.

  8. In the Finished! pane, enter DateFunction as the name to save this session's settings under.

    Note   Digital signing and licensing of the ActiveX control must be done outside the packaging process. For more information, see "Internet Packages" in the Visual Basic online documentation.

When you choose Finish, the wizard creates a .cab file and the support files needed for a user to download the DateFunction ActiveX control and displays a report. You can then deploy the DateFunction control to the Web server.

Deploying the .cab File and Support Files to a Web Server

The Package and Deployment wizard allows you to easily add the installation files for a custom ActiveX control to the Web server. This wizard steps you through choosing which files to deploy and selecting the destination Web server for those files.

Note    HTTP posting requires that you have Posting Acceptor 2.0 or later installed on the Web server. Posting Acceptor can be installed from the Visual Studio Server setup. You must also have the appropriate permissions to write content to the Web server.

To deploy the .cab file and support files:

  1. Launch the Package and Deployment wizard in Visual Basic and choose Deploy.

  2. In the Deployment Script pane, enter DateFunction as the name for the deployment script.

  3. In the Package to Deploy pane, select DateFunction as the package to be deployed.

  4. In the Deployment Method pane, choose Web Publishing.

    Figure 5. Package and Deployment wizard deployment method

  5. In the Items to Deploy pane, choose all of the items in the list to be deployed to the Web server.

  6. In the Additional Items to Deploy pane, do not select any additional files to deploy.

  7. In the Web Publishing Site pane, enter the root URL for the Web server you want to deploy the files to. For example, http://MyWebServer.

    Note   If the wizard has not deployed to the URL before, you will be asked to save this information.

  8. In the Finished! pane, enter DateFunction as the name to save the wizard settings under.

  9. When you choose Finish, the wizard deploys the DateFunction package to the Web server and displays a report. You can now add the DateFunction ActiveX control to a Web page and test the download capabilities.

Preparing the Control for Use on the Server

If you intend to run the ActiveX control from the Web server, you do not need to create a .cab file for the control. Instead, you can simply add the control to the Web application.

To add a control to a Web application:

  1. In a Visual InterDev Web application, right-click the root Web name.
  2. From the shortcut menu, choose Add, then Add Item.
  3. In the Add Item dialog box, choose the Existing tab.
  4. In the Files of Type drop-down list, choose Executable Files.
  5. Browse to find the Visual Basic project that contains the ActiveX control.
  6. Select the .ocx file for the ActiveX control and choose Open.

Once you have added the control to the Web application, you can now add the control to the Toolbox and use the control on your Web pages.

Adding the DateFunction Control to a Web Page

You can place the ActiveX control on a page for the user to download or you can run the ActiveX control from the Web server. Depending on which option you choose, the method you use to call the control from the Web page differs.

To use the DateFunction control in Visual InterDev, you need to make Visual InterDev aware that the control exists. You do this by adding the control to the Toolbox in Visual InterDev.

To add the DateFunction ActiveX control to the Toolbox:

  1. From the Tools menu, choose Customize Toolbox.

  2. Choose the ActiveX Controls tab.

  3. Scroll through the list of controls until you find the DateFunction control.

  4. Check the control and choose OK.

    Note   If the control does not appear in the Customize Toolbox dialog box, the control has not been registered on your machine. You can quickly register the control on your machine by adding the .ocx file to your Web project and choosing the appropriate options from the Component Installation tab of the File Properties dialog box.

Scripting for Use on the Client

Once you have added the control to the Toolbox, you can use the control on a page. For the DateFunction control, we created the following page called ConvertDate.htm.

Figure 6. ConvertDate.htm

The page allows you to enter a numeric date and then display the converted date in the caption of a label. After adding the DateFunction control to the page, we added the following JavaScript to call the control from the Convert button.

function cmdConvert_onclick() {
var newdate
newdate=DateFunction1.DateConvert(txtNumericDate.value)
lblWeekDay.setCaption(newdate)
}

We then added the following script for the Clear button.

function cmdClear_onclick() {
txtNumericDate.value=""
lblWeekDay.setCaption("")
}

Once the Web page was completed, we viewed the page in Quick View.

Figure 7. ConvertDate.htm in Quick View

After verifying that the DateFunction ActiveX control works on the Web page, you need to test that the user can download the ActiveX control from the Web server.

To test that the DateFunction ActiveX control downloads properly:

  1. On a machine other than your development machine, launch your Web browser.

    Note   You may need to adjust your browser security settings in order to download the ActiveX control.

  2. Point your Web browser to the appropriate URL. For example, http://MyWebServer/MyWebApplication/ConvertDate.htm.

  3. In the Security Alert dialog box, choose Yes to download the control.

  4. When the page finishes loading, test the DateFunction ActiveX control by entering a sample date in the text box and choosing Convert.

If the ActiveX control does not load properly, try one of the following:

  • Lower the security settings on your Web browser.

  • Verify that the .cab and support files are on the Web server.

  • Verify that the CODEBASE attribute in the <OBJECT> tag of the ActiveX control contains the correct URL for the .cab file. For example, the CODEBASE attribute may point to DateFunction.cab on your development computer instead of HTTP://MyWebServer/.../DateFunction.cab on the Web server.

    Tip   To check the CODEBASE attribute, in Visual InterDev right-click the DateFunction control in Source View and choose Always View as Text.

For more information, see "Testing your Internet Component Download" in the Visual Basic online documentation.

Scripting for Use on the Web Server

Once you have added the control to the Toolbox, you can use the control on a page. To run DateFunction on the Web server, we created the following page called ConvertDate.asp.

Figure 8. ConvertDate.asp

The page uses a form to collect and submit the numeric date information for the control.

<form action="ConvertDate.asp" id="FORM1" method="post" name="FORM1" LANGUAGE="javascript">
<p>What day of the week was that?
<p>Use this page to 
determine the day of the week for any numeric date. Find out what day of the 
week you were born, married, or a particular event in history took 
place.</p>
<p>Enter a date below using the following format: mm/dd/yyyy. For example, 
1/1/1825. </P>
<p>
<input id="text1" name="text1"></p>

<p><input id="submit1" name="submit1" type="submit" value="Submit"></p>
</form>

The Active Server Page allows you to enter a numeric date and then display the converted date on the page. After adding the DateFunction control to the page, we specified that the control run on the server. To do this, we chose View Controls As Text from the View menu and then added the following directive to the <OBJECT> tag for DateFunction:

RUNAT=Server

Next we added the following server script after the line "The date:" on the page.

<%
If IsEmpty(Request.Form("text1")) Then
   Msg = "Please enter a date."
ElseIf len(Request.Form("text1")) < 6 Then
   Msg = "Enter a date using the format mm/dd/yy."
Else
   Response.Write(DateFunction1.DateConvert(Request.Form("text1")))
End If
%>
<% = Msg %>

The server script checks to see if the textbox, text1, is empty or not. If the textbox is empty, a message appears instructing the user to enter a date. If the textbox contains a string, then the length of the string is checked. If the string is greater than six characters, the string is passed to the DateFunction control, where the control attempts to convert the date.

Although the DateFunction example is simplistic, it illustrates the basics of preparing ActiveX controls for use in Web applications. You can, of course, encapsulate sophisticated code in an ActiveX control and easily make it accessible to your Web applications.

Conclusion

ActiveX controls can help you create robust Web applications for use on the Internet or intranet. You can use Visual Basic to develop a custom ActiveX control and then include that control in a Visual InterDev Web application. The Package and Deployment wizard makes it easy for you to prepare your ActiveX controls for use in Web pages.

For additional information about ActiveX controls in Web pages, try the following online articles:

You can also search for related Knowledge Base articles on Microsoft Support Online (https://support.microsoft.com/support)