Click here to Skip to main content
15,867,453 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 170.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

 
QuestionNot working for computers outside localhost Pin
Ivn Y.30-Jun-11 1:07
Ivn Y.30-Jun-11 1:07 
GeneralDrag and Drop the controls at runtime in ASP.NET application Pin
Akshay srikanth4-Aug-09 4:33
Akshay srikanth4-Aug-09 4:33 
QuestionHow to access into java Web application Pin
bhangale.parag12-Jan-09 3:29
bhangale.parag12-Jan-09 3:29 
QuestionHas anyone got this to work with non-.NET (ActiveX) controls? Pin
Sanx7221-Aug-08 3:47
Sanx7221-Aug-08 3:47 
Question.NET Applet works only on IIS? Pin
easyguy10-Jul-08 18:46
easyguy10-Jul-08 18:46 
AnswerThis Code works correctly :) Pin
adel31214-Jan-08 12:09
adel31214-Jan-08 12:09 
General&#304;t is not working Pin
Hakan Fatih YILDIRIM31-Oct-07 6:13
Hakan Fatih YILDIRIM31-Oct-07 6:13 
QuestionDoes it work on Apache ? Pin
norbp15-Jul-07 0:18
norbp15-Jul-07 0:18 
QuestionInvisible applet Pin
euthebium20-Jun-07 22:41
euthebium20-Jun-07 22:41 
GeneralThis example doesnt work Pin
janocho8-Feb-07 9:46
janocho8-Feb-07 9:46 
GeneralRe: This example doesnt work Pin
Welliton Alves Toledo9-Feb-07 4:43
Welliton Alves Toledo9-Feb-07 4:43 
GeneralRe: This example doesnt work Pin
xilogon17-Nov-09 3:48
xilogon17-Nov-09 3:48 
GeneralTrust assembly Pin
janocho7-Feb-07 13:08
janocho7-Feb-07 13:08 
GeneralRe: Trust assembly Pin
Welliton Alves Toledo9-Feb-07 4:44
Welliton Alves Toledo9-Feb-07 4:44 
Generalsecutity permission Pin
rizomatosa26-Jan-07 9:18
rizomatosa26-Jan-07 9:18 
GeneralRe: secutity permission Pin
Welliton Alves Toledo9-Feb-07 4:45
Welliton Alves Toledo9-Feb-07 4:45 
GeneralFailed to grant minimum permission requests Pin
rictursi4-Sep-06 10:30
rictursi4-Sep-06 10:30 
GeneralRe: Failed to grant minimum permission requests Pin
Welliton Alves Toledo5-Sep-06 3:27
Welliton Alves Toledo5-Sep-06 3:27 
Generalbe carefull of ComVisible attribute Pin
ernest_elias29-Aug-06 3:37
ernest_elias29-Aug-06 3:37 
I created my own testing library in VS.NET 2005 with HelloWorld User Control. The control displays string "Say Hello" and it has public method Hello().
The control was displayed correctly, but IE was unable to call the method through Javascript.
I had to do long-time exploration and I found that VS.NET 2005 creates attribute [assembly: ComVisible(false)] in AssemblyInfo.cs (by default), which makes the method invisible to IE.
I solved the problem due to your sample project Smile | :)
Thank you

-ernest

GeneralRe: be carefull of ComVisible attribute Pin
Welliton Alves Toledo5-Sep-06 3:25
Welliton Alves Toledo5-Sep-06 3:25 
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 

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.