470,644 Members | 1,204 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,644 developers. It's quick & easy.

How to fire Javascript events from a .NET winforms user control back to Javascript in IE

I have been working on a client project recently that is using winforms
..NET user controls within web pages in Internet Explorer, and need to
find out how to make the user control communicate back to the webpage
(typically to tell it that it has finished doing something). I started
digging around and found out that I needed to implement a COM interface
for the .NET user control, and pick up the events from that in
Javascript on the webpage. Finding out how to do it was another

After spending a couple of days digging through books, MSDN articles,
and various forums, I discovered half the answer here, and half the
answer there - but never a full solution. With this in mind (and in the
spirit of giving something back to the internet), I have laid out
everything you need to do below for a simple case. The following code
is not intended to be run - it is a "primer" to show you what goes


Jonathan Beckett
Some Background Information...
Internet Explorer does not understand "dot net" user controls, but
it does understand ActiveX controls. Therefore the user control you
build has to implement a COM interface (essentially allowing it to
impersonate an ActiveX control).

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

// required for COM interoperability features
using System.Runtime.InteropServices;

// exposes events to the outside world through COM
[assembly: ClassInterface(ClassInterfaceType.AutoDual)]

namespace my.controls {

// establish which public variables will be available through COM
public interface ImyControlCOMIncoming {
string MyData { get; set; }

// interface for the Event - the guid is a random number (you can
write code to generate it)
// the DispId a number of your own choosing - the typical format is
0x6002000n (each event
// you expose will need it's own DispId)
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface ImyControlCOMEvents {
void MyEvent();

// Control class, implementing the required interfaces
public partial class myControl : UserControl, ImyControlCOMIncoming {

// delegate and event declaration
public delegate void MyEventHandler();
public event MyEventHandler MyEvent;

// when called within the control, DoMyEvent raises the event
protected void DoMyEvent(){
// only raise if the event is registered against something
if (MyEvent != null){

// constructor for the control
public myControl {

// button click
private void MyButton_Click(Object sender, EventArgs e){
// call the event

// public property
private string _MyData;
public string MyData {
get { return _MyData; }
set { _MyData = value; }



Javascript to instatiate the UserControl, and respond to an event fired
within the control...
<title>Test Page</title>

<script FOR="uc1" EVENT="MyEvent">
alert("event fired");

<object id="uc1"
<param name="MyData" value="foo" />

Footnote - in order to get a UserControl to appear on a webpage you
will need to change the .NET framework security settings for the
network zone, and to get the events to fire back to the Javascript
without IE having a fit you will need to change the permissions of
activex components within the browser.

Jul 13 '06 #1
0 2326

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mark | last post: by
1 post views Thread by Tebogo Tefo via .NET 247 | last post: by
3 posts views Thread by Mike Cain | last post: by
19 posts views Thread by Daniela Roman | last post: by
2 posts views Thread by John Kotuby | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.