Click here to Skip to main content
13,347,462 members (53,180 online)
Click here to Skip to main content
Add your own
alternative version


98 bookmarked
Posted 7 Sep 2003

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

, 7 Sep 2003
Rate this:
Please Sign up or sign in to vote.
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


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:

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

        public DateTimePicker() : base()
        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:

<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:

<object id="DateTimePicker" height="31" width="177" 

                     Web.Controls.DateTime.DateTimePicker" VIEWASTEXT>

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:

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

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

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


  • 9/9/2003 - First version


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


About the Author

Raul Alonzo
Web Developer
Mexico Mexico
No Biography provided

You may also be interested in...


Comments and Discussions

Questionnot show activexcontrol Pin
jaimin8527-Aug-13 4:07
memberjaimin8527-Aug-13 4:07 
GeneralMy vote of 1 Pin
Omer Afridi11-Oct-09 22:44
memberOmer Afridi11-Oct-09 22:44 
Generali cannot retrive value from a win form textbox using javascript Pin
njoshi19878-Oct-09 21:55
membernjoshi19878-Oct-09 21:55 
GeneralMy vote of 1 Pin
mfrasiu3-Aug-09 13:29
membermfrasiu3-Aug-09 13:29 
GeneralMy vote of 2 Pin
jamilhaddadin1-Jan-09 5:06
memberjamilhaddadin1-Jan-09 5:06 
Generaldynamic add winform control to htmlpage problem Pin
jefers_gu22-Sep-08 16:21
memberjefers_gu22-Sep-08 16:21 
Generaldynamic add winform control to Html page failed Pin
jefers_gu22-Sep-08 16:19
memberjefers_gu22-Sep-08 16:19 
GeneralNice Pin
AbhilashAshok26-Dec-07 1:46
memberAbhilashAshok26-Dec-07 1:46 
GeneralGood work Pin
xibeifeijian25-Dec-07 23:00
memberxibeifeijian25-Dec-07 23:00 
GeneralYou need to change the classid string to get it working. Pin
amithiremath27-Nov-07 1:41
memberamithiremath27-Nov-07 1:41 
GeneralRe: You need to change the classid string to get it working. Pin
Nick Polyak10-Dec-07 14:09
memberNick Polyak10-Dec-07 14:09 
GeneralRe: You need to change the classid string to get it working. Pin
defineconst7-Oct-08 16:51
memberdefineconst7-Oct-08 16:51 
QuestionCan I load the DLL directly from user's PC Pin
goobleblob20-May-07 21:04
membergoobleblob20-May-07 21:04 
AnswerRe: Can I load the DLL directly from user's PC Pin
Vasudevan Deepak Kumar9-Jul-07 3:08
memberVasudevan Deepak Kumar9-Jul-07 3:08 
GeneralDoesn't work after i compiled the source code Pin
musclejj29-Apr-07 5:32
membermusclejj29-Apr-07 5:32 
AnswerRe: Doesn't work after i compiled the source code Pin
Raul Alonzo16-May-07 13:38
memberRaul Alonzo16-May-07 13:38 
GeneralRe: Doesn't work after i compiled the source code Pin
Vasudevan Deepak Kumar9-Jul-07 3:10
memberVasudevan Deepak Kumar9-Jul-07 3:10 
GeneralIs it stable/robust/reliable Pin
HughBG1-Apr-07 23:04
memberHughBG1-Apr-07 23:04 
GeneralRe: Is it stable/robust/reliable Pin
Vasudevan Deepak Kumar9-Jul-07 3:09
memberVasudevan Deepak Kumar9-Jul-07 3:09 
Questioncontrols events Pin
Vishal Marya20-Mar-07 22:45
memberVishal Marya20-Mar-07 22:45 
AnswerRe: controls events Pin
Raul Alonzo22-Mar-07 7:03
memberRaul Alonzo22-Mar-07 7:03 
QuestionCan I load it from a file Pin
HughBG14-Mar-07 21:15
memberHughBG14-Mar-07 21:15 
AnswerRe: Can I load it from a file Pin
Raul Alonzo16-Mar-07 6:19
memberRaul Alonzo16-Mar-07 6:19 
GeneralRe: Can I load it from a file Pin
HughBG1-Apr-07 22:56
memberHughBG1-Apr-07 22:56 
GeneralRe: Can I load it from a file Pin
Raul Alonzo2-Apr-07 7:05
memberRaul Alonzo2-Apr-07 7:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 8 Sep 2003
Article Copyright 2003 by Raul Alonzo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid