Embedding .NET code and Windows Forms controls into ASPX pages
Sometimes you can't trust the users of your web application to be savvy enough to navigate away from your application. Most likely, what happens is that a user must do a third party process in order to pass data to your web application, such as scanning in an image before uploading it. Having the user need to minimize the browser window, then open a scanning application, then save the file to a remember-able place, then restore the browser window and 'Browse' to that location can be very overwhelming to people not necessarily technical.
Eliminating the steps above and encompassing as much of the functionality within the browser can be very convienient. As a primer, see this article:
http://msdn.microsoft.com/msdnmag/issues/02/06/rich/default.aspxThere are a few interesting hiccups I ran into trying to get this to work, and I wanted to share these with you:
- Getting the control to load can be difficult. To make sure the control itself is loading, add a simple button on the user control. If the control loads with the 'image' type square in the top left, and you don't see your button, check the path to the DLL. make sure the DLL is NOT in the Bin folder (IIS will not serve files from the Bin folder).
- If you get an 'Object does not support this property or method' error client side in your javascript when trying to access public methods of your class, be sure you have ComVisible set to true above your definition (see below). This most often happens with .NET 2.0
The ComVisible attribute was the hardest nut to crack. Above your class definition, do as such:
1 2 | [ComVisible(true)] public class MyClass : UserControl{...} |
This will get rid of the accesibility problems for javascript and the pesky 'object does not support' messages.
What did I use this all for? Well, we support a large base of users who are not technically savvy. As part of their compliance operations they need to have a document management solution that keeps a digital copy of their documents and letters. Our web application has a form to fill out document information, but instead of a 'browse' button for the file itself, we have a 'scan' button in the Windows forms control inside the webpage. When the user clicks that button, the Twain interface takes over, letting them crop the document. When they finish the scanning, Twain passes the image back to our control, which uploads it to the Web Server along with the other form information.