我創建了一個ActiveX組件,但無法訪問ASP.NET中的ActiveX控件。它使用JavaScript創建ActiveX對象時會出現「Microsoft JScript運行時錯誤:自動化服務器無法創建對象」錯誤消息。如何在ASP.NET中使用ActiveX控件
ActiveX組件代碼:
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FirstActiveX
{
[Guid("465F2D2E-C638-413e-A353-01E09DC4C7ED")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IMyActiveX
{
[DispId(1)]
string FirstName{ get; set;}
[DispId(2)]
string LastName { get; set; }
[DispId(3)]
string Address { get; set; }
[DispId(4)]
void Show();
}
[Guid("8975D137-9D96-492c-87AE-37D653BADE16")]
[ProgId("FirstActiveX.MyActiveX")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IMyActiveX))]
[ComVisible(true)]
public class MyActiveX : IMyActiveX
{
#region IMyActiveX Members
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public void Show()
{
MessageBox.Show(string.Format("Mr. {0} {1}, Address : {2}", FirstName, LastName, Address));
}
#endregion
}
}
HTML代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebActiveXTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<script language="javascript" type="text/javascript">
function UseActiveX() {
var x = new ActiveXObject("FirstActiveX.MyActiveX");
x.FirstName = "Nirajan";
x.LastName = "Singh";
x.Address = "Kamothe, Navi Mumbai";
alert(x.FirstName);
return false;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnShow" runat="server" Text="Show" OnClientClick="return UseActiveX();" />
</div>
</form>
</body>
</html>