在很多情况下,客户往往需要在客户端实现一些复杂的功能,有些可以通过AJAX来实现,有些则是现有客户端脚本不能完成的,此时则必须通过ActiveX控件来实现。
这里给出一个用.net编写简单的ActiveX并在网页中应用的例子:
///////////////////////////////////////////////////////////////////////////////////
// AxComp.cs
// Creates a simple ActiveX component.
//
// 1. Compile into a class library, or use:
// csc /t:library AxComp.cs
// 2. Register and generate the Typelib on the client's machine with:
// regasm AxComp.dll /tlb:AxCompNet.dll /codebase
///////////////////////////////////////////////////////////////////////////////////
using System;
using System.Runtime.InteropServices;
namespace AxComponent
{
public interface IAxTest
{
string CallIt();
}
[ClassInterface(ClassInterfaceType.AutoDual)]
public class AxComp : IAxTest
{
public string CallIt()
{
return "This is a test.";
}
}
}
// AxComp.cs
// Creates a simple ActiveX component.
//
// 1. Compile into a class library, or use:
// csc /t:library AxComp.cs
// 2. Register and generate the Typelib on the client's machine with:
// regasm AxComp.dll /tlb:AxCompNet.dll /codebase
///////////////////////////////////////////////////////////////////////////////////
using System;
using System.Runtime.InteropServices;
namespace AxComponent
{
public interface IAxTest
{
string CallIt();
}
[ClassInterface(ClassInterfaceType.AutoDual)]
public class AxComp : IAxTest
{
public string CallIt()
{
return "This is a test.";
}
}
}


///////////////////////////////////////////////////////////////////////////////////
// Tester.htm
// Test the ActiveX component.
///////////////////////////////////////////////////////////////////////////////////
<html>
<head>
<script language=JavaScript>
var g_axComponent = new ActiveXObject('AxComponent.AxComp');
alert(g_axComponent.CallIt());
</script>
</head>
<body>
<h1>Tester.htm</h1>
</body>
</html>