CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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 !


Published Jun 18 2007, 02:57 PM by pvanooijen
Filed under:

Comments

pvanooijen said:

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.

# June 18, 2007 1:47 PM

Alan said:

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

# June 27, 2007 2:38 PM

pvanooijen said:

Have you tried ILmerge yet ?

# June 28, 2007 7:23 AM

Christopher Steen said:

Link Listing - June 18, 2007

# July 20, 2007 1:53 AM

vladimir said:

I have same problem with multiple assemblies.

may be exist solution?

# July 25, 2007 5:02 PM

dheeraj said:

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)

# August 1, 2007 3:04 AM

pvanooijen said:

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

# August 17, 2007 9:43 AM

Will said:

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

# August 17, 2007 3:37 PM

deepak davesar said:

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

# August 19, 2007 10:03 PM

Deepak Davesar said:

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

gacutil /cdl

# August 19, 2007 10:06 PM

pvanooijen said:

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

# August 20, 2007 5:42 AM

Deepak Davesar said:

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

# August 20, 2007 10:11 PM

pvanooijen said:

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

# August 21, 2007 5:51 AM

Sapacool said:

Hi Peter,

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

sapacool@yahoo.com.

Thank you in advance.

Sapa,

# September 15, 2007 11:45 AM

pvanooijen said:

This is all that's neede :)

# September 25, 2007 3:57 PM

sapacool said:

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)

# September 30, 2007 10:36 AM

pvanooijen said:

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

# September 30, 2007 3:32 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News