Click here to Skip to main content
15,898,035 members
Articles / Programming Languages / C#
Article

Simple way to expose a .NET WinForm control as an ActiveX control in any HTML page

Rate me:
Please Sign up or sign in to vote.
4.43/5 (32 votes)
7 Sep 2003CPOL3 min read 597K   10.2K   100   138
This article shows how to take a .NET (C#) WinForm control and use it as an ActiveX control in any HTML web page.

Sample Image - htmlwincontrol.jpg

Introduction

This article intends to show a very simple way to add any Windows Form control to any HTML page, whether it is an ASP.NET Web Form or any other simple HTML page. It is useful because sometimes we need extra functionality in our web pages, which simple HTML controls do not always provide. It is being used for a long time now (ActiveX) and this is a way to do the same using .NET. You could think of it as a C# applet. This approach will need that the .NET runtime is installed on the client machines, which may be an inconvenience compared to real ActiveX, but as with Java, why not download the .NET runtime?

Using the code

First of all we need (of course) our control, so we need to create a UserControl and build it in a .NET assembly. To avoid any complexity I used the DateTimePicker, extended it, and added a Date property that returns the short date format of the DateTime property of the control. Why this one? No reason, but there has been always the need for good date controls, and this one is very good.

Here's some of the code:

C#
....
namespace DeAcero.Web.Controls.DateTime
{
    public class DateTimePicker : System.Windows.Forms.DateTimePicker
    {
....
        public string Date
        {
            get {return Value.ToShortDateString();}
        }

        public DateTimePicker() : base()
        {
            InitializeComponent();
        }
....
        private void InitializeComponent()
        {
            this.Format = System.Windows.Forms.DateTimePickerFormat.Short;
        }
....

Once we build (and prove) our control, we need to add it in the web page. To add an ActiveX control to an HTML page, we need to use the tag:

HTML
<object id=myId classid=myclassid>

Where the id attribute is the identifier of the control. To access the control from a JavaScript block (for example), this is the name we have to use. The classid attribute contains information of the file and class that contains the control. There's no need for the codebase attribute since the .NET runtime and Explorer do the download process automatically.

Here's an example:

HTML
<object id="DateTimePicker" height="31" width="177" 
  classid="bin/DeAcero.Web.Controls.DateTime.dll#DeAcero.
                     Web.Controls.DateTime.DateTimePicker" VIEWASTEXT>
</object>

Now let's have a look at the classid attribute. The bin/ part is the relative path of the DLL file containing our control, in this case DeAcero.Web.Controls.DateTime.dll. So in the example, the DLL is located inside the bin directory which is in the same directory as the HTML page containing the <object> tag.

The next part, DeAcero.Web.Controls.DateTime.dll, is just the name of the file (assembly) containing the control. The last part (notice the # separator) DeAcero.Web.Controls.DateTime.DateTimePicker is the full name of the class that implements the control, including the namespace.

That's all the code we need to add this control to the page. To access the control's public methods and properties, we just need scripting say JavaScript, for example:

JavaScript
<script language="javascript">
function Button1_onclick() {
//If DateTimePicker is outside a Form
    alert(DateTimePicker.Date);
//If DateTimePicker is inside a Form (Like in a ASP.NET WebForm)
    alert(Form1.DateTimePicker.Date);
}
</script>

This would need the onclick event of some button control point to that function. Example:

HTML
<input id="Button1" type="button" value="Button" 
         name="Button1" onclick="return Button1_onclick()">

Points of interest

There are some considerations to take in count when doing this. First, all client machines must have installed the .NET runtime framework in order to run the control, otherwise it will not work.

Second, the page and the DLL must be hosted in a virtual directory in IIS, this will not work locally or in other web servers (at least it is not proven to do so). Also, the folder where the DLL containing the control is located must have Read permissions.

And that is all we need to know. I included complete examples in the source & demo downloads. The source contains the control and the demo contains the HTML and Web Forms examples of use.

I hope someone finds this article useful.

History

  • 9/9/2003 - First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDo it before running the demo project Pin
Wgte16-Mar-04 3:51
Wgte16-Mar-04 3:51 
Generalconverting string value to money data type Pin
Qamarwis23-Feb-04 0:46
Qamarwis23-Feb-04 0:46 
GeneralRe: converting string value to money data type Pin
Raul Alonzo23-Feb-04 4:03
Raul Alonzo23-Feb-04 4:03 
QuestionHow do I access my PARAM values? Pin
eden18823-Jan-04 0:01
eden18823-Jan-04 0:01 
AnswerRe: How do I access my PARAM values? Pin
eden18823-Jan-04 6:07
eden18823-Jan-04 6:07 
GeneralRe: How do I access my PARAM values? Pin
Raul Alonzo12-Feb-04 4:28
Raul Alonzo12-Feb-04 4:28 
GeneralRe: How do I access my PARAM values? Pin
eden18812-Feb-04 4:38
eden18812-Feb-04 4:38 
GeneralRe: How do I access my PARAM values? Pin
MobileCoder2-May-04 14:03
MobileCoder2-May-04 14:03 
GeneralRe: How do I access my PARAM values? Pin
hejndorf9-Sep-04 0:38
hejndorf9-Sep-04 0:38 
Generali cant see the date picker on my webpage Pin
HarshP20-Jan-04 20:19
HarshP20-Jan-04 20:19 
GeneralRe: i cant see the date picker on my webpage Pin
eden18821-Jan-04 1:26
eden18821-Jan-04 1:26 
GeneralRe: i cant see the date picker on my webpage Pin
Raul Alonzo21-Jan-04 4:41
Raul Alonzo21-Jan-04 4:41 
GeneralRe: i cant see the date picker on my webpage Pin
shon911-Feb-04 22:21
shon911-Feb-04 22:21 
GeneralRe: i cant see the date picker on my webpage Pin
Anonymous12-Feb-04 4:15
Anonymous12-Feb-04 4:15 
GeneralBlank object Pin
eden18819-Jan-04 6:35
eden18819-Jan-04 6:35 
GeneralRe: Blank object Pin
Raul Alonzo20-Jan-04 4:33
Raul Alonzo20-Jan-04 4:33 
GeneralRe: Blank object Pin
eden18820-Jan-04 5:02
eden18820-Jan-04 5:02 
Generalespose .NET WinForm as an active X Control Pin
Virgil Reboton15-Jan-04 3:09
Virgil Reboton15-Jan-04 3:09 
GeneralRe: espose .NET WinForm as an active X Control Pin
Raul Alonzo15-Jan-04 4:28
Raul Alonzo15-Jan-04 4:28 
GeneralHere's what you need to do... Pin
Raul Alonzo15-Jan-04 5:38
Raul Alonzo15-Jan-04 5:38 
GeneralRe: Here's what you need to do... Pin
Virgil Reboton15-Jan-04 17:50
Virgil Reboton15-Jan-04 17:50 
GeneralRe: Here's what you need to do... Pin
Virgil Reboton15-Jan-04 18:14
Virgil Reboton15-Jan-04 18:14 
GeneralRe: Here's what you need to do... Pin
Raul Alonzo16-Jan-04 5:34
Raul Alonzo16-Jan-04 5:34 
GeneralImports System.Windows.Forms Does NOT Work w/ VB and vbc Compiler Pin
Luke Luchkan11-Dec-03 10:29
Luke Luchkan11-Dec-03 10:29 
GeneralRe: Imports System.Windows.Forms Does NOT Work w/ VB and vbc Compiler Pin
Raul Alonzo12-Dec-03 5:04
Raul Alonzo12-Dec-03 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.