Hosting a Windows Form Control in a web page

Although it is not the most common use of it, it is possible to host a Windows Form Control in a Web Page and run it from within Internet Explorer. This allows to build powerful client side functionality with all the advantages of using the .Net framework and executing the control in the client side. Of course there are some restrictions that cannot be left aside. At least the .Net framework must be installed on the client for the control to run. In addition, it is possible that some permission must be granted to the control, too, depending on the actions the control will take on the client machine.

Let’s build an example to see how this works:

1. Create the Windows Form Control

Create a “Windows Control Library” project in Visual Studio. For this simple example we will add a label to the control and a public “SendMessage” method to change the label’s text. In order to be able to call the method from IE, we will set the control’s COMVisible attribute to true.

 

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsControlLibrary1

{

    [ComVisible(true)]

    public partial class UserControl1 : UserControl

    {

        public UserControl1()

        {

            InitializeComponent();

        }

 

        public void SendMessage(string msg){

            _lblMessage.Text = msg;

        }

    }

}

 

2. Create the hosting HTML document

 

The control will be hosted using the HTML <object> element. In its classid attribute we will place the control's reference in the following manner: classid="http:[relativePath/]<winControlAssemblyName>.dll#<ControlNamespace>.<ControlClassName>". For this example we will have an HTML page (index.html) with the following code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >   
 <head>       
  <title>Windows Form Control Test Page</title>       
  <script type="text/javascript">           
    function SendMessageToWinControl()
    {                     
     var winCtrl = document.getElementById("MyWinControl");                   
     winCtrl.SendMessage("Message sent from the HTML page!!");               
    }       
  </script>   
 </head>   
 <body>       
  <h1>Windows Form Control:</h1>       
  <object id="MyWinControl" classid="http:WindowsControlLibrary1.dll#WindowsControlLibrary1.UserControl1"           
          height="100" width="300" VIEWASTEXT/>       
  <br/><br/>       
  <input type="button" onclick="SendMessageToWinControl()" value="Send Message" />   
 </body>
</html> 

  

3. Deploy the sample

 

Copy both the index.html page and the control library WindowControlLibrary1.dll to a directory (we will name it WinControlTest). In the “Web Sharing” dir properties select  Share this folder, leave the default alias and application permissions to Scripts and make sure not to select  the Execute permission).

 

4. Try it in Internet Explorer

 

Open the page http://localhost/WinControlTest/index.html in Internet Explorer. We can see the control hosted in the html page:

 

 

By clicking the Send Message button, we will call the Control's SendMessage method from javscript. Resulting in the label's text changed:

 

 

 

If you modify the control after its first use, you will need to close IE, replace the control's dll in the virtual directory and clean the contents of the download cache. You do this cleaning using the following command: "gacutil /cdl".To list the contents of the download cache use "gacutil /ldl".

 

This solution is not intended to replace a WebControl or any common web artifact. But is good to know it can be use in an uncommon scenario if needed.

 

Sole

 

42 Comments

  • Hi Mau, to grant permissions to your control go to Administrative Tools ->Microsoft .Net Framework 2.0 Configuration. Under My Computer -> Runtime Security Policy -> Machine -> Code Groups -> All_Code select New… to create a new Code Group. Set a descriptive name for the group, select URL as the condition type and enter the URL of you site, for example http://localhost/WinControlTest/*. Finally assign the FullTrust permission set to the code group.
    For a production environment you should be more careful and assign only the minimum permissions instead of full trust and use a Strong Name condition type rather than a URL one. But for development purposes the above should work perfectly.

  • Mau, try changing the membership condition to All Code in the code group properties.

  • I make a control and i hosted it; but when i refresh the page, the control dissapears

    Please do you have some solution

    Thanks

  • Thanks Spano, that worked beautifully. Great work.

  • Hi Spano, I tried your sample but the send message button seem to get lost. Did I miss something?

    TIA,
    John

  • This article is good. I did it and worked fine in localhost. But when I did in internet(my production web server) The control did not load at all. I modified the Security settings in IE also did include the controls under the URL as full Trust through the .NET Runtime. Nothing seems to work and the control will not load. Any help would be appreciated. I use IE6 SP2 on a Win XP.

    Thanks in advance.

  • Hi Balu, it seems that when executing on the client, your control is being downloaded but not the referenced webcamlib assembly, isn't it? You can check by executing "gacutil /ldl" on the client machine and see if all needed assemblies are in the download cache.

    Honestly, I haven't try the case where the control referencies another assembly that is not in the GAC. As an alternative you can try to install the webcamlib on the GAC on the client machine and see what happens.

  • hi

    is it possible to create an event in the winform control and capture it with javascript on the web page.

    I just can't get it to work

    hope you can help
    cheers
    james

  • I created user control which inturn calls Vncsharp.dll.
    I am getting "could not load Vncsharp.dll" error.
    I tried setting up full trust for the dll.But I get the same error.Is it possible to embed VncSharp.dll in a another user control?

    thanks..

  • Has anyone figured out a way to debug the windows forms control code behind logic when its dll is only referenced in the aspx web page html object element and the dll is placed in the root of the aspx web page?

    I have tried quite a lot of things and still have not successfully not gotten it to work.

    The project I am working on has this scenario, with parameters being passed between the windows forms control and the aspx web page it in on.

    cnc

  • cnc, the best that I can come up with to debug the windows control while it was running is to place MessageBox.Show at places where I want to get info back.

  • Hi Sole this is very good sample for the hosting program.
    can you tell me how to call the hosting page(like asp.net) event from hosted object(Windows Form Control) ?

  • I would like to know how to include multiple libraries (.dll files) with my project. Do I make a cabinet file to do this? If so how do you deploy it.

    I see a bunch of people saying "my control works fine here but not on my intranet or internet". This is most likely because of the nightmare known as "Code Access Security". You can read about it under the MSDN on "Windows Forms Security". It is a pain to debug any kind of technology like this. Best of luck to you all.

  • Hello Dear

    I've read in some articles that this Security stuff hasn't to deal with framework configuration nor Code Access Security. As i know the client just has to "trust" the website you are connecting to.

    Since this is "ActiveX"...

  • Hi,

    Nice artical for hosting windows control in web page.
    But suppose if i have multiple frameworks installed on the client system say 1.1, 2.0 etc.
    My control is created using framework 1.1 and is not compatible with 2.0 and onwards.
    So when i try to access my control on client system, control is getting invoked under framework 2.0.
    So any idea, how can we control this.

    Regards,
    Harry

  • i m getting javascript error for the above code 'object doesn't support this property or method'

  • I have created a simple Windows Control Lib, and put it in a web page. It works OK. But, when I modified the Lib dll, the web page can't display the control any more. Any one know the solution?

    Thanks

  • For Jeffery Z's I think you would need to do GACUTIL /cdl

    My problem is that the control displays for IT users with elevated permissions but not for our normal users.

  • Hello, I will repeat the Caru question because I have the exact same thought...

    Hi Sole this is very good sample for the hosting
    program.

    can you tell me how to call the hosting page(like asp.net) event from hosted object(Windows Form Control) ?

    Thanks
    Eric Ouellet

  • Hi,

    It's working fine for me. But i tried with another example. After login form i want to show an MDI form in IE. Is it possible & how to do it. Because for me if click the login button, the MDI form comes seperate window.

  • Hi all, I have one problem, When I do my Windows User Control I need add one external DLL for display some medical image, compile my project and genereted the dll, so, This dll export to my Web Application and creat with tag



    but this object not show in my IE, I guess that is for the DLL external, but i do not know include this dll, any idea for this problem??

    In my cache only download WindowsFormsControlLibrary1.dll,Please help me Im going crazy for this!!!!

    Regards

    Sorry with my english

  • Hi,

    I bulit a new Class Library project and got a DLL. I also added app.config file in the class library project.
    Now i am calling this DLL in the web application. Everything is working fine. But now i want to read the appName.exe.config file for connection string using the DLL.
    Any idea will be more helpfull as i need it very urgently.

  • Thanks for the information to clear the download cache!

    I have another question.. How can I host a 3rd party Windows Form Control in a web page? I added the reference onto the web page but it doesn't work.

    Any ideas?

    Thanks in advance.

  • hi


    I use this concept than we use for simpal work ,no any problem ,but when i handle Sqlserver so it generate error

    please help me

  • Hi Ravi, remember that the control runs on the client side, so you can't access the SqlServer on the server side.

  • Its good to know these. but in truly practical terms, its of little value especially since dot net framework has to be installed in client machine.

  • hi guys,

    I have cofusion abt this, that is the code executed on Client Machine ? Or it will be executed on Server Mahine???

    Plz Response

    Thanks in advance... ! :)

    Regards,

    Habib Ahmed Bhutto

  • On the client.

  • That's great... ! :)

    Actually I m trying to execute some programs on Client Machine that are Pre-installed. Those programs are data collector and bar code readers, so I want to load them when my page is loading in browser. I have got my it through a vb script code but it is restricted due to security settings. And Each time user have to allow this or he/she has to set his/her browser settings. So my customer demands that it must be automated.
    Now I m searching for a perfect Solution. If u have any Idea regarding to this, kindly share it... !

    Can we do that we develop a control library and we write a function in that library for executing programs using shell32.dll (Open() function). And finaly we use that function to execute a program on client machine???

    Plzzzz

    I will be thankful to u for this act of kindness :)

    Regards,

    Habib Ahmed Bhutto

  • Solved... !
    Actualy that was due my mistake... ! ;)

  • It would be great if we could use the Windows Form to Interface to an asp.net Website for a user to logon and pass pack his id

  • i created windows user control for retrieving excel sheet data .. i tried to embed it in webpage.. no errors found but my usercontrol is not visible in webpage instead a blank box is appeared please help me...

  • hi.. my windows usercontrol is working fine in webpage. but when i click button which is declared in webform.it will clear contents of textbox of usercontrol... please help me ...

  • I tried to run it on my local PC development web server (Windows XP, VS2008), no positive results: It displays a blank box (sometimes it looks like "image not loeaded box" when the CAS access rights are not set the right way; sometimes it looks like a "blank textarea"). Then I tried to run some samples on Windows 2000 with IIS6, the behaviour was very similar, but it started to work when accessed at the webserver locally via IE6, in the end, after I had set up AllCode Fulltrust access in CAS (Control Panel/Administrative Tools/.NET Framework 2.0 Configuration). HOWEVER, accessing the same website from my local PC with IE7 does not work, showing a "image not loaded" picture. NOTE: You have to run IISRESET command in command prompt after each change to CAS settings! .....ALSO, more complicated WFC was being displayed in a wrong manner, some controls missing from the WFC, bad layout. (That might be a result of mixing .NET 3.5 textboxes into 2.0 WFC, I'll investigate on this.) I hope it helped some of you a little. Write your knowledge please, too.

  • To add to my previous post: I just realized that the CAS access rights must be set on the CLIENT machine, not the server! I have just set up the AllCode option on my client machine and that works. (I hope it will work with the more narrow options.) ...As far as the 3.5 and 2.0 framework mixing, yes that was the case. Your VS2008 project has to have its target framework in its properties set for 2.0 if you are setting the CAS for 2.0 framework. So it actually works.

  • I am passing Dataset as parameter to function and Everything is working fine, but I am getting class doesn't support automation.

  • am using this code.. but it give the error in java scirpt ' like object does not support this property or method'

    can u help.

  • Hi,

    I have a problem in hosting my .Net ActiveX Control written in C# on my webpage. I am able to view the ActiveX Control on the page, but am unable to set the Control's properties using javascript.Setting the properties using the param tag within the object id is working.I have tried all options including, making my localhost as a trusted site and also giving full trust using the AllCode option, but to no avail. I am using .Net framework 2.0, VS2005 and IE7.Can anyone please help?.

  • hi,
    i have a question. Can we use windows form control in an internet web application?


  • if your object id="ctrl" and say it has a function BeginPrint and parameter TemplateString exposed to com

    then in the section of your code
    function BeginPrint()
    {
    //assign to a parameter
    ctrl.TemplateString=document.getElementById('data').innerText;
    //call a function
    ctrl.BeginPrint();
    }

    this assumes the public interface (com) has been set correctly in the .net component


  • Hi Soledad Pano,

    I am looking for some tecnical stuff ofr having the Windows look and feel in Web Forms. Like having Tab which deiplay different panel or so on clicking. You can reply me at gautam.roy@igt.in aslo if required.

    Regards
    Gautam Roy

  • iam trying to do it,
    it workrd when i hosted under IIS6 on win XP
    but when i upgraded to windows 7 it is not working
    and i dont have .net configuration in the control panel

    how can i fix this ?

Comments have been disabled for this content.