WEB下引用.NET Windows Control(Windows控件)经常出现的错误和解决办法
因为B/S项目有很多打印发票单据的需要,所以在网页中使用了Windows Control,专门用C#做了一个打印控件,页面调用这个打印控件,控件从WEBSERVICE获取数据进行打印,使用中出现过的一些问题,总结了一下,希望对大家有所帮助。
控件的代码如下:
这个程序正常情况下应该显示如图,点击“测试1”按钮后,弹出"WGSys.WGCtrlLib.CtrlPrint"

错误1控件不能正确加载,控件状态如下图,访问控件的时候脚本错误“拒绝访问”

这种情况往往是因为页面引用控件的位置不正确,比如本来应该是classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint"
却写成了classid="http:../WGCtrlLib2.dll#WGSys.WGCtrlLib.CtrlPrint",还有一种情况是Bin目录下边的Dll是不能直接引用的,所以这个控件不要放在bin下面,我是放在网站根目录下面的。
错误2控件加载,但是不能判断出具体类型,控件状态如下图

在脚本中测试 alert(document.all["CtrlPrint"]);的时候提示[object],说明页面已经判断出来这个控件了,但是不知道具体类型
产生这个错误的原因可能是classid里边的名称控件不正确,比如写成了classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint2"还有一种情况比较奇怪,如果你用了客户机器里边不存在的字体,控件也是这种状态,比如我控件上显示OK的字体用"Arial Black",如果客户机器上没有这个字体,控件同样是这种状态,不能正常加载。估计是MS的Bug...
错误3 控件在页面上彻底的不显示,连空白的地方都没有留
原因是你用.Net 1.1写的控件,客户机器上装的是.Net 2.0 估计还是MS的Bug,应该向下兼容才对啊。
错误4 无法验证发行者。您确定要运行此软件吗?

目前为止,只有一台机器每次加载控件的时候都提示这个错误。点击运行按钮后控件工作正常,但是很烦。
本来以为是Internet选项 -> 安全 -> 安全设置 里边的“运行未用Authenticode签名的组件”的设置问题,但是选择“启用”后仍然不起作用。并且选择“提示”后提示的窗口跟这个窗口是不同的。
提示的窗口应该如下

这个问题还没有解决,谁有办法请指教。
错误5 控件可能加载正常,运行某些方法的时候控件抛出“请求****类型的权限已失败”的异常?

这个可以到 管理工具->Microsoft .NET Framework 1.1 向导->调整.NET安全性 里边调节相应区域的安全级别
控件的代码如下:
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Drawing;
5
using System.Data;
6
using System.Windows.Forms;
7
using WGSys.DataExchange;
8
using WGSys.DataExchange.WGService;
9
10
namespace WGSys.WGCtrlLib
11

