472,805 Members | 834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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
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
jo**************@gmail.com
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.InterfaceIsIDispatc h)]
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.

Jul 13 '06 #1
0 2427

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
9
by: Greg Muncien | last post by:
In VB.Net, when you double click a control you see all available events of that control in a combo box. Click on one, and the interface code is right there waiting for you to type implementation....
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
1
by: Tebogo Tefo via .NET 247 | last post by:
Hi I have a user control that contains three dropdownlists that I populate according to what was selected in the other dropdownlist (i.e. populate dropdownlist2 after selecting a value in...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
3
by: Mike Cain | last post by:
Hi I have a JS library that I want others in my company to use that uses AttachEvent (etc) to add a method to the onLoad event so that it is called. Using this approach of course is good because...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
1
by: Duncan | last post by:
Hi all, I'm tearing my hair out on this! I have a simple ASP.NET 2 page with a TextBox control, in the PreRender I set ClientSide events:- Response.Attributes.Add("onchange",...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.