Windows Develop Bookmark and Share   
 index > Windows Forms General > Embedded User Control and Events in IE 8
 

Embedded User Control and Events in IE 8

Hello,

I am trying to develop a Windows Forms user control to be embedded in IE 8.
The control is showing fine, but I can't fire an event that I can catch in JavaScript.
My code is like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;

[assembly: ClassInterface(ClassInterfaceType.AutoDual)]

namespace ASPNETBasicsWorkshop.Windows.Forms.UserControl
{
[Guid("C76FFA39-6EDF-49a5-A825-C66B1E4FC32C")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyControlData
{
String MyData
{
get;
set;
}
}

[Guid("2c4b843f-9c5e-4bc0-83c3-2e2e928f6815")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyControlEvents
{
[DispId(0x0)]
void MyEvent();
}

public delegate void MyEventHandler();

[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IMyControlEvents))]
public partial class MyControl : System.Windows.Forms.UserControl, IMyControlData
{
public MyControl()
{
}

public event MyEventHandler MyEvent;

[SecurityPermission(SecurityAction.Assert, Unrestricted = true, UnmanagedCode = true)]
protected void OnMyEvent()
{
if (this.MyEvent != null)
{
this.MyEvent();
}
}

protected override void OnLoad(EventArgs e)
{
Button button = new Button();
button.Name = "MyButton";
button.Text = "Click Me";
button.Click += MyButton_Click;
this.Controls.Add(button);

base.OnLoad(e);
}

private void MyButton_Click(Object sender, EventArgs e)
{
this.OnMyEvent();
}

public String MyData
{
get;
set;
}
}
}

And my HTML:

<%@ Page Language="C#" MasterPageFile="~/Site.Master" CodeBehind="Controls.aspx.cs" Inherits="ASPNETBasicsWorkshop.Controls" %>
<asp:Content ContentPlaceHolderID="body" runat="server">
<object id="MyControl" classid="http:ASPNETBasicsWorkshop.Windows.Forms.UserControl.dll#ASPNETBasicsWorkshop.Windows.Forms.UserControl.MyControl" width="300" height="200">
</object>
<script type="text/javascript" for="MyControl" event="MyEvent">
window.status = 'Selection: ' + window.document.getElementById('CustomControl').SelectionStart + ' to ' + window.document.getElementById('CustomControl').SelectionEnd;
</script>
</asp:Content>

Whenever I click on the button, I get a security exception on method OnMyEvent. I've tried adding a SecurityPermissionAttribute, but no luck.

Any ideas?

Thanks,

Ricardo Peres

rjperes  Thursday, September 17, 2009 4:16 PM
BUG: Security Exception When You Use Event Handlers in Internet Explorer
The WinForm Setup forum is for questions related to using Windows Forms controls in IE.


The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP
Sheng Jiang 蒋晟  Thursday, September 17, 2009 10:13 PM
Hello,

Have you got any progress on this issue Sheng's suggestion? If there is anything else we can help, welcome to post here.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Wednesday, September 23, 2009 8:46 AM

Hello,

First of all, thanks for the help!

No,Sheng's suggestionwas useless, since security can no longer be turned off:

C:\Documents and Settings\rjperes>caspol -s on
Microsoft (R) .NET Framework CasPol 2.0.50727.3053
Copyright (c) Microsoft Corporation. All rights reserved.

Because security can no longer be turned off, turning on security does not have
any effect.
Success

I managed to fire the event handlers by creating a code group in the.NET Configuration tool, assigning my control to that group and giving some permissions. I believe this has something to do with IE 8 and the new security model.
What I still can't do is call a public method from JavaScript: I always get a "undefined method" exception.

Any clues?

RP

rjperes  Wednesday, September 23, 2009 9:19 AM
Hello RP,

Thanks for your feedback.

As suggested in the article, we need to use .NET Framework Configuration tool (Mscorcfg.msc) to grant the required, individual permissions to the assembly. You can also run the CMD prompt as Administrator(elevated process), and use "caspol -s off" to ture off the security temporarily and run the web page to see if the problem continues.

Since your problem is about IE, I suggest that you also try on the IE forum.
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Thursday, September 24, 2009 11:41 AM

You can use google to search for other answers

Custom Search

More Threads

• Visual Styles documentation
• TextRenderer?
• When ShowDialog() is called with a parent, memory is not released
• XmlDocument.Load Question
• how to rectify paint event problem
• FontDialog and TrueType fonts
• Filename from ListView
• Control.Invoke() hangs when desktop is locked.
• edit subitems of listview control using ATL/WTL
• What would you do with a [property] As System.ComponentModel.PropertyDescriptor in IBindingList?