开发者

How to use ActiveX with ASP.NET

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 m开发者_C百科essage while creating activeX object using javascript.

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 Code:

<%@ 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>


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.

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.

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.


You probably need to register the DLL.

See this for a complete tutorial on how to go about this.

regasm AClass.dll /tlb /codebase

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