Click here to Skip to main content
15,891,136 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: be carefull of ComVisible attribute Pin
ProJee30-Oct-06 4:35
ProJee30-Oct-06 4:35 
GeneralRe: be carefull of ComVisible attribute Pin
cdmiwa13-Nov-06 9:21
cdmiwa13-Nov-06 9:21 
GeneralRe: be carefull of ComVisible attribute Pin
sakhawatAli6-Jan-09 1:05
sakhawatAli6-Jan-09 1:05 
GeneralCrashes Internet Explorer Pin
up_late6-Jun-06 15:40
up_late6-Jun-06 15:40 
GeneralRe: Crashes Internet Explorer Pin
wamra15-Jun-06 6:29
wamra15-Jun-06 6:29 
GeneralRe: Crashes Internet Explorer Pin
wamra15-Jun-06 6:34
wamra15-Jun-06 6:34 
Generalsecurity Pin
53R15-May-06 23:51
53R15-May-06 23:51 
GeneralPermissions Pin
Axon26-Mar-06 1:24
Axon26-Mar-06 1:24 
What permission for web users should be set to the bin folder?

Can the .net control be working well without the read permission for web users on the folder containing the control's dll?

Ax.
Generalrefresh problem Pin
bookerman7-Apr-05 22:21
bookerman7-Apr-05 22:21 
GeneralRe: refresh problem Pin
Welliton Alves Toledo16-Apr-05 3:19
Welliton Alves Toledo16-Apr-05 3:19 
GeneralRe: refresh problem Pin
rictursi4-Sep-06 10:12
rictursi4-Sep-06 10:12 
GeneralRe: refresh problem Pin
Welliton Alves Toledo5-Sep-06 3:21
Welliton Alves Toledo5-Sep-06 3:21 
GeneralRe: refresh problem Pin
rictursi5-Sep-06 7:36
rictursi5-Sep-06 7:36 
GeneralRe: refresh problem Pin
rictursi6-Sep-06 6:25
rictursi6-Sep-06 6:25 
GeneralWebSharing Pin
Patrice Dargenton2-Apr-05 21:34
Patrice Dargenton2-Apr-05 21:34 
GeneralRe: WebSharing Pin
Welliton Alves Toledo3-Apr-05 15:00
Welliton Alves Toledo3-Apr-05 15:00 
GeneralRe: WebSharing Pin
Patrice Dargenton4-Apr-05 4:54
Patrice Dargenton4-Apr-05 4:54 
GeneralQuestion Pin
Sk8tz27-Feb-05 17:59
professionalSk8tz27-Feb-05 17:59 
GeneralRe: Question Pin
Welliton Alves Toledo2-Mar-05 13:40
Welliton Alves Toledo2-Mar-05 13:40 
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 

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.