Click here to Skip to main content
15,886,518 members
Articles / Web Development / HTML

C# COM Object for Use In JavaScript / HTML, Including Event Handling

Rate me:
Please Sign up or sign in to vote.
4.92/5 (29 votes)
22 Apr 2009CPOL6 min read 182.4K   2.5K   74  
A complete example of how to create a C# COM object for use in JavaScript / HTML, including event handling
<html>
	<head>
		<title>My Com Component</title>
		<object id="myComComponent" name="myComComponent" classid="clsid:4794D615-BE51-4a1e-B1BA-453F6E9337C4"></object>
		<script language="javascript" type="text/javascript">
		    function myButton_click() {
		        var returnCode = myComComponent.MyFirstComCommand(myArgs.value);
		        var msg = "myComComponent.MyFirstComCommand returned " + returnCode;
		        appendText(msg);
		        appendText("\r\n");
		    }
		    
		    function setText(txt) {
		        myTextBox.value = txt;
		    }
		    
		    function appendText(txt) {
		        myTextBox.value = myTextBox.value + txt;
		    }
		    
		    function MyComComponent_onload() {
		        setText("");
		        myComComponent.MyFirstComCommand("Hi");
		    }
		    
		    function MyComComponent_onunload() {
		        myComComponent.Dispose();
		    }
		</script>
	</head>
	<body onload="MyComComponent_onload();" onunload="MyComComponent_onunload();">
	    <h1>My Com Component</h1>
	    <table>
	        <tr>
	            <td>
	                <button id="myButton" onclick="myButton_click();">Com Method</button>
	                <label>Args</label>
	                <textarea id="myArgs" rows="1" cols="16">Hello World!</textarea>
	            </td>
	        </tr>
	        <tr>
	            <td>
	                <textarea id="myTextBox" rows="10" cols="80"></textarea>
	            </td>
	        </tr>
	    </table>
	    
        <script for="myComComponent" event="MyFirstEvent(args)" language="javascript">
        function myComComponent::MyFirstEvent(args) {
            appendText("myComComponent raised MyFirstEvent. args: ");
            appendText(args);
            appendText("\r\n");
        }
        </script>
	</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Desire2Learn
Canada Canada
Professional software developer in St. John's, Newfoundland, Canada

http://www.jerometerry.com

Comments and Discussions