在编码中不可否认调用active会给编程带来很大便利,虽然微软不太只持.net调用active 组件。
本实例是一个简单web 页面调用active组件的例子,实现把数据传入并显示在active组件里的控件中。
实例代码如下:
1、建立active组件(这里用.net c#创建)
(本程序中:建立类库添加组件类后托放:label 、 button 、groupbox各一个)
需要注意的是在生成.dll之前一定要在类库的assemblyinfo.cs中加入引用using system.security;和属性[assembly : allowpartiallytrustedcallers()],这是为了给控件赋予足够的权限使它能在页面上显示。
using system;
using system.componentmodel;
using system.collections;
using system.diagnostics;
using system.windows.forms ;
namespace activexdotnet
{
/**//// <summary>
/// abeencontrol 的摘要说明。
/// </summary>
public class abeencontrol : system.windows.forms.usercontrol
{
private system.windows.forms.label label1;
private system.windows.forms.button button1;
private system.windows.forms.groupbox groupbox1;
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public string labeltext
{
get
{
return this.label1.text.tostring();
}
set
{
this.label1.text=value;
}
}
public abeencontrol(system.componentmodel.icontainer container)
{
/**////
/// windows.forms 类撰写设计器支持所必需的
///
this.label1.text="test";
container.add(this);
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
public abeencontrol()
{
/**////
/// windows.forms 类撰写设计器支持所必需的
///
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
组件设计器生成的代码#region 组件设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.button1 = new system.windows.forms.button();
this.groupbox1 = new system.windows.forms.groupbox();
this.groupbox1.suspendlayout();
this.suspendlayout();
//
// label1
//
this.label1.location = new system.drawing.point(16, 24);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(296, 23);
this.label1.tabindex = 0;
this.label1.text = "label1";
//
// button1
//
this.button1.location = new system.drawing.point(120, 56);
this.button1.name = "button1";
this.button1.tabindex = 1;
this.button1.text = "button1";
this.button1.click += new system.eventhandler(this.button1_click);
//
// groupbox1
//
this.groupbox1.controls.add(this.label1);
this.groupbox1.controls.add(this.button1);
this.groupbox1.location = new system.drawing.point(0, 0);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(320, 88);
this.groupbox1.tabindex = 2;
this.groupbox1.tabstop = false;
this.groupbox1.text = "groupbox1";
//
// abeencontrol
//
this.controls.add(this.groupbox1);
this.name = "abeencontrol";
this.size = new system.drawing.size(328, 96);
this.load += new system.eventhandler(this.abeencontrol_load);
this.groupbox1.resumelayout(false);
this.resumelayout(false);
}
#endregion
private void button1_click(object sender, system.eventargs e)
{
this.label1.text="点击完成!";
}
private void abeencontrol_load(object sender, system.eventargs e)
{
}
}
}
2、web 页面调用active组件
注意web页面调用时需要把active的.dll放到web的虚拟要目录下。
<html>
<head>
<title>testactive</title>
<meta name="generator" content="microsoft visual studio .net 7.1">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body >
<font face="arial" size="1">
<object id="mycontrol1" name="mycontrol1" classid="http://localhost/webapplication1/activexdotnet.dll#activexdotnet.abeencontrol"
width="288" height="72" viewastext>
</object>
</font>
<form id="frm" name="frm">
<input type="text" name="txt" value="请输入数据:" id="text1">
<input type="button" value="确定" id="button1" name="button1">
<script type="text/javascript">
function doscript()
{
mycontrol1.labeltext=frm.txt.value;
}
</script>
</form>
</body>
</html>
新闻热点
疑难解答
图片精选