How to use windows forms in the webapplication.
generally we cant use some windowsforms functionalities in the webapplications like previously in this DNS we had a large discussion for how to select multiple files to upload the images to server for this i gave the alternate solutions for this but no direct way for selecting multiple files as we do in winforms application. Now i found a way to use that functionality by making the activex object.
How to create ActiveX control for using windows forms in web application.
points
want.( here in my example iam designing as i required to select multiple files as
we dont have an option to select multiple files for uploading using file upload
so here we are preparing our own control for selecting multiple files)
file.
the namespace and class definition.[ProgId("multiUpload.ActiveControl")]
i added.
statement.
id for identification so just for generating this follow below steps.
opens a window and show you different statements with radio buttons.
id button on the right side and select copy button and exit the window.
[ComVisible(true)]
cs files, if you want to expose those methods to javascript.
below steps
the new in the dropdown.
pwd you can give password for it here iam not doing it, then press ok which creates
the sn key file for project.
d:\\testactive.dll
file from debug folder to the cmd after typing regasm /codebase which automatically
adds the path by continuing with /codebase
how to consume the activex created above
< object
id="DemoActiveX" classid="clsid:415D09B9-3C9F-43F4-BB5C-C056263EF270" codebase="multiUpload.cab"
height="200" width="200">
</object >
the GUID given in the classlibrary project.
give properties height and width.
here is my sample code for creating ActiveX means classlibrary project code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace multiUpload
{
[ProgId("multiUpload.ActiveControl")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("415D09B9-3C9F-43F4-BB5C-C056263EF270")]
[ComVisible(true)]
public partial class ActiveControl : UserControl
{
string values=string.Empty;
public ActiveControl()
{
InitializeComponent();
}
[ComVisible(true)]
public string getfiles()
{
return values.TrimEnd(',');
}
[ComVisible(true)]
public void button1_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.ShowDialog();
if (openFileDialog1.FileNames!= null)
{
foreach (string names in openFileDialog1.FileNames)
{
listBox1.Items.Add(names);
values += names+",";
}
}
else { MessageBox.Show("no items selected"); } }
catch
(Exception ex) { MessageBox.Show(ex.Message); } } } }
my aspx code in the
webapplication for invoking the activex
<div >
<table >
<tr >
<td >
<object id="Object1" classid="clsid:415D09B9-3C9F-43F4-BB5C-C056263EF270" codebase="multiUpload.cab"
height="200" width="200" >
</object >
</td >
</tr >
<tr >
<td >
<input type="button" value="click" onclick="getitems();" / >
</td >
</tr >
</table >
</div >
my script code
<script type="text/javascript">
function getitems() {
var items = document.DemoActiveX;
if (items) {
alert(items.getfiles());
}
else
alert("no msg");
}
</script>