Wednesday, December 28, 2011

ActiveX controls at asp.net


As web application developers, we want to give our users a highly functional application. We want to provide our users with functionality like printing ,scanning images etc., but it is difficult due to the security and the disconnected architecture of the Internet .

1. create an activeX control
   a. create interface
     public interface ITest
    {
        string Name { get; }
        string SayHello(string str);
    }
    b.create class
     [Guid("a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4")]
    [ComVisible(true)]
    [ProgId("Test.Test")]
    public class TestClass:ITest
    {
         public string Name
        {
            get  { return "My Test Name"; }
        }
        public string SayHello(string str)
        {
            return "Hello " + str;
        }

    }
   c.register the activeX control:
      at the project properties window => build => check => "Register for COM interop"
2.Host control at wweb application
    a. add object tag to the html page
      <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />
    b. access the object and the functions/properties from javascript
      var myAx = new ActiveXObject("Test.Test");    => the name of ProgId
            alert(myAx.Name);  

   an example for the html:
     <script type="text/javascript">
        function getProperty() {
            var myAx = new ActiveXObject("Test.Test");
            alert(myAx.Name);
        }
        function getName() {
            var myAx = new ActiveXObject("Test.Test");
            var text = document.getElementById("myName").value;
            alert(myAx.GetMe(text));
        }
    </script>
    <table>
        <tr>
            <td>
                <input type="text" id="myName" />
            </td>
            <td>
                <input type="button" value="Property" onclick="getProperty()" />
            </td>
            <td>
                <input type="button" value="Function" onclick="getName()" />
            </td>
        </tr>
    </table>
    <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />

       

No comments: