Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General ASP.NET >  Embedded Windows User Controls into Internet Explorer
Add to MyHood
   Embedded Windows User Controls into Internet Explorer   [ printer friendly ]
Stats
  Rating: 4.66 out of 5 by 53 users
  Submitted: 01/30/02
Andrew Ma ()

 
Web programming is great and the changes since the old days of plain Html have allowed web programmers to do amazing things with a web browser. Although, even with all the fancy JavaScript and DHTML we have, some things are difficult or impossible to do with a web browser. To get around that, Microsoft introduced ActiveX Controls which allowed Internet Explorer users to download controls which have a richer interface.
Enter the .NET Framework. Now what does .NET provide? The .NET Framework comes with something similar to ActiveX. It allows us to embed windows user controls into Internet Explorer in a similar way as ActiveX. Any user controls written for a windows desktop application can be loaded into any IE browser.

Let's get started.

The User Control
First we'll start by creating a Windows User Control. Here is a sample control you can cut and paste. I won't be explaining the details of a user control since it's out of the scope of this tutorial. This is a simple user control which contains a rich text box and two buttons to add bold and italics to selected text.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace devhood
{
    /// <summary>
    /// Summary description for rtfcontrol.
    /// </summary>
    public class rtfcontrol : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button btnBold;
        private System.Windows.Forms.Button btnItalic;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public rtfcontrol()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if( components != null )
                    components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnItalic = new System.Windows.Forms.Button();
            this.btnBold = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.BackColor = System.Drawing.Color.White;
            this.richTextBox1.Location = new System.Drawing.Point(0, 40);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(488, 432);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.LightGray;
            this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                 this.btnItalic,
                                                                                 this.btnBold});
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(488, 40);
            this.panel1.TabIndex = 1;
            // 
            // btnItalic
            // 
            this.btnItalic.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, 
                    System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnItalic.Location = new System.Drawing.Point(72, 8);
            this.btnItalic.Name = "btnItalic";
            this.btnItalic.Size = new System.Drawing.Size(56, 24);
            this.btnItalic.TabIndex = 1;
            this.btnItalic.Text = "Italic";
            this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
            // 
            // btnBold
            // 
            this.btnBold.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, 
                    System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnBold.Location = new System.Drawing.Point(8, 8);
            this.btnBold.Name = "btnBold";
            this.btnBold.Size = new System.Drawing.Size(56, 24);
            this.btnBold.TabIndex = 0;
            this.btnBold.Text = "Bold";
            this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
            // 
            // rtfcontrol
            // 
            this.BackColor = System.Drawing.Color.LightGray;
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.panel1,
                                                                          this.richTextBox1});
            this.Name = "rtfcontrol";
            this.Size = new System.Drawing.Size(488, 472);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void btnBold_Click(object sender, System.EventArgs e)
        {
            if (this.richTextBox1.SelectionFont.Bold)
                this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Regular);
            else
                this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Bold);
        }

        private void btnItalic_Click(object sender, System.EventArgs e)
        {
            if (this.richTextBox1.SelectionFont.Italic)
                this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Regular);
            else
                this.richTextBox1.SelectionFont = new Font(this.richTextBox1.SelectionFont, FontStyle.Italic);
        }
    }
}

One major thing to note about this user control is that it does not use any system colours. The colour of the form, panel and the textbox are all defined in this control. This is an important thing to note because if anything is set to system colours, the control will not load in the browser.

Now that we have the User Control created, we have to compile it to a DLL. Use this syntax to create a DLL:
csc /t:library rtfcontrol.cs

After compilation is done, place the DLL in a location on your web server. Note, do not place the DLL in the /bin directory since this DLL will be downloaded by the IE client. Most /bin directories are set up with permissions that do not allow files to be downloaded from the /bin directory.

The HTML page
Next we have to create the HTML page that the users will visit. Simple enough, all we have to add is an <object> tag at the location where we want the user control to appear. Here is a sample HTML page:

<html>
  <body>
   <center>
     <object id="rtfcontrol" height="472" width="488"
         classid="http://127.0.0.1/RtfControl.dll#devhood.rtfcontrol">
     </object>
   </center>
  </body>
</html>

Notice the classid in the <object> tag. The classid has two parts to it. First we have the URL to the DLL file (again, make sure the DLL is not in the /bin directory). It is then followed by a # sign and ends with the fully qualified name of the control (namespace + control name).

There you have it. When you load this HTML page in a IE web browser, the control will load and you're ready to go.

Closing Remarks
Why is this useful? Web programming is sufficient for most tasks, but some require richer UI. Embedded user controls can be used for a richer text editing environment, uploading a lot of files via drag-drop (a-la or style), a chat client, etc. The possibilities are endless.

