我创建了一个 ActiveX 组件,但无法在 ASP.NET 中访问该 ActiveX 组件。使用 JavaScript 创建 ActiveX 对象时,它给出“Microsoft JScript 运行时错误:自动化服务器无法创建对象”错误消息。
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>
如果使用 JavaScript 访问 ActiveX 控件,则必须将 ActiveX 控件安装为浏览器(仅限 IE)附加组件,并将权限设置为允许编写脚本。您收到的错误是因为 ActiveX 控件在 IE 中无法访问。
您可以在服务器上使用 ActiveX 控件(在 ASP.NET 中),但这并不常见。 ActiveX 控件主要用于浏览器,但由于 ActiveX 控件也是 COM DLL,因此这是可能的。
我建议不要开发自己的 ActiveX 控件,IE 安全性已经变得更加严格,除非它供内部使用(即在防火墙后面),否则大多数人(您网页的访问者)将拒绝将其安装在他们的计算机上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)