如何在ASP.NET中使用ActiveX [英] How to use ActiveX with ASP.NET

查看:101
本文介绍了如何在ASP.NET中使用ActiveX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ActiveX组件,但是无法在ASP.NET中访问该ActiveX组件。使用JavaScript创建ActiveX对象时,它会显示 Microsoft JScript运行时错误:自动化服务器无法创建对象错误消息。

I have created an ActiveX component, but not able to access that ActiveX compoment in ASP.NET. It gives "Microsoft JScript runtime error: Automation server can't create object" error message while creating activeX object using javascript.

ActiveX组件代码:

ActiveX Component Code:

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)加载项,并且将权限设置为允许脚本。您收到的错误是因为无法在IE中访问ActiveX控件。

If the ActiveX control is accessed with JavaScript, then the ActiveX control must be installed as a browser (IE only) add-on with permissions set to allow scripting. The error you are receiving is because the ActiveX control is not accessible in IE.

您可以在服务器(在ASP.NET中)上使用ActiveX控件,但是可以异常。 ActiveX控件主要用于浏览器,但是由于ActiveX控件也是COM DLL,因此有可能。

You can use ActiveX controls on the server (in ASP.NET), but it would be unusual. ActiveX controls are primarily for the browser, but since an ActiveX control is also a COM DLL, it is possible.

我建议不要开发自己的ActiveX控件,IE安全性变得越来越紧密,除非它是供内部使用(即在防火墙后面),否则大多数人(访问您的网页的人)都会拒绝将其安装在他们的计算机上。

I recommend against developing your own ActiveX control, IE security has gotten tighter, and unless it is for internal use (i.e., behind a firewall), most people (visitors to your web page) will resist installing it on their computer.

这篇关于如何在ASP.NET中使用ActiveX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