Welcome to MSDN Blogs Sign in | Join | Help

Writing a simple managed control under IE

Most web developers are aware of various "controls" and "applets" that can be "hosted" by "Internet Explorer" (hmm, not sure why I quoted that last thing, I guess I was on a "roll".)  This code is compiled code that runs within the IE process and draws itself within the boundaries of the document.  The control even has access to the outside container.  This is useful for doing all sorts of nifty stuff that might be hard to do using pure script or HTML, such as drawing graphs, animation, creating an interactive grid, creating a chat room, etc.  Most of these controls are written in Java or COM (ActiveX).  Java requires a special runtime that you have to download, and ActiveX requires your users to be running one of the many ActiveX compatible web browsers (There's tons!  IE3..  Umm, IE4..  IE5..  Oh yea, IE5.5..  IE6, just to name a few)..  However, what if you really want to keep users away from your site and write a control that requires them to download a special runtime ANNNNND only be able to use one browser.  Well, then my friend you definitely need to write your own managed controls.

 Only a hand full of web developers are aware that IE (recent versions, not sure which ones) can run managed code.  Even less than a hand full (maybe a paw of a very small cat?) can actually tell you how to do this.  Provided your users have the .NET framework installed (who doesn't?) there's a special syntax of the <object> tag that can instantiate a managed Winforms control.  I found information on the Internet a little sparse on this subject so I decided to write a post describing exactly how to do this.  Not that you should, as I'm sure I'll get 32,768 comments telling me that my apps are crap, proprietary and how that mentality somehow mirrors the exact attitude of Microsoft as a whole.  Well, let me tell you something.  This blog is dedicated to building web based applications, not web pages.  Applications run on a certain platform.  That platform might be Windows, that platform might be Mac, that platform might be the Java runtime.  It might be a Commodore PET, a cell phone, a Zune, one of those old TI-85 graphing calculators, whatever.  This platform can also be Internet Explorer.  In fact, one of the reasons IE became so much more popular (especially within corporate networks) over say, Netscape, is it's an actual web application platform - not just a browser.  Authors of applications can write applications for whatever platform they choose, based on its requirements as well as the users of said application.  I might write an IE specific application for setting up direct deposit because my IT department has rolled out IE to everyone in my company.  It's cost effective to target a single platform.  There exists a certain spectrum; on one side you can scale to many different types of users but your development/test costs go way up and/or your feature set goes way down.  On the other side, you can write a program once for a certain platform and  target a very specific set of users.  It's up to developers to figure out the best spot in that spectrum for their specific application.  So enough of my rant, let's get back to the subject.

First, let's create a managed DLL with a single class in it.  Since I don't feel like writing a full fledged control, all this thing will do is pop up a message box.  Or maybe I don't actually know how to program and this whole blog's a lie.  However, you'll get the point - you can add more controls to the child controls collection, override all sorts of events, etc.


[assembly:System.Security.
AllowPartiallyTrustedCallersAttribute]
namespace
HostedControlTest {
   public class TestControl : Control
   {
      public TestControl()
      {
         MessageBox.Show(
"Hello World!");
      }
   }
}

Compile the DLL, upload it to your web server in a location that clients have access to reach.  You can test this by typing in the URL for the DLL and make sure the IE "Save" dialog comes up and you can download your own DLL.  You might have certain web server security restrictions (especially if it's a Sharepoint server) that prevent users from downloading DLL files.  It doesn't matter what web server you're using, it doesn't matter if the web server knows anything about .NET.  It's simply a binary file sitting up on a server somewhere and it's downloaded exactly how, say, a JPG file is downloaded.  Also, I'm pretty sure you can even use another extension other than DLL.

Once you have this working, create a new web page.  On the page, place an object tag that looks something like:

<OBJECT classid="HostedControlTest.dll#HostedControlTest.TestControl">

You can replace HostedControlTest.dll with any valid relative path to your DLL.  After the pound sign (or, octothorpe as I like to say) is the class within the DLL to load.  Note I fully qualify the path with the namespace as well.

Load up your page through your web browser and the control should run and you should get a popup on your screen.  No need to sign anything, no need to click "Yes" to any security warnings, etc.  In fact, all security is taken care of for you by Code Access Security and is enforced by the runtime's security policy on your machine.  Your control can request permissions (such as local file access) and it's up to the client's machine what it trusts the control to do.  If a control all of a sudden tries to do something evil, the runtime will throw an exception.

That's all there is to it.  Don't ask me any questions because this is all I know!  Seriously.  Actually, feel free to ask me questions and I'm sure I can find out the answers for you.  Thanks!

Mike (Web Dev Guy)

Published Tuesday, November 28, 2006 2:12 AM by mikechr

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Writing a simple managed control under IE

Monday, December 11, 2006 9:06 AM by Tian

Hi Mike,

Thanks for this post. You now know roughly as much as I do about this topic (unless you're hiding the full extent of your expertise...;-)

Anyway, I'm trying to do something more than just pop up a howzit dialog, to wit, to print to the default printer on the client's machine. Daring stuff, I know, presumptuous even. Now this is meant to run on an intranet (I want to print labels from my asp.net app) so it's quite a controlled environment, and I can ensure that the server/url of my app is placed in the "Local Intranet" zone on each user's browser.

However, even while running on my own development machine and trying to print using the PrintDocument class I get a security exception. A bit of digging reveals that I should be doing something called "Safe Printing" ... any clues/tips/references?

Cheers,

Tian

# re: Writing a simple managed control under IE

Monday, December 11, 2006 6:47 PM by Mike

Yea try putting something like this at the top of your class, or in the assembly.cs:

[assembly:System.Drawing.Printing.PrintingPermission(SecurityAction.RequestMinimum)]

Note that the client machine would have to grant this permission to your control.  I'm far from a code access security expert, but plenty of info on MSDN.

Mike - Web Dev Guy

# re: Writing a simple managed control under IE

Monday, December 11, 2006 6:56 PM by Mike

Sorry, pasted in the wrong code..  Try something like this:

[assembly:System.Drawing.Printing.PrintingPermission(SecurityAction.RequestMinimum, Level = System.Drawing.Printing.PrintingPermissionLevel.SafePrinting)]

# re: Writing a simple managed control under IE

Wednesday, December 27, 2006 11:56 AM by Michael Feingold

Does it have to be a relative path? To the dll I mean.

I tried to run it with an absolute path and got some really confusing results: sometimes it works just fine: events fire, and I can call control's methods from JScript, but after certain POSTs it breaks - all atempts to call control's methods throw exceptions. But events still fire and the control itself is fully functional. Any thoughts?

# re: Writing a simple managed control under IE

Saturday, March 24, 2007 10:58 AM by Roy K.

Hey Mike,

This post is a great start and with some additional investigation I've discovered how to expose methods, properties and events so that JScript in the web page can interact with the control. However one thing that has me stumped is how I can get access to the IE object model from within the hosted, managed control. Specifically, how can I get the URL of the page that is hosting the control (i.e. think "site locking".)

Thanks,

- Roy

# re: Writing a simple managed control under IE

Monday, March 26, 2007 7:06 PM by Mike

Not quite sure on that one, lemme check into it a bit and see if I can find anyone who knows the answer..

Mike

# calculators mortgage amortization

Wednesday, March 28, 2007 2:21 PM by ctl00$_$ctl00$_$ctl01$_$form$_$tbname

mortgage piti calculators <a href= http://urlin.it/cbd7 >calculators mortgage amortization</a> [url=http://urlin.it/cbd7]calculators mortgage amortization[/url]

# re: Writing a simple managed control under IE

Thursday, July 12, 2007 5:24 PM by Amber

Hi Mike,

I am trying to do the same type of project that Tian above mentioned... I'm trying to create my own print functionality using managed code, on my company's intranet so that I can control margin size and the header/footer information, but I'm not really sure where to start. Is there anywhere on the internet that you can point me to that'll help get me started? I've been researching this problem for quite sometime and am pretty just can't figure out where to start. Any help would be fantastic. Thank you!

Amber

# re: Writing a simple managed control under IE

Friday, July 13, 2007 12:13 PM by Piyush

Can we use this for Internet users too?

I am targetting all those Vista IE7 users. I think on which managed code is there by default. Is this possible?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker