One way to get a .NET object to run in a browser is to create a User Control in .NET.
You can then call this User Control from Javascript in your client.
There's nothing magical about this except, it only works on IE and there are a few small bits that you must implement in order for your User Control to be picked up, the errors you get aren't very helpful so I recommend following these steps (Some of these I'm not sure if they're required but they've worked for me).
{
if (_timerTextBox == null)
{
_timerTextBox = new System.Windows.Forms.TextBox();
_timerTextBox.Text = " Time (msec)";
}
_timerTextBox.Text += ("\r\n" + time);
this.Refresh();
}
{
void AddText( string time);
}
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("4F3B57CE-XXXX-XXXX-ACBD-EBB2380990E7")]
public partial class TimerControl : UserControl, IActiveXTimerControl
{
public TimerControl()
{
InitializeComponent();
}
}
}
You now have an Assembly which has a control usable in a browser.
Now to use this Control
Create a website and add the following aspx page (the content of this could also be in html page) inside the body
function doScript()
{
var time = "some time";
activeXTimerControl.AddText( time);
}
Note the clsid above this is crucial it must be the same GUID value as you gave the Class.
You can then call this User Control from Javascript in your client.
There's nothing magical about this except, it only works on IE and there are a few small bits that you must implement in order for your User Control to be picked up, the errors you get aren't very helpful so I recommend following these steps (Some of these I'm not sure if they're required but they've worked for me).
- Create a Class Project in Visual Studio 2005.
- Add a User Control to the project.
- In the User Control Designer add a TextBox Control
- Add a Property to your class to set the Text of the TextBox you've just added.
{
if (_timerTextBox == null)
{
_timerTextBox = new System.Windows.Forms.TextBox();
_timerTextBox.Text = " Time (msec)";
}
_timerTextBox.Text += ("\r\n" + time);
this.Refresh();
}
- Create an Interface in the same namespace as your UserControl and add the signature of the Property you've just added.
{
void AddText( string time);
}
- Mark the Class with a the GuidAttribute and also inherit it from the interface you've just created.
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("4F3B57CE-XXXX-XXXX-ACBD-EBB2380990E7")]
public partial class TimerControl : UserControl, IActiveXTimerControl
{
public TimerControl()
{
InitializeComponent();
}
}
}
- Mark the Assembly as Com Visible in the Project Properties "Assembly Information".
- Compile this and register it by adding a Post-Build Event to your project
- regasm $(TargetFileName) /tlb /codebase
You now have an Assembly which has a control usable in a browser.
Now to use this Control
Create a website and add the following aspx page (the content of this could also be in html page) inside the body
function doScript()
{
var time = "some time";
activeXTimerControl.AddText( time);
}
Note the clsid above this is crucial it must be the same GUID value as you gave the Class.
Comments