Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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 !


Posted 06-18-2007 2:57 PM by pvanooijen
Filed under:

[Advertisement]

Comments

Eric wrote re: Including a winforms user control in an asp.net web page
on 06-18-2007 11:35 AM

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.

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 06-18-2007 1:47 PM

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.

Alan wrote re: Including a winforms user control in an asp.net web page
on 06-27-2007 2:38 PM

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

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 06-28-2007 7:23 AM

Have you tried ILmerge yet ?

Christopher Steen wrote Link Listing - June 18, 2007
on 07-20-2007 1:53 AM

Link Listing - June 18, 2007

vladimir wrote re: Including a winforms user control in an asp.net web page
on 07-25-2007 5:02 PM

I have same problem with multiple assemblies.

may be exist solution?

dheeraj wrote re: Including a winforms user control in an asp.net web page
on 08-01-2007 3:04 AM

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.

Thanks

Dheeraj

(dheeraj_jojo@hotmail.com)

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 08-17-2007 9:43 AM

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

Will wrote re: Including a winforms user control in an asp.net web page
on 08-17-2007 3:37 PM

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

deepak davesar wrote re: Including a winforms user control in an asp.net web page
on 08-19-2007 10:03 PM

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.

Deepak

Deepak Davesar wrote re: Including a winforms user control in an asp.net web page
on 08-19-2007 10:06 PM

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

gacutil /cdl

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 08-20-2007 5:42 AM

@ Deepak,

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

Deepak Davesar wrote re: Including a winforms user control in an asp.net web page
on 08-20-2007 10:11 PM

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

Deepak

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 08-21-2007 5:51 AM

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

Sapacool wrote re: Including a winforms user control in an asp.net web page
on 09-15-2007 11:45 AM

Hi Peter,

Can you send me  source code of this article?My email ia :

sapacool@yahoo.com.

Thank you in advance.

Sapa,

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 09-25-2007 3:57 PM

This is all that's neede :)

sapacool wrote re: Including a winforms user control in an asp.net web page
on 09-30-2007 10:36 AM

Hi all,

Problem with Winform usercontrol.Please help me.

Object reference not set to an instance of an object.

Hide    

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)

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 09-30-2007 3:32 PM

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

LanceZhang wrote 在网页中使用Winform控件,并调整其大小
on 08-06-2008 11:40 PM
santhosh wrote re: Including a winforms user control in an asp.net web page
on 09-13-2008 1:55 AM

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,

santhosh

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 09-14-2008 4:10 PM

:?? Could you be more precise ?

Srinath N wrote re: Including a winforms user control in an asp.net web page
on 10-01-2008 3:26 PM

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

Jignesh wrote re: Including a winforms user control in an asp.net web page
on 11-13-2008 6:53 AM

Hi, Srinath N

I am having same problem but my OS is server 2000

pvanooijen wrote re: Including a winforms user control in an asp.net web page
on 11-13-2008 4:38 PM

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)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?