{
12
/**//// <summary>
13
/// Summary description for CtrlPrint.
14
/// </summary>
15
public class CtrlPrint : System.Windows.Forms.UserControl
16
{
17
/**//// <summary>
18
/// Required designer variable.
19
/// </summary>
20
private System.ComponentModel.Container components = null;
21
private System.Windows.Forms.Label label1;
22
WGSys.DataExchange.WGProxy svc = new WGSys.DataExchange.WGProxy();
23
public CtrlPrint()
24
{
25
// This call is required by the Windows.Forms Form Designer.
26
InitializeComponent();
27
28
// TODO: Add any initialization after the InitializeComponent call
29
30
}
31
private System.Drawing.Printing.PrintDocument printDoc;
32
Component Designer generated code#region Component Designer generated code
33
/**//// <summary>
34
/// Required method for Designer support - do not modify
35
/// the contents of this method with the code editor.
36
/// </summary>
37
private void InitializeComponent()
38
{
39
this.label1 = new System.Windows.Forms.Label();
40
this.SuspendLayout();
41
//
42
// label1
43
//
44
this.label1.Font = new System.Drawing.Font("Verdana", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
45
this.label1.ForeColor = System.Drawing.Color.White;
46
this.label1.Location = new System.Drawing.Point(0, 0);
47
this.label1.Name = "label1";
48
this.label1.Size = new System.Drawing.Size(80, 50);
49
this.label1.TabIndex = 0;
50
this.label1.Text = "OK";
51
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
52
//
53
// CtrlPrint
54
//
55
this.BackColor = System.Drawing.Color.Brown;
56
this.Controls.Add(this.label1);
57
this.Name = "CtrlPrint";
58
this.Size = new System.Drawing.Size(80, 50);
59
this.ResumeLayout(false);
60
61
}
62
#endregion
63
64
变量销毁#region 变量销毁
65
/**//// <summary>
66
/// Clean up any resources being used.
67
/// </summary>
68
protected override void Dispose( bool disposing )
69
{
70
if( disposing )
71
{
72
if(components != null)
73
{
74
components.Dispose();
75
}
76
}
77
base.Dispose( disposing );
78
}
79
#endregion
80
81
判断是否安装打印机#region 判断是否安装打印机
82
public void HavePaperType(string PrintPaperName)
83
{
84
printDoc = new System.Drawing.Printing.PrintDocument();
85
if(!NonePrinterOrPaper(PrintPaperName))
86
{
87
MessageBox.Show("此计算机安装了打印机,并且包含相应纸张!");
88
}
89
}
90
private bool NonePrinterOrPaper(string PrintPaperName)
91
{
92
try
93
{
94
if(System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count<=0)
95
{
96
MessageBox.Show("您的机器没有安装打印机,不能打印!请先添加打印机!","无法打印",MessageBoxButtons.OK,MessageBoxIcon.Error);
97
return true;
98
}
99
else
100
{
101
bool hasPaperType=false;
102
foreach(System.Drawing.Printing.PaperSize ps in printDoc.PrinterSettings.PaperSizes)
103
{
104
if(ps.PaperName==PrintPaperName)
105
{
106
printDoc.PrinterSettings.DefaultPageSettings.PaperSize=ps;
107
printDoc.DefaultPageSettings.PaperSize=ps;
108
printDoc.DefaultPageSettings.Margins.Bottom=0;
109
hasPaperType=true;
110
break;
111
}
112
}
113
if(!hasPaperType)
114
{
115
if(MessageBox.Show(@"未能找到相应的纸张 """+PrintPaperName+@""",这可能导致打印的格式不正确,您确定要打印么?","您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
116
{
117
return false;
118
}
119
else
120
{
121
return true;
122
}
123
}
124
return false;
125
}
126
}
127
catch(Exception exp)
128
{
129
if(MessageBox.Show("您本地的安全级别设置的太高,这可能导致打印的格式不正确,您确定要打印么?\n详细信息请参考:"+exp.Message,"您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
130
{
131
return false;
132
}
133
else
134
{
135
return true;
136
}
137
}
138
}
139
#endregion
140
141
142
测试控件#region 测试控件
143
public void ShowHelp()
144
{
145
string strMessage="";
146
strMessage+="-----------------------------------------------------\n";
147
strMessage+="WebService地址:"+svc.Url.ToString()+"\n";
148
strMessage+="版本:"+this.ProductVersion+"\n";
149
strMessage+="-----------------------------------------------------\n";
150
MessageBox.Show(strMessage,"打印控件状态",MessageBoxButtons.OK,MessageBoxIcon.Information);
151
}
152
#endregion
153
}
154
}
页面引用的代码如下
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Drawing;5
using System.Data;6
using System.Windows.Forms;7
using WGSys.DataExchange;8
using WGSys.DataExchange.WGService;9

10
namespace WGSys.WGCtrlLib11


{12

/**//// <summary>13
/// Summary description for CtrlPrint.14
/// </summary>15
public class CtrlPrint : System.Windows.Forms.UserControl16

{17

/**//// <summary> 18
/// Required designer variable.19
/// </summary>20
private System.ComponentModel.Container components = null;21
private System.Windows.Forms.Label label1;22
WGSys.DataExchange.WGProxy svc = new WGSys.DataExchange.WGProxy();23
public CtrlPrint()24

{25
// This call is required by the Windows.Forms Form Designer.26
InitializeComponent();27

28
// TODO: Add any initialization after the InitializeComponent call29

30
}31
private System.Drawing.Printing.PrintDocument printDoc;32

Component Designer generated code#region Component Designer generated code33

/**//// <summary> 34
/// Required method for Designer support - do not modify 35
/// the contents of this method with the code editor.36
/// </summary>37
private void InitializeComponent()38

{39
this.label1 = new System.Windows.Forms.Label();40
this.SuspendLayout();41
// 42
// label143
// 44
this.label1.Font = new System.Drawing.Font("Verdana", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));45
this.label1.ForeColor = System.Drawing.Color.White;46
this.label1.Location = new System.Drawing.Point(0, 0);47
this.label1.Name = "label1";48
this.label1.Size = new System.Drawing.Size(80, 50);49
this.label1.TabIndex = 0;50
this.label1.Text = "OK";51
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;52
// 53
// CtrlPrint54
// 55
this.BackColor = System.Drawing.Color.Brown;56
this.Controls.Add(this.label1);57
this.Name = "CtrlPrint";58
this.Size = new System.Drawing.Size(80, 50);59
this.ResumeLayout(false);60

61
}62
#endregion63

64

变量销毁#region 变量销毁65

/**//// <summary> 66
/// Clean up any resources being used.67
/// </summary>68
protected override void Dispose( bool disposing )69

{70
if( disposing )71

{72
if(components != null)73

{74
components.Dispose();75
}76
}77
base.Dispose( disposing );78
}79
#endregion80

81

判断是否安装打印机#region 判断是否安装打印机82
public void HavePaperType(string PrintPaperName)83

{84
printDoc = new System.Drawing.Printing.PrintDocument();85
if(!NonePrinterOrPaper(PrintPaperName))86

{87
MessageBox.Show("此计算机安装了打印机,并且包含相应纸张!");88
}89
}90
private bool NonePrinterOrPaper(string PrintPaperName)91

{92
try93

{94
if(System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count<=0)95

{96
MessageBox.Show("您的机器没有安装打印机,不能打印!请先添加打印机!","无法打印",MessageBoxButtons.OK,MessageBoxIcon.Error);97
return true;98
}99
else100

{101
bool hasPaperType=false;102
foreach(System.Drawing.Printing.PaperSize ps in printDoc.PrinterSettings.PaperSizes)103

{104
if(ps.PaperName==PrintPaperName)105

{106
printDoc.PrinterSettings.DefaultPageSettings.PaperSize=ps;107
printDoc.DefaultPageSettings.PaperSize=ps;108
printDoc.DefaultPageSettings.Margins.Bottom=0;109
hasPaperType=true;110
break;111
}112
}113
if(!hasPaperType)114

{115
if(MessageBox.Show(@"未能找到相应的纸张 """+PrintPaperName+@""",这可能导致打印的格式不正确,您确定要打印么?","您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)116

{117
return false;118
}119
else120

{121
return true;122
}123
}124
return false;125
}126
}127
catch(Exception exp)128

{129
if(MessageBox.Show("您本地的安全级别设置的太高,这可能导致打印的格式不正确,您确定要打印么?\n详细信息请参考:"+exp.Message,"您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)130

{131
return false;132
}133
else134

{135
return true;136
}137
}138
}139
#endregion140

141

142

测试控件#region 测试控件143
public void ShowHelp()144

{145
string strMessage="";146
strMessage+="-----------------------------------------------------\n";147
strMessage+="WebService地址:"+svc.Url.ToString()+"\n";148
strMessage+="版本:"+this.ProductVersion+"\n";149
strMessage+="-----------------------------------------------------\n";150
MessageBox.Show(strMessage,"打印控件状态",MessageBoxButtons.OK,MessageBoxIcon.Information);151
}152
#endregion153
}154
} 1
<OBJECT id="CtrlPrint" style="WIDTH: 80px; HEIGHT: 50px" height="32" width="32"
2
classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint" VIEWASTEXT>
3
</OBJECT>
4
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test1();" type="button" value="测试1">
5
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test2();" type="button" value="测试2">
6
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test3();" type="button" value="测试3">
7
<SCRIPT language="javascript">
8
function test1()
9
{
10
try
11
{
12
alert(document.all["CtrlPrint"]);
13
}
14
catch(e)
15
{
16
alert(e.message);
17
}
18
}
19
function test2()
20
{
21
if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")
22
{
23
try
24
{
25
document.all["CtrlPrint"].ShowHelp();
26
}
27
catch(e)
28
{
29
alert(e.message);
30
}
31
}
32
else
33
{
34
alert("打印控件未能正确加载!");
35
}
36
}
37
function test3()
38
{
39
if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")
40
{
41
try
42
{
43
document.all["CtrlPrint"].HavePaperType("A4");
44
}
45
catch(e)
46
{
47
alert(e.message);
48
}
49
}
50
else
51
{
52
alert("打印控件未能正确加载!");
53
}
54
}
55
</SCRIPT>
<OBJECT id="CtrlPrint" style="WIDTH: 80px; HEIGHT: 50px" height="32" width="32" 2
classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint" VIEWASTEXT>3
</OBJECT>4
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test1();" type="button" value="测试1">5
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test2();" type="button" value="测试2">6
<INPUT class="NormalButton" style="WIDTH: 80px; HEIGHT: 24px" onclick="test3();" type="button" value="测试3">7

<SCRIPT language="javascript">
8
function test1()9

{10
try11

{12
alert(document.all["CtrlPrint"]);13
}14
catch(e)15

{16
alert(e.message);17
}18
}19
function test2()20

{21
if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")22

{23
try24

{25
document.all["CtrlPrint"].ShowHelp();26
}27
catch(e)28

{29
alert(e.message);30
}31
}32
else33

{34
alert("打印控件未能正确加载!");35
}36
}37
function test3()38

{39
if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")40

{41
try42

{43
document.all["CtrlPrint"].HavePaperType("A4");44
}45
catch(e)46

{47
alert(e.message);48
}49
}50
else51

{52
alert("打印控件未能正确加载!");53
}54
}55
</SCRIPT>这个程序正常情况下应该显示如图,点击“测试1”按钮后,弹出"WGSys.WGCtrlLib.CtrlPrint"

错误1控件不能正确加载,控件状态如下图,访问控件的时候脚本错误“拒绝访问”

这种情况往往是因为页面引用控件的位置不正确,比如本来应该是classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint"
却写成了classid="http:../WGCtrlLib2.dll#WGSys.WGCtrlLib.CtrlPrint",还有一种情况是Bin目录下边的Dll是不能直接引用的,所以这个控件不要放在bin下面,我是放在网站根目录下面的。
错误2控件加载,但是不能判断出具体类型,控件状态如下图

在脚本中测试 alert(document.all["CtrlPrint"]);的时候提示[object],说明页面已经判断出来这个控件了,但是不知道具体类型
产生这个错误的原因可能是classid里边的名称控件不正确,比如写成了classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint2"还有一种情况比较奇怪,如果你用了客户机器里边不存在的字体,控件也是这种状态,比如我控件上显示OK的字体用"Arial Black",如果客户机器上没有这个字体,控件同样是这种状态,不能正常加载。估计是MS的Bug...
错误3 控件在页面上彻底的不显示,连空白的地方都没有留
原因是你用.Net 1.1写的控件,客户机器上装的是.Net 2.0 估计还是MS的Bug,应该向下兼容才对啊。
错误4 无法验证发行者。您确定要运行此软件吗?

目前为止,只有一台机器每次加载控件的时候都提示这个错误。点击运行按钮后控件工作正常,但是很烦。
本来以为是Internet选项 -> 安全 -> 安全设置 里边的“运行未用Authenticode签名的组件”的设置问题,但是选择“启用”后仍然不起作用。并且选择“提示”后提示的窗口跟这个窗口是不同的。
提示的窗口应该如下

这个问题还没有解决,谁有办法请指教。
错误5 控件可能加载正常,运行某些方法的时候控件抛出“请求****类型的权限已失败”的异常?

这个可以到 管理工具->Microsoft .NET Framework 1.1 向导->调整.NET安全性 里边调节相应区域的安全级别

