473,468 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2466

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",...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.