Some of you are probably wondering about the security of this since the DLL is actually downloaded to the client's local hard drive. When a control is run via an IE client, it has very little permissions to do things to your computer.
Here are two articles about .NET and security. I don't think it deals specifically with Controls embedded in IE, but it will give you a good idea of the security mechanisms in place.


  • Links
    Devhood ASP.NET Message Forum - Feel free to ask questions to me or other Devhood members.
    Devhood C# Message Forum - Feel free to ask questions to me or other Devhood members.
    http://www.devhood.com - Can't forget to mention this great resource. :-)
    - Good portal to other ASP.NET resources.
    - Free hosting
    - Gotdotnet.com's quickstart tutorial for embedded controls (based on beta2 code).

    Tips
  • Do not use system colours. If you do, the control will not load.
  • Don't place the DLL in the /bin directory since the permissions on that directory do not allow browsers to download the code from it.
  • Embedded User Controls require a browser with IE 5.5 and the .NET Runtime distributables must be installed on the client.
  • If you compile a new version, refreshing the browser doesn't always get the new version of the DLL. Close the browser and then visit the page again.

    Copyright © 2002 Andrew Ma.

  • Return to Browsing Tutorials

    Email this Tutorial to a Friend

    Rate this Content:  
    low quality  1 2 3 4 5  high quality

    Reader's Comments Post a Comment
     
    I would also like to add that assemblies deployed this way cannot install into or access the GAC (global assembly cache). This also helps developers by aleviating the need to write package descriptions such as INF files for distribution in a CAB file to install like ActiveX control. No registry access is need for assemblies (which is nice!) and IE cleans up the downloaded assembly when it's done using it. It's a pretty neat system, really.
    -- Heath Stewart, January 30, 2002
     
    Great tutorial, I hope to explore its possibilities in the future.
    -- Josh Vetere, January 31, 2002
     
    Great work!!! This actually helped me :)
    -- Matt Trepina, February 01, 2002
     
    Great Work, that will be something fun to implement into my browser. Thanks!
    -- Steve Hulin, February 01, 2002
     
    I just took a look at this...it looks good...keep posting stuff like this Andrew
    -- Emily Rimas, February 02, 2002
     
    Good stuff, Andrew. Keep them coming.
    -- Peter Huene, February 02, 2002
     
    Awesome stuff man... keep on coming out with these awesome tutorials
    -- Gabriel Chan, February 07, 2002
     
    Keep up the good work! Brought up interesting ideas about what you can do with .NET.
    -- Lucky Rumengan, February 07, 2002
     
    Great job
    -- manuel torres, February 14, 2002
     
    Very informative. Keep up great work.
    -- averia ^^, February 18, 2002
     
    Great work. Keep the tutorials coming.
    -- Reid Jonasson, March 01, 2002
     
    Very helpful!
    -- Kuniaki Tran, March 03, 2002
     
    Great! We heavily use custom ActiveX controls and want to move to .Net for such functionality. This is just what we need. Thanks!
    -- Charles Porter, March 13, 2002
     
    I am impressed once again andrew
    -- Sean Fitzgerald, March 14, 2002
     
    Good tutorial.
    -- Brian Simoneau, March 18, 2002
     
    ...Just wondering why my browser won't display the control? I have IE 6, but I would imagine that it shouldn't matter. Any ideas?
    -- Debauch Propaganda, March 29, 2002
     
    great tutorial. very helpful.
    -- Evan Deutsch, March 31, 2002
     
    Wow. Thanks tons. This is cool.
    -- Karl Mortensen, April 02, 2002
     
    excellent tutorial. I didn't realize that the client requires .Net runtime the first time i tested it. Good job andrew!
    -- asad ladha, May 08, 2002
     
    Wow this is a really nice tool. I hope that people will all get the .NET redistributable installed on their computer so that they can see the embedded controls.. hopefully maybe the next Windows version has it built in...
    -- Victor Vuong, June 04, 2002
     
    Also... for those of you who want another tutorial on this topic, please look at Heath Stewart's:
    http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=388
    -- Andrew Ma, July 10, 2002
     
    Nice tutorial, can't wait for .NET to really catch on
    -- Dan Cramer, October 17, 2002
     
    Does anyone have any experience serving a control like this from a non-windows non-IIS server? I like the possibilities that this technology brings, but I would like to be able to serve the files from an apache/linux platform. Since the execution is all done on the client side I don't see why it wouldn't work but I haven't had any luck so far. Has anyone else tried this with different experience?
    -- Eric Harding, April 30, 2003
     
    I try to user apache/tomcat as a web server but in this case no assembly is downloaded.
    I think that link tag is ok only for IIS web server, so in apache/tomcat my custom control does not find dependency because explorer does not find the configuration file.

    I will continue trying

    Archi


    -- Archimede Pitagorico, October 08, 2003
     
    You can use also apache/tomcat!

    The only, and most important, thing to do is to add a mime mapping for the "dll" extensiont to "application/x-msdownload"

    Tomcat example: in <TOMCATHOME>/conf/web.xml add:

    <mime-mapping>
    <extension>dll</extension>
    <mime-type>application/x-msdownload</mime-type>
    </mime-mapping>
    -- Archimede Pitagorico, October 17, 2003
     
    Copyright © 2001 DevHood® All Rights Reserved