Microsoft KB Archive/317392

= How to host ActiveX controls in a Web form =

Article ID: 317392

Article Last Modified on 4/21/2006

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5
 * Microsoft Internet Explorer (Programming) 6.0

-



This article was previously published under Q317392



For more information about how to perform this task by using Microsoft Active Server Pages, click the following article number to view the article in the Microsoft Knowledge Base:

159923 How to use licensed ActiveX controls in Internet Explorer

IN THIS TASK

 * SUMMARY
 * Requirements
 * Hosting an ActiveX control in ASP.NET
 * Complete code for hosting a sample ActiveX control in an ASP.NET page
 * REFERENCES



SUMMARY
This step-by-step article describes how you can host ActiveX controls in a Web Form of an ASP.NET page.

You can use the drag-and-drop feature of the Microsoft Visual Studio .NET toolbox in an ActiveX control. By using the drag-and-drop feature, you can add the OBJECT tag with the ClassID of the control to the underlying Hypertext Markup Language (HTML) code of the Web Form.

NOTE: ActiveX controls that you host in a Web Form are still downloaded, installed, and run on the client side. These controls cannot run on the server side, as do Web controls.

back to the top

Requirements
The following list outlines the hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * ASP.NET
 * ActiveX controls

back to the top

Hosting an ActiveX control in ASP.NET
If you add a reference to the ActiveX control by using the Add Reference option, you can create an object of the control type in the code by using the Create Object method. That object runs on the server side, and you can invoke methods of the object. For example, if the return type of the method is binary data, you can write the binary data to the client. Use the BinaryWrite method of the Response objects.

NOTE: The BinaryWrite method calls cannot be made from the client-side script.

If you host an ActiveX control, and you want to access the properties and methods of the ActiveX control on the client side, you must manually modify the HTML code. The HTML code resides behind the ASPX page.

By default, you are working in the Design mode of the ASPX page in the Visual Studio .NET integrated development environment (IDE). To see the HTML version of the page, click the HTML button in the lower left of the Design window.

When you place an ActiveX control on the Web Form, an OBJECT tag is created in the HTML code. A client-side ActiveX control is created when the page is loaded. Because the preceding example is a pure client-side control, the server-side code cannot access this control. Server-side code can access only server controls, which are the controls that are listed on the Web Form tab in the toolbox.

To determine whether a control is a server-side control or a client-side control, see the upper-left corner of the control. By default, a small green arrow in the upper-left corner of the control indicates that the control is located on the server side.

back to the top

Complete code for hosting a sample ActiveX control in an ASP.NET page
The following example shows how to use the MSChart control on a Web Form in Microsoft Visual Basic .NET. This example changes the width of the chart when the page is loaded. You can view the HTML version of the ASPX page and then copy and paste the following code into the page. This action replaces the original code. To do this, follow these steps:  Create a new ASP.NET Web Application project in Visual Basic .NET. To do this, follow these steps:  On the File menu, click New, and then click Project. Under Project Types, click Visual Basic Projects. Under Templates, click ASP.NET Web Application.  You now have an empty Web Form. To see the HTML version of the page, click the HTML button in the lower left of the Design window.  Replace the existing code with the following code: <%@ Page SmartNavigation=&quot;true&quot; Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;VBWebApp.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> WebForm1 <script language=&quot;javascript&quot;> function changeWidth(x) {                    x.width = 300 }   </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <OBJECT id=&quot;chart1&quot; style=&quot; Z-INDEX: 102; LEFT: 125px; WIDTH: 484px; POSITION: absolute; TOP: 85px; HEIGHT: 200px&quot; onReadyStateChange = &quot;changeWidth(this)&quot; classid=&quot;clsid:3A2B370C-BA0A-11D1-B137-0000F8753F5D&quot; VIEWASTEXT> </OBJECT> </HTML> </li></ol>

Alternatively, you can insert the control through the integrated development environment (IDE) by following these steps:
 * 1) Open the toolbox, and then click the Components tab.
 * 2) Right-click in any area on the tab, and then click Customize toolbox.
 * 3) On the COM Components tab, scroll down, and then click to select the Microsoft Chart Control 6.0 (SP4) (OLEDB) check box or a similar entry.
 * 4) When you finish, a new component called MSChart appears on the Components tab. Drag this control onto the Web Form.

When you finish the preceding steps, you see that the control does not have a green arrow in the upper-left corner. A green arrow means that the control resides on the server side and that the control is accessible from the server-side code.

back to the top

<div class="references_section">