G
Guest
I am trying to get a UserControl hosted in IE working. Having read some
articles on this and followed the examples (Jay Allen’s article at
http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/
was a great source, especially for the security side of things), I have
been able to use params to set properties, use the local filesystem and
access the web from with the control.
What I really need to get working is interaction with the hosting page, in
other words calling methods / setting properties / handling events via
javascript.
I have seen that many others are having the same problems as I am when
trying to do this. In my sample I have a simple control with a single button
and textbox; clicking the button calls a method that gets the machines
hostname (using System.Net.Dns.GetHostName) before displaying this in the
textbox.
I also have a public method on the control that should do the same – it is
this method that I would like to call from javascript.
Within this method an event is raised once the hostname has been obtained,
again I would like to handle this event in javascript.
I believe I have followed the requirements as shown in the articles
(however, I am using .NET 2.0 and think those articles were for 1.0/1.1) to
both expose properties and methods and raise events.
The code for my control is as follows:
using ...;
namespace ClientSideControl
{
[ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IMyControlCOMEvents))]
public partial class MyControl2 : UserControl, IMyControlCOMIncoming
{
public MyControl2()
{
InitializeComponent();
}
#region Private Properties
private string _hostname = "";
#endregion
#region Private methods
private void DoHostIdentificationComplete()
{
if (HostIdentificationComplete != null)
{
new
SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
HostIdentificationComplete();
CodeAccessPermission.RevertAssert();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.GetHostName();
}
#endregion
#region IMyControlCOMEvents
public delegate void HostIdentificationCompleteHandler();
public event HostIdentificationCompleteHandler
HostIdentificationComplete;
#endregion
#region IMyControlCOMIncoming
public void GetHostName()
{
_hostname = Dns.GetHostName();
textBox1.Text = _hostname;
this.DoHostIdentificationComplete();
}
public string HostName
{
get { return _hostname; }
}
#endregion
}
/// <summary>
/// Source interface for hooking up to COM events so that
JScript/VBScript can sink event
/// handlers with us. Disgusting, but it works.
/// </summary>
[Guid("E85219FB-04F2-4230-A579-CD7ACF0CAACB")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyControlCOMEvents
{
[DispId(0x60020000)]
void HostIdentificationComplete();
}
/// <summary>
/// Default incoming interface for our control. Required when using
COM-style events,
/// otherwise IE will no longer be able to access our public properties
and methods.
/// </summary>
public interface IMyControlCOMIncoming
{
void GetHostName();
string HostName { get; }
}
}
And the hosting page:
<html>
<head>
<title>IE Hosted UserControl</title>
<script for="myControl2" event="HostNameObtained">
var hostname = myControl2.HostName;
window.status = "HostName obtained : " + hostname;
</script>
<script language="javascript" type="text/javascript">
function GetHostName()
{
myControl2.GetHostName();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<object id="myControl2" name="myControl2" width="400"
height="80" classid="ClientSideControl2.dll#ClientSideControl.MyControl2">
</object>
<br />
<br />
<input type="button" id="btn1" onclick="GetHostName();"
value="get hostname" name="btn1">
</div>
<br />
</form>
</body>
</html>
This is the final step to getting things working and being able to use this
technique for a host of things we’re looking at internally.
What happens right now is that when I click on the “get hostname†button the
following error appears:
---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?
Line: 15
Error: 'myControl2' is undefined
---------------------------
Yes No
---------------------------
What I would like to happen here is that the javascript calls the controls
GetHostName method, which raises the event “HostIdentificationComplete†and
displays the hostname on the status bar of the browser.
I have set things up so Full Access is available to the control.
Please help! (if this is the wrong group then please can you let me know
which one this cross group question should go to - thanks!)
articles on this and followed the examples (Jay Allen’s article at
http://msdn.microsoft.com/msdnmag/issues/02/01/UserCtrl/
was a great source, especially for the security side of things), I have
been able to use params to set properties, use the local filesystem and
access the web from with the control.
What I really need to get working is interaction with the hosting page, in
other words calling methods / setting properties / handling events via
javascript.
I have seen that many others are having the same problems as I am when
trying to do this. In my sample I have a simple control with a single button
and textbox; clicking the button calls a method that gets the machines
hostname (using System.Net.Dns.GetHostName) before displaying this in the
textbox.
I also have a public method on the control that should do the same – it is
this method that I would like to call from javascript.
Within this method an event is raised once the hostname has been obtained,
again I would like to handle this event in javascript.
I believe I have followed the requirements as shown in the articles
(however, I am using .NET 2.0 and think those articles were for 1.0/1.1) to
both expose properties and methods and raise events.
The code for my control is as follows:
using ...;
namespace ClientSideControl
{
[ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IMyControlCOMEvents))]
public partial class MyControl2 : UserControl, IMyControlCOMIncoming
{
public MyControl2()
{
InitializeComponent();
}
#region Private Properties
private string _hostname = "";
#endregion
#region Private methods
private void DoHostIdentificationComplete()
{
if (HostIdentificationComplete != null)
{
new
SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
HostIdentificationComplete();
CodeAccessPermission.RevertAssert();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.GetHostName();
}
#endregion
#region IMyControlCOMEvents
public delegate void HostIdentificationCompleteHandler();
public event HostIdentificationCompleteHandler
HostIdentificationComplete;
#endregion
#region IMyControlCOMIncoming
public void GetHostName()
{
_hostname = Dns.GetHostName();
textBox1.Text = _hostname;
this.DoHostIdentificationComplete();
}
public string HostName
{
get { return _hostname; }
}
#endregion
}
/// <summary>
/// Source interface for hooking up to COM events so that
JScript/VBScript can sink event
/// handlers with us. Disgusting, but it works.
/// </summary>
[Guid("E85219FB-04F2-4230-A579-CD7ACF0CAACB")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyControlCOMEvents
{
[DispId(0x60020000)]
void HostIdentificationComplete();
}
/// <summary>
/// Default incoming interface for our control. Required when using
COM-style events,
/// otherwise IE will no longer be able to access our public properties
and methods.
/// </summary>
public interface IMyControlCOMIncoming
{
void GetHostName();
string HostName { get; }
}
}
And the hosting page:
<html>
<head>
<title>IE Hosted UserControl</title>
<script for="myControl2" event="HostNameObtained">
var hostname = myControl2.HostName;
window.status = "HostName obtained : " + hostname;
</script>
<script language="javascript" type="text/javascript">
function GetHostName()
{
myControl2.GetHostName();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<object id="myControl2" name="myControl2" width="400"
height="80" classid="ClientSideControl2.dll#ClientSideControl.MyControl2">
</object>
<br />
<br />
<input type="button" id="btn1" onclick="GetHostName();"
value="get hostname" name="btn1">
</div>
<br />
</form>
</body>
</html>
This is the final step to getting things working and being able to use this
technique for a host of things we’re looking at internally.
What happens right now is that when I click on the “get hostname†button the
following error appears:
---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?
Line: 15
Error: 'myControl2' is undefined
---------------------------
Yes No
---------------------------
What I would like to happen here is that the javascript calls the controls
GetHostName method, which raises the event “HostIdentificationComplete†and
displays the hostname on the status bar of the browser.
I have set things up so Full Access is available to the control.
Please help! (if this is the wrong group then please can you let me know
which one this cross group question should go to - thanks!)