Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > C# UserControl in IE, no events
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 27th March 11:33
michael neumann
External User
 
Posts: 1
Default C# UserControl in IE, no events


I am fairly new to .Net and I'm trying to build a C# UserControl that can be
run in IE that triggers events I can respond to with client-side scripting.
My environment is VS 2005 and IE 6.0 SP2, I'm using .NetV2.

After some time spent looking around in MSDN and Google I built a
UserControl (strong named) whose guts look like this:

namespace EventTest
{
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
[Guid("6C4ED6D6-AA34-4538-BEF7-BA287AFF6BF2")]
public interface IClientControlEvents
{
[DispId(1)]
void InfoMessage(string message);
}

public delegate void InfoMessageEventHandler(string data);

[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IClientControlEvents))]
public partial class UplTest : UserControl
{
public event InfoMessageEventHandler InfoMessage;

public UplTest()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (InfoMessage != null)
InfoMessage("Bingo!");
else
label1.Text = "No Handler";
}
}
}

In IE I have:

<object id="myControl" classid="EventTest.dll#EventTest.UplTest"></object>

<script language="javascript" for="myControl" event="InfoMessage(data)">
alert(data);
</script>

The control always starts up fine. It has a label and a button. When I click
the button, the code should trip the event, but the InfoMessage event
variable is always null.

Even though this is all running on my local machine I thought it might be a
permissions issue, so I went into the .Net configuration manager and altered
my settings so the assembly evaluates as "Unrestricted" when pulled over
http.

Before I went down this road and added the second interface and the COM-like
stuff, I tried creating some standard C# string properties with get/set
methods and I could not see those in IE either.

I feel certain I'm doing something really basic wrong. Has anybody tried
this with .NetV2 and gotten it to work?

Thanks very much for your help,

Mike N.
  Reply With Quote


  sponsored links


2 27th March 17:08
v-phuang@online.microsoft.com (peter huang
External User
 
Posts: 1
Default C# UserControl in IE, no events


Hi,

Currently I am researching the issue and we will reply here with more
information as soon as possible.
If you have any more concerns on it, please feel free to post here.

Thanks for your understanding!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - http://www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
  Reply With Quote
3 27th March 21:22
v-phuang@online.microsoft.com (peter huang
External User
 
Posts: 1
Default C# UserControl in IE, no events


Hi Mike,

By default, when you VS.NET IDE will set the ComVisible attribute to false
in the AssemblyInfo.cs file.
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

So we need to enable at the interface level, so that after we check the
register for Com Interop, there will be tlb file generated in the same
directory of the dll.
namespace UserControlTest
{
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
[Guid("6C4ED6D6-AA34-4538-BEF7-BA287AFF6BF2"),ComVisible(true)]//NOTE
here
public interface IClientControlEvents
{
[DispId(1)]
void InfoMessage(string message);
}

public delegate void InfoMessageEventHandler(string data);

[ClassInterface(ClassInterfaceType.None)]

[ComSourceInterfaces(typeof(IClientControlEvents)), ComVisible(true)]//NOTE
here
public partial class UplTest : UserControl
{
public event InfoMessageEventHandler InfoMessage;

public UplTest()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Environment.Version.ToString());
if (InfoMessage != null)
InfoMessage("Bingo!");
else
label1.Text = "No Handler";
}
}
}

Also there is link for your reference.
Host Secure, Lightweight Client-Side Controls in Microsoft Internet Explorer
http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/default.aspx


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - http://www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
  Reply With Quote
4 28th March 20:08
michael neumann
External User
 
Posts: 1
Default C# UserControl in IE, no events


Awesome!

That fixed the problem.

Here's the final code that worked, I also had to implement IObjectSafety so
IE wouldn't block the events and show the "security ribbon" at the top of
the page.

namespace EventTest
{

[ComImport]
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, out int
pdwSupportedOptions, out int pdwEnabledOptions);

[PreserveSig]
int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask,
int dwEnabledOptions);
}

[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
[Guid("6C4ED6D6-AA34-4538-BEF7-BA287AFF6BF2"), ComVisible(true)]
public interface IClientControlEvents
{
[DispId(1)]
void InfoMessage(string message);
}

public delegate void InfoMessageEventHandler(string data);

[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IClientControlEvents)), ComVisible(true)]
public partial class UplTest : System.Windows.Forms.UserControl,
IObjectSafety
{
public event InfoMessageEventHandler InfoMessage;
protected string akito = "Ow! Starrt";

// Constants for implementation of the IObjectSafety interface.
private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;

public UplTest()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (InfoMessage != null)
InfoMessage("Wahoo!");
else
label1.Text = "No handler";
}

public string Akito
{
get { return akito; }
set {
akito = value;
label1.Text = akito;
}
}

// Implementation of the IObjectSafety methods.
int IObjectSafety.GetInterfaceSafetyOptions(ref Guid riid, out int
pdwSupportedOptions, out int pdwEnabledOptions)
{
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA;
return S_OK; // return S_OK
}

int IObjectSafety.SetInterfaceSafetyOptions(ref Guid riid, int
dwOptionSetMask, int dwEnabledOptions)
{
return S_OK; // return S_OK
}
}

}

Thanks for your help!

Mike N.
  Reply With Quote
5 29th March 09:16
v-phuang@online.microsoft.com (peter huang
External User
 
Posts: 1
Default C# UserControl in IE, no events


Hi Mike,

You are welcome and thanks for your experiences sharing in the community.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - http://www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666