Simple way to expose a .NET WinForm control as an ActiveX control in any HTML page
2007-09-25 10:03
756 查看
转:http://www.codeproject.com/cs/miscctrl/htmlwincontrol.asp
Download source files - 7.5 Kb
Download demo project - 14.5 Kb
![](http://www.codeproject.com/cs/miscctrl/htmlwincontrol/htmlwincontrol.jpg)
Here's some of the code:
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:
Where the
Here's an example:
Now let's have a look at the
The next part,
That's all the code we need to add this control to the page. To access the control's
This would need the
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.
Download source files - 7.5 Kb
Download demo project - 14.5 Kb
![](http://www.codeproject.com/cs/miscctrl/htmlwincontrol/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 aUserControland build it in a .NET assembly. To avoid any complexity I used the
DateTimePicker, extended it, and added a
Dateproperty that returns the short date format of the
DateTimeproperty 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() { 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:
<object id=myId classid=myclassid>
Where the
idattribute 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
classidattribute contains information of the file and class that contains the control. There's no need for the
codebaseattribute since the .NET runtime and Explorer do the download process automatically.
Here's an example:
<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
classidattribute. 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.DateTimePickeris 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
publicmethods and properties, we just need scripting say JavaScript, for example:
<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
onclickevent 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.
相关文章推荐
- How to start a second thread in an MFC-based ActiveX control to fire events in Visual C++
- Writing an ActiveX Control in .NET
- How to get URL and QueryString value in an ASP.NET page
- The way to access control in sub-page witin masterpage
- An easy way to change colour in text control
- How to make text of an html checkbox clickable (like ASP.NET control)
- How to obtain access to an ActiveX control from its property page 如何从一个控件的属性页中获取对该控件的访问?
- A simple way to read an XML file in Java
- Passing parameters to an ASP.NET page running in an IFRAME
- how to know wether an element is exist in a html page.
- How To Retrieve the URL of a Web Page from an ActiveX Control
- How to get IOleSite interface of the WebBrowser in an ActiveX control
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- Simple and easiest way to make a screen shot in .net 2.0
- Using the HTMLControl Class in Adobe AIR to parse HTML as a data source
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- Passing Domain Credentials in C# to a WebBrowser Control in .Net WinForms Applications(winform中传递域认)
- write an updatable ActiveX control for web page in VB6
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- an easy way to debug windows service in .net