How do Java applets fit into the world of .NET? The short answer is that they don’t, but it’s possible to achieve a similar design using Windows Forms controls.

ASP.NET may or may not be the best thing since sliced bread, but it is a relatively new kid on the block. I first realized its true power in 2001 during a developer conference on a sunny day in downtown Boston. That is, it was sunny until the great Don Box decided to take off his t-shirt in front of the audience, but that is another story.

ASP.NET has now been around for several years and every day it attracts new arrivals. The other day I was asked if there is an adequate equivalent to Java applets in .NET.

That is a good question. Here is a way to achieve a similar effect.

Creating a WinForm control

Start Visual Studio, create a new C# project using the Windows Control Library and name it WinFormControl.

Create form window

This will give us a blank canvas. I usually stay away from design mode, but this is just a simple demonstration. Create a button and a few text labels.

Blank canvas

Name one of the text labels lblMessage. In the click event for the button, write something like the following code:

private void button1_Click(object sender, System.EventArgs e) { lblMessage.Text = "This is the response message."; }

Compile and copy the DLL file into a new directory.

HTML

Next step is to create the HTML page where we embed the control. Create a text file with the following content:

<html> <head> <title>WinForms Control example</title> </head> <body> <h1>WinForms Control example</h1> </body> <object id="UserControl1" classid="http:UserControl1.dll#WinFormControl.UserControl1" height="400" width="400"> </object> </html>

UserControl1.dll is the name of the assembly created in the previous step, WinFormControl is the namespace and UserControl1 the control itself. Name the file ControlHost.html and place it where you placed the DLL file.

Web server configuration

Start the IIS management snap-in and create a new virtual directory by right-clicking "Default Web Site", select New -> Virtual Directory, specify alias WinFormsControl and locate the directory where you placed the DLL and HTML files. The default access permissions, "Read" and "Run scripts" will do fine.

Properties in IIS

View result

Start your Internet Explorer browser (sorry, this trick won’t work in better browsers such as Mozilla) and navigate to http://localhost/winformscontrol/controlhost.html

The control will load into the HTML page. Click the button and verify that the message is displayed.

View result

It’s a quick and dirty solution with lots of room for improvement. Happy coding!

Comments

No comments yet.

Leave a reply