Click here to Skip to main content
15,891,033 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.4K   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: Question Pin
Sk8tz4-Mar-05 1:19
professionalSk8tz4-Mar-05 1:19 
GeneralRe: Question Pin
Welliton Alves Toledo11-Mar-05 16:17
Welliton Alves Toledo11-Mar-05 16:17 
GeneralRe: Question Pin
Sk8tz13-Mar-05 18:11
professionalSk8tz13-Mar-05 18:11 
GeneralRe: Question Pin
Welliton Alves Toledo13-Mar-05 18:25
Welliton Alves Toledo13-Mar-05 18:25 
Generalawsome !!! Pin
DaberElay20-Feb-05 10:23
DaberElay20-Feb-05 10:23 
GeneralRe: awsome !!! Pin
Welliton Alves Toledo21-Feb-05 9:16
Welliton Alves Toledo21-Feb-05 9:16 
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 
dear Abdallah,

as per the first issue you raised here, there is nothing wrong with .Net 2.0,
in fact it has nothing to do with it, its actually an update made on the browser itself, you may also notice it happening on falsh objects embedded in html on web sites.
The reason for is originates in a patent law suit held by someone over embedding interactive objects within the browser. in order not to pay this guy for every browser installation, microsoft makes you click to activate such objects (namely .net applets and flash, and i guess also java applets although i did see on in a while ).

regarding your second problem. I cant beleive that the browser know's its a .net component and behaves differently, because the implementation behind it is simply calling an activeX that implements a certain interface and which is unfamilier to the browser. i suggest you test your website against other browsers to see whether this behavior prevails. i'm not sure if firefox supports adding .net applets, but my guess is that it should because after all its the same interface it uses for flash/java applets, in any way you should see if the rest of the page is being drawn correctly...

good luck
nimrod cohen.
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 
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.