Click here to Skip to main content
15,898,036 members
Articles / Web Development / HTML
Article

A very easy .NET “Applet”

Rate me:
Please Sign up or sign in to vote.
3.16/5 (17 votes)
13 Mar 2005CPOL2 min read 171.7K   1.7K   46   56
Make a Windows Forms UserControl work like an Applet.

Sample Image - CSharpApplet.gif

Introduction

Some time ago, I needed my web application to interact with a specific hardware in the computer, but I didn't want to write an ActiveX, I wanted to write it in a .NET language. So I made a search in the web and I found some examples that helped me on that. In this article I will use a UserControl as an applet on my .htm page.

This example of "applet" doesn't make anything relevant. It's only to show how to put a Windows Form's code in a web page and interact with its properties and methods.

How it works?

Everything is very simple, it isn’t a “black box”. I created a Class Library project, then I added a Windows Forms UserControl and drag-and-drop'ed some components. Then I added an HTML page in the same project and I changed the code to:

HTML
<html>
   <head>
   <title>My Applet</title>
   <script language=javascript>
   <!--

     function Button1_onclick() {
        document.getElementById("myID").MyMethodReset();
    }

   //-->
  </script>
  </head>
  <body>
      <OBJECT id="myID" height="150" width="300" 
        classid="http:bin/release/CSharpApplet.dll#CSharpApplet.MyApplet"
        VIEWASTEXT>
     </OBJECT>
    <P>
    <INPUT id="Button1" type="button" value="Reset by way of javascript" 
        name="Reset" onclick="return Button1_onclick()">
  </body>
</html>

On the tag OBJECT the property classid is composed by the path (I used relative path) and name of the DLL, plus namespace and class name.

Note that MyApplet (UserControl) has a button with the caption “Reset” that calls the MyMethodReset public method, setting the value of the progress bar to zero. And on the HTML file, I have another button that calls the same public method MyMethodReset by way of JavaScript!

Remarks

You must create a WebSharing at the folder CSharpApplet from this source code example. Then run by, for example: http://localhost/CSharpApplet/MyApplet.htm.

Of course, .NET Framework is to be installed on clients.

Depending of what your user control will do and where it will run (by intranet/Internet), it will be necessary to trust the assembly by making proper settings on Administrative Tools > Microsoft .NET Framework Configuration/Wizards.

That’s all and sorry for my bad English!

Revision history

  • 20 February 2005

    English improvements :P

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
Welliton is a nice brazilian guy.
He started the kidding with software with his "top" PC-AT 286 in 1991 when he was 14 years old.
Now he begins solo career in a software company (http://www.actuar.com.br) in Goiânia-Goiás - Brazil

Comments and Discussions

 
GeneralRe: awsome !!! Pin
wamra15-Jun-06 6:32
wamra15-Jun-06 6:32 
GeneralRe: awsome !!! Pin
DaberElay15-Jun-06 22:25
DaberElay15-Jun-06 22:25 
GeneralFirefox Pin
Klapouchy13-Feb-05 23:02
Klapouchy13-Feb-05 23:02 
GeneralRe: Firefox Pin
Welliton Alves Toledo14-Feb-05 1:03
Welliton Alves Toledo14-Feb-05 1:03 
GeneralRe: Firefox Pin
Klapouchy14-Feb-05 1:12
Klapouchy14-Feb-05 1:12 
GeneralRe: Firefox Pin
jake h14-Feb-05 7:09
jake h14-Feb-05 7:09 
GeneralRe: Firefox Pin
Welliton Alves Toledo14-Feb-05 15:35
Welliton Alves Toledo14-Feb-05 15:35 
GeneralRe: Firefox Pin
Anonymous23-Feb-05 3:43
Anonymous23-Feb-05 3:43 
You wouldn't want Firefox or Mozilla to support the OBJECT tag would you? It was the worse security decision Microsoft ever made. The security was all or none, there was no sandbox for the object. A developer could easily write an ActiveX object that formats the hard drive in IE. Microsoft never did care about security in web-enabled applications...
GeneralRe: Firefox Pin
Welliton Alves Toledo23-Feb-05 8:53
Welliton Alves Toledo23-Feb-05 8:53 
GeneralRe: Firefox Pin
The_Decryptor14-Mar-05 1:49
The_Decryptor14-Mar-05 1:49 
GeneralRe: Firefox Pin
Judah Gabriel Himango21-Feb-05 8:12
sponsorJudah Gabriel Himango21-Feb-05 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.