在編碼中不可否認調用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>
新聞熱點
疑難解答
圖片精選