Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Including a winforms user control in an asp.net web page

Despite all the nice UI and usability offerings of AJAX nothing beats a real windows control. These days you can do terrific things on a web form but when it comes to things like drag and drop between the nodes of different treeviews a real windows control is the way the go. In an old post I already demonstrated how to include something like tablet pc functionality in a web page. Since then I’ve been building another web-hosted user control, let me share some of the new things I learned.

There is a good tutorial on the basics of building a control over here on MSDN. For my example I have built myUserControl which is in the assembly myLibrary. Including the user control on the client is a matter of client side html. Displaying the control in the page is done in an html object.

When rendering the page IE expects the assembly MyLibrary.dll alongside the aspx on the server. It will download the assembly and extracts the class MyNameSpace.MyUserControl. From this class it creates the user control which is displayed in the space reserved for the object.

For this to work well the following requirements are put on the client

  • Use Internet Explorer. This does not work in any of the other browsers.

  • Have the .NET framework installed on the client machine.

  • The site should be included in the trusted sites list of the browser.

  • The browser should be allowed to download files from the (trusted) site.

Note that when your user control uses other custom assemblies, not part of the installed framework, the control will not display. The assembly itself is downloaded, the dependencies are not. Also note the security settings. It is easy to drown in the IE 7 security settings. Allowing a file download is something which you explicitly have to do by hand. When all these requirements are not met the object will be an empty space on the page, filled with the default “image missing” icon.

The location of the assembly is read by the browser as an http address. Just the filename assumes the assembly alongside the aspx on the server. Setting a different location is a matter of setting an URL

To make this configuration configurable requires a little twist, as this markup is static html. The way to manipulate static html server-side is using an asp.net literal control.

<asp:Literal ID=”Literal1″ runat=”server”></asp:Literal>

The contents of the literal is manipulated from serverside code behind.

This will result in exactly the same html and, provided the assembly is found at the url (and all other (security) settings are in place), the control will display in the browser.

The usercontrol can interact with the other parts of the page. All its public members are accessible from script. All events as well, the MSDN article dives deeper into that. Beside manipulating the control from script it’s public properties can also be set on initialization.

This way public properties are initialized. It works well over different types, you can even pass enumeration members. The nice thing about this construction is that it also can be done from the server side code behind while constructing the html for the literal control.

Note the enumeration member being passed.

It is possible to invoke public methods from script. But here we invoke the initialization method by setting a property.

Having set all properties a configurable method is fired. There is no need for any script yet. But there is one thing where ajax can play a very important and handy role. On every roundtrip the usercontrol is reinitialized and any changes in its state are gone. Putting all other controls which cause a roundtrip on an ajax updatepanel are the way to keep this under control. The partial updates will now preserve the state of your user control.

That’s all there is to it. First build you user control, of course best in a TDD style, and then check what it looks like in IE. It will look good but be prepared for a slightly different appearance. Have fun !

This entry was posted in ASP.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Patrick Heneghan

    Srinath N – I had the same problem – it worked fine when I ran it through Visual Studio, but as soon as I deployed to IIS7 it stopped working.

    I figured out the problem. When I accessed my page, the browser would try to download the WinForm DLL from the server. However, IIS7 default behaviour is to *run* a DLL when it is requested. What we want instead is for IIS7 to serve up the file for download.

    So the solution is to open IIS7, go to the folder in your site where the DLL is located, open Handler Mappings, choose Edit Feature Permissions from the Actions menu, and deselect Execute. This will stop IIS7 from trying to run the DLL, and allow the client to download it instead.

    I’ve also read that the folder where you store the DLL must also be a virtual folder.

    I hope this helps someone, but be prepared for a host of other problems… :)

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    What you mean by that ?
    It’s a winform, what you want to do in there is compeletely up to you. A forum ? Why not

    But: the techniques described here is a dead end. Go for Silverlight !

  • http://www.r4-ds.es r4 nds

    does this embedded winforms controls help in creating forums? Thats what I got from it…..Please provide information over it. Provide links to related topics if possible.

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    I don’t know.. IMHO you’re creating stuff which will find it’s way to the daily WTF sooner or later…

    All thes controls are just wrappers around IE. What works with one version for the moment doesn’t work in another the next day.

    I honestly think you should reconsider your project at the basics.

  • carbophoto

    It is possible to create a web browser control within a windows control library embedded in IE as an object, but only by using the older AxWebBrowser control (or this: http://www.codeproject.com/KB/miscctrl/WebBrowserEx.aspx)

    The newer web browser controls that ship with the latest .net frameworks will throw exceptions when embedded in WCL and IE, and will not work.

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    You want to embed a windows containing a webbrowser in a web-browser 😕 WTF 😕

    Perhaps possible but it will no doubt have a lot of security issues.

    Are you following the right approach ?
    What about
    – Javascript JQuery
    – Silverlight

  • enebelle

    not able to embed on web , a windows control having a web browser control on it.

  • enebelle

    i have a windows control with web browser control displayed on it. I wnt to embed this windows control on web.

    It tried but I am not able to embed indows control with web browser on it.

    When I tried to display a button on my windws control and then embed it on a web page I was able to do it.

    Is there a problem in using we browser control in embeding objects on web.

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    Security settings ?

    Instead of fixing that use Silverlight. See my previous comment.

  • Amit Gupta

    I am trying the example posted. But I need to access the Webservice from my windows user control. When I am trying to call Webservice from my WinControl in ASPX page I am getting error

    ” System.Security.SecurityException: Request for the permission of type ‘System.Net.WebPermission, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed”

    Can you please help me how to get around this error and be able to call my webservice in thie control.


  • http://petersgekko.codebetter.com pvanooijen

    I’m sorry but I don’t have any hints for these (undoubtly) security problems.

    I do have experience with running a silverlight control in a browser, also on a server. Which just works. IMHO Silverlight is the way to go. It’s not just the possibility to bring winforms things to a browser it also takes care of a lot of hosting and integration issues.

    I would advise you to migrate these kind of projects to Silverlight.(ASAP)

  • Jignesh

    Hi, Srinath N

    I am having same problem but my OS is server 2000

  • Srinath N


    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.


    Srinath N

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    :?? Could you be more precise ?

  • santhosh

    If foreach () construct not supported when we use windows control inside a webpage

    I am developing one application where u hace to embed a windows control in side a web page .. but inside the control i am usingh foreach loop but control is not going inside foreach loop at all..

    please guide me to resolve this..

    with regards,

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    That’s about the most common, least informative error message you can get in .NET…..

    You have to be a lot more specific about your problem regarding the subject of this blogpost

  • sapacool

    Hi all,
    Problem with Winform usercontrol.Please help me.

    Object reference not set to an instance of an object.

    at Microsoft.VisualStudio.Design.Serialization.CodeDom.MergedCodeDomParser.System.CodeDom.Compiler.ICodeParser.Parse(TextReader stream)
    at System.CodeDom.Compiler.CodeDomProvider.Parse(TextReader codeStream)
    at Microsoft.VisualStudio.Shell.Design.Serialization.CodeDom.CodeDomDocDataAdapter.get_CompileUnit()
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    This is all that’s neede :)

  • Sapacool

    Hi Peter,
    Can you send me source code of this article?My email ia :
    Thank you in advance.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    hi Deepak,

    I know about the CASP thing. But that’s not always the full security puzzle. In our case security settings required any downloaded executable (.net or no .net) to be signed with one very specific certificate. It took an sa to make that clear.

    My doubt is about any custom assemblies referenced by the usercontrol. AFAIK these will never be found unless they are in the local gac

  • http://deepak.davesar@gmail.com Deepak Davesar

    Hi Peter,

    as i said in my previous comment, you need to play with CASP settings in order to do many things when you are hosting winform control in IE. If you delve deeper into how CASP settings affect .NET assemblies, i am sure you will find this answer


  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    @ Deepak,

    are you sure about finding multiple assemblies ? In my experience that did not work. The path to the assembly was something like http://Myserver/MyApp/MySsembly.dll. Perhaps it only works when you use a non http path ? This requires a test.

  • http://deepak.davesar@gmail.com Deepak Davesar

    Will, if you want to clear cache on client for the winform user control then goto visual studio command prompt and write:
    gacutil /cdl

  • http://deepak.davesar@gmail.com deepak davesar

    Hi All,
    I had recently done a project of hosting winform user control in IE using .NET Framework 2.0, the major consideration while developing is CASP (Code Access Security Policy). By default any assemblies that come from internet zone are given very restrictive access to protected resources, so many functions like registry settings, IO, ado.net (partial), calling web services or other callbacks on domains other than the control is hosted are disabled and gives security exceptions. Regarding multiple assembly reference, if you put your referenced assemblies on the same location where the user control is hosted, it will work fine.

  • Will

    I am having major caching issues and I am deving off II7 I have cleared cache countless times in my browswer and have reset IIS I dont know how many times but the page itself. I also cleared out ASP.NET temp cache in my ASP.NET Temporary Files folder. Seems like no matter what I do the page is not updating with any changes I make to it

  • http://codebetter.com/blogs/peter.van.ooijen pvanooijen

    It’s a security thing. I’ve seen something like this where we had to sign the usercontrol assembly with a specific certificate. Only asssemblies signed with that specific trusted certificate were accepted by the SSL.
    Consult your IT staff for specific details

  • dheeraj

    I am facing a problem with asp.net and usercontrol.
    I am using web-services with usercontrols behind the SSL.

    On some machines the user-control does not get instantiate while working on SSL. If no ssl then it works fine.
    I am not usiing any proxy settings with my web-service and usercontrol. Do i need to use.

    Please suggest.


  • http://vladimirb@leadcom-is.com vladimir

    I have same problem with multiple assemblies.
    may be exist solution?

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Have you tried ILmerge yet ?

  • Alan

    I am referencing about 4 assemblies in my winform User control Assembly, the control can not be displayed correctly, Do you have any idea about this? thanks a lot

  • http://codebetter.com/blogs/peter.van.ooijen pvanooijen

    I have no personal experience with ILmerge but it looks like it’s worth a try. It will not work when your code uses unmanaged dll’s, but in such a scenario that will not be your only (security) problem.

    Reflection is not that hard to use, but I would use wrapper classes in that scenario. I think the hardest part of this scenario is downloading the depencies and being able to find them from your code.

    Yes it is an Achilles heel.

  • Eric

    I used an embedded winforms control like this to add scanning functionality to a webpage I built. The achilles heel of this technique is definitely its inability to handle dependent assemblies. Do you think that maybe a tool like ILMerge would allow the dependent assemblies to be bundled with the winforms control? The only other technique I thought of was downloading the dependent assemblies using the WebClient class and then using reflection to instantiate them, but that sounds like a pain. Solving the dependent assembly issue would be huge.