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

J

jonathan.beckett

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
story...

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
where.

Regards,

Jonathan Beckett
(e-mail address removed)


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)
[ComVisible(true)]
[Guid("2c4b843f-9c5e-4bc0-83c3-2e2e928f6815")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ImyControlCOMEvents {
[DispId(0x60020000)]
void MyEvent();
}

// Control class, implementing the required interfaces
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ImyControlCOMEvents))]
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){
MyEvent();
}
}

// constructor for the control
public myControl {
InitializeComponent();
}

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

// 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...


<html>
<head>
<title>Test Page</title>
</head>
<body>

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

<object id="uc1"
classid="myControl.dll#my.controls.myControl">
<param name="MyData" value="foo" />
</object>

</body>
</html>


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top