Soledad Pano's Blog

Just Technical Stuff

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

 

Posted: Sep 19 2007, 03:56 PM by spano | with 49 comment(s)
Filed under: ,

Comments

spano said:

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 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.

# September 27, 2007 12:21 PM

spano said:

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

# October 7, 2007 11:06 PM

Juan Francisco said:

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

Please do you have some solution

Thanks

# October 19, 2007 3:48 PM

Mau said:

Thanks Spano, that worked beautifully. Great work.

# October 20, 2007 3:33 AM

Newbie said:

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

TIA,

John

# October 20, 2007 10:13 AM

Aswin Dwarakanath said:

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.

# October 24, 2007 2:10 PM

spano said:

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.

# December 6, 2007 3:35 PM

james morris said:

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

# February 7, 2008 9:20 AM

Smith said:

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..

# February 22, 2008 1:40 PM

cnc said:

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

# May 5, 2008 8:04 PM

Law said:

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.

# May 12, 2008 7:34 PM

caru said:

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) ?

# July 28, 2008 3:37 AM

Dantevios Exsavior said:

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.

# July 29, 2008 2:22 PM

#Monkey said:

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"...

# July 30, 2008 2:36 AM

Harry said:

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

# September 2, 2008 1:07 AM

som said:

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

# September 5, 2008 6:15 AM

Srinath N said:

Hi,

I've created simple user contol using .NET 2.0 and included in ASP.NET 2.0 page it works fine while i'am accessing the web page from localhost and even from other client systems making my system as server.

I'am using Windows XP and IIS 5.1.

Now the problem is I hosted the control in a testing server with is Windows 2003 server and IIS 6.0. The control is not displaying. Contorl is showing up as disabled multiline text box.

I've added url into all code, full trust permission in .net configuration tool and I've also tried adding the servers url in trusted site in IE. Still does't work. It even doesn't work if I access webpage from server with localhost url.

can some body help me out where i'am going wrong.

thanks,

Srinath N

# October 1, 2008 3:52 PM

Jerry Z said:

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

# October 16, 2008 6:11 PM

Mike P said:

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.

# October 22, 2008 6:59 AM

Eric Ouellet said:

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

# October 27, 2008 10:51 AM

nilaangel78 said:

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.

# December 29, 2008 2:24 AM

arruix2001 said:

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

<object classid="http:WindowsFormsControlLibrary1.dll#WindowsFormsControlLibrary1.UserControl1" height="900px" width="800px">            

</object>

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

# January 16, 2009 11:41 AM

Manoj said:

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.

# January 29, 2009 10:48 PM

blurboy said:

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.

# March 23, 2009 6:34 AM

Ravi Ranjan said:

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

# May 2, 2009 3:52 AM

spano said:

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

# May 2, 2009 10:37 AM

web hosting nigeria said:

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.

# May 19, 2009 12:45 PM

Habib Ahmed Bhutto said:

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

# May 27, 2009 8:15 AM

spano said:

On the client.

# May 27, 2009 9:00 AM

Habib Ahmed Bhutto said:

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

# May 28, 2009 1:33 AM

Habib Ahmed Bhutto said:

Solved... !

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

# May 28, 2009 5:24 AM

rshevlin said:

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

# July 3, 2009 8:16 PM

Sathya said:

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...

# July 30, 2009 6:47 AM

sathya said:

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 ...

# August 1, 2009 5:52 AM

Roman said:

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.

# August 5, 2009 6:34 AM

Roman said:

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.

# August 5, 2009 8:34 AM

Shankar said:

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

# August 8, 2009 11:10 AM

Viren said:

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

can u help.

# September 2, 2009 8:12 AM

Anu said:

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?.

# September 24, 2009 7:21 AM

imran said:

hi,

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

# October 31, 2009 1:48 AM

richard said:

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

then in the <head> 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

# November 21, 2009 2:12 PM

Gautam Roy said:

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

# January 8, 2010 12:41 AM

ScriptiZer said:

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 ?

# March 19, 2010 12:34 AM