Microsoft Corporation
August 1998
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 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:
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 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.
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.
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.
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.
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.
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:
Figure 4. Package and Deployment wizard package type
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.
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.
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:
Figure 5. Package and Deployment wizard deployment method
http://MyWebServer
.
Note If the wizard has not deployed to the URL before, you will be asked to save this information.
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:
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.
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:
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.
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:
Note You may need to adjust your browser security settings in order to download the ActiveX control.
http://MyWebServer/MyWebApplication/ConvertDate.htm
.If the ActiveX control does not load properly, try one of the following:
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.
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.
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 (http://support.microsoft.com/support)