WEB下引用.NET Windows Control(Windows控件)经常出现的错误和解决办法

因为B/S项目有很多打印发票单据的需要,所以在网页中使用了Windows Control,专门用C#做了一个打印控件,页面调用这个打印控件,控件从WEBSERVICE获取数据进行打印,使用中出现过的一些问题,总结了一下,希望对大家有所帮助。
控件的代码如下:
  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Drawing;
  5 None.gif using  System.Data;
  6 None.gif using  System.Windows.Forms;
  7 None.gif using  WGSys.DataExchange;
  8 None.gif using  WGSys.DataExchange.WGService;
  9 None.gif
 10 None.gif namespace  WGSys.WGCtrlLib
 11 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 13InBlock.gif    /// Summary description for CtrlPrint.
 14ExpandedSubBlockEnd.gif    /// </summary>

 15InBlock.gif    public class CtrlPrint : System.Windows.Forms.UserControl
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 18InBlock.gif        /// Required designer variable.
 19ExpandedSubBlockEnd.gif        /// </summary>

 20InBlock.gif        private System.ComponentModel.Container components = null;
 21InBlock.gif        private System.Windows.Forms.Label label1;
 22InBlock.gif        WGSys.DataExchange.WGProxy svc = new WGSys.DataExchange.WGProxy();
 23InBlock.gif        public CtrlPrint()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            // This call is required by the Windows.Forms Form Designer.
 26InBlock.gif            InitializeComponent();
 27InBlock.gif
 28InBlock.gif            // TODO: Add any initialization after the InitializeComponent call
 29InBlock.gif
 30ExpandedSubBlockEnd.gif        }

 31InBlock.gif        private System.Drawing.Printing.PrintDocument printDoc;
 32ContractedSubBlock.gifExpandedSubBlockStart.gif        Component Designer generated code#region Component Designer generated code
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 34InBlock.gif        /// Required method for Designer support - do not modify 
 35InBlock.gif        /// the contents of this method with the code editor.
 36ExpandedSubBlockEnd.gif        /// </summary>

 37InBlock.gif        private void InitializeComponent()
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            this.label1 = new System.Windows.Forms.Label();
 40InBlock.gif            this.SuspendLayout();
 41InBlock.gif            // 
 42InBlock.gif            // label1
 43InBlock.gif            // 
 44InBlock.gif            this.label1.Font = new System.Drawing.Font("Verdana"26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
 45InBlock.gif            this.label1.ForeColor = System.Drawing.Color.White;
 46InBlock.gif            this.label1.Location = new System.Drawing.Point(00);
 47InBlock.gif            this.label1.Name = "label1";
 48InBlock.gif            this.label1.Size = new System.Drawing.Size(8050);
 49InBlock.gif            this.label1.TabIndex = 0;
 50InBlock.gif            this.label1.Text = "OK";
 51InBlock.gif            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
 52InBlock.gif            // 
 53InBlock.gif            // CtrlPrint
 54InBlock.gif            // 
 55InBlock.gif            this.BackColor = System.Drawing.Color.Brown;
 56InBlock.gif            this.Controls.Add(this.label1);
 57InBlock.gif            this.Name = "CtrlPrint";
 58InBlock.gif            this.Size = new System.Drawing.Size(8050);
 59InBlock.gif            this.ResumeLayout(false);
 60InBlock.gif
 61ExpandedSubBlockEnd.gif        }

 62ExpandedSubBlockEnd.gif        #endregion

 63InBlock.gif
 64ContractedSubBlock.gifExpandedSubBlockStart.gif        变量销毁#region 变量销毁
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> 
 66InBlock.gif        /// Clean up any resources being used.
 67ExpandedSubBlockEnd.gif        /// </summary>

 68InBlock.gif        protected override void Dispose( bool disposing )
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            if( disposing )
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 72InBlock.gif                if(components != null)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 74InBlock.gif                    components.Dispose();
 75ExpandedSubBlockEnd.gif                }

 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            base.Dispose( disposing );
 78ExpandedSubBlockEnd.gif        }

 79ExpandedSubBlockEnd.gif        #endregion

 80InBlock.gif
 81ContractedSubBlock.gifExpandedSubBlockStart.gif        判断是否安装打印机#region 判断是否安装打印机
 82InBlock.gif        public void HavePaperType(string PrintPaperName)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            printDoc = new System.Drawing.Printing.PrintDocument();
 85InBlock.gif            if(!NonePrinterOrPaper(PrintPaperName))
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                MessageBox.Show("此计算机安装了打印机,并且包含相应纸张!");
 88ExpandedSubBlockEnd.gif            }

 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif        private bool NonePrinterOrPaper(string PrintPaperName)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            try
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 94InBlock.gif                if(System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count<=0)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 96InBlock.gif                    MessageBox.Show("您的机器没有安装打印机,不能打印!请先添加打印机!","无法打印",MessageBoxButtons.OK,MessageBoxIcon.Error);
 97InBlock.gif                    return true;
 98ExpandedSubBlockEnd.gif                }

 99InBlock.gif                else
100ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
101InBlock.gif                    bool hasPaperType=false;
102InBlock.gif                    foreach(System.Drawing.Printing.PaperSize ps in printDoc.PrinterSettings.PaperSizes)
103ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
104InBlock.gif                        if(ps.PaperName==PrintPaperName)
105ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
106InBlock.gif                            printDoc.PrinterSettings.DefaultPageSettings.PaperSize=ps;
107InBlock.gif                            printDoc.DefaultPageSettings.PaperSize=ps;
108InBlock.gif                            printDoc.DefaultPageSettings.Margins.Bottom=0;
109InBlock.gif                            hasPaperType=true;
110InBlock.gif                            break;
111ExpandedSubBlockEnd.gif                        }

112ExpandedSubBlockEnd.gif                    }

113InBlock.gif                    if(!hasPaperType)
114ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
115InBlock.gif                        if(MessageBox.Show(@"未能找到相应的纸张 """+PrintPaperName+@""",这可能导致打印的格式不正确,您确定要打印么?","您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
116ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
117InBlock.gif                            return false;
118ExpandedSubBlockEnd.gif                        }

119InBlock.gif                        else
120ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
121InBlock.gif                            return true;
122ExpandedSubBlockEnd.gif                        }

123ExpandedSubBlockEnd.gif                    }

124InBlock.gif                    return false;
125ExpandedSubBlockEnd.gif                }

126ExpandedSubBlockEnd.gif            }

127InBlock.gif            catch(Exception exp)
128ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
129InBlock.gif                if(MessageBox.Show("您本地的安全级别设置的太高,这可能导致打印的格式不正确,您确定要打印么?\n详细信息请参考:"+exp.Message,"您确定要打印么?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
130ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
131InBlock.gif                    return false;
132ExpandedSubBlockEnd.gif                }

133InBlock.gif                else
134ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
135InBlock.gif                    return true;
136ExpandedSubBlockEnd.gif                }

137ExpandedSubBlockEnd.gif            }

138ExpandedSubBlockEnd.gif        }

139ExpandedSubBlockEnd.gif        #endregion

140InBlock.gif
141InBlock.gif
142ContractedSubBlock.gifExpandedSubBlockStart.gif        测试控件#region 测试控件
143InBlock.gif        public void ShowHelp()
144ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
145InBlock.gif            string strMessage="";
146InBlock.gif            strMessage+="-----------------------------------------------------\n";
147InBlock.gif            strMessage+="WebService地址:"+svc.Url.ToString()+"\n";
148InBlock.gif            strMessage+="版本:"+this.ProductVersion+"\n";
149InBlock.gif            strMessage+="-----------------------------------------------------\n";
150InBlock.gif            MessageBox.Show(strMessage,"打印控件状态",MessageBoxButtons.OK,MessageBoxIcon.Information);
151ExpandedSubBlockEnd.gif        }

152ExpandedSubBlockEnd.gif        #endregion

153ExpandedSubBlockEnd.gif    }

154ExpandedBlockEnd.gif}
页面引用的代码如下
 1 None.gif < OBJECT  id ="CtrlPrint"  style ="WIDTH: 80px; HEIGHT: 50px"  height ="32"  width ="32"  
 2 None.gif                                            classid ="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint"  VIEWASTEXT >
 3 None.gif </ OBJECT >
 4 None.gif < INPUT  class ="NormalButton"  style ="WIDTH: 80px; HEIGHT: 24px"  onclick ="test1();"  type ="button"  value ="测试1" >
 5 None.gif < INPUT  class ="NormalButton"  style ="WIDTH: 80px; HEIGHT: 24px"  onclick ="test2();"  type ="button"  value ="测试2" >
 6 None.gif < INPUT  class ="NormalButton"  style ="WIDTH: 80px; HEIGHT: 24px"  onclick ="test3();"  type ="button"  value ="测试3" >
 7 ExpandedBlockStart.gifContractedBlock.gif < SCRIPT  language ="javascript" > dot.gif
 8InBlock.gif                                    function test1()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
10InBlock.gif                                        try
11ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
12InBlock.gif                                            alert(document.all["CtrlPrint"]);
13ExpandedSubBlockEnd.gif                                        }

14InBlock.gif                                        catch(e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
16InBlock.gif                                            alert(e.message);
17ExpandedSubBlockEnd.gif                                        }

18ExpandedSubBlockEnd.gif                                    }

19InBlock.gif                                    function test2()
20ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
21InBlock.gif                                        if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")
22ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
23InBlock.gif                                            try
24ExpandedSubBlockStart.gifContractedSubBlock.gif                                            dot.gif{
25InBlock.gif                                                document.all["CtrlPrint"].ShowHelp();
26ExpandedSubBlockEnd.gif                                            }

27InBlock.gif                                            catch(e)
28ExpandedSubBlockStart.gifContractedSubBlock.gif                                            dot.gif{
29InBlock.gif                                                alert(e.message);
30ExpandedSubBlockEnd.gif                                            }

31ExpandedSubBlockEnd.gif                                        }

32InBlock.gif                                        else
33ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
34InBlock.gif                                            alert("打印控件未能正确加载!");
35ExpandedSubBlockEnd.gif                                        }

36ExpandedSubBlockEnd.gif                                    }

37InBlock.gif                                    function test3()
38ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
39InBlock.gif                                        if(document.all["CtrlPrint"]=="WGSys.WGCtrlLib.CtrlPrint")
40ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
41InBlock.gif                                            try
42ExpandedSubBlockStart.gifContractedSubBlock.gif                                            dot.gif{
43InBlock.gif                                                document.all["CtrlPrint"].HavePaperType("A4");
44ExpandedSubBlockEnd.gif                                            }

45InBlock.gif                                            catch(e)
46ExpandedSubBlockStart.gifContractedSubBlock.gif                                            dot.gif{
47InBlock.gif                                                alert(e.message);
48ExpandedSubBlockEnd.gif                                            }

49ExpandedSubBlockEnd.gif                                        }

50InBlock.gif                                        else
51ExpandedSubBlockStart.gifContractedSubBlock.gif                                        dot.gif{
52InBlock.gif                                            alert("打印控件未能正确加载!");
53ExpandedSubBlockEnd.gif                                        }

54ExpandedBlockEnd.gif                                    }

55None.gif
</ SCRIPT >

这个程序正常情况下应该显示如图,点击“测试1”按钮后,弹出"WGSys.WGCtrlLib.CtrlPrint"



错误1控件不能正确加载,控件状态如下图,访问控件的时候脚本错误“拒绝访问”
4.jpg
这种情况往往是因为页面引用控件的位置不正确,比如本来应该是classid="http:../WGCtrlLib.dll#WGSys.WGCtrlLib.CtrlPrint"
却写成了classid="http:../WGCtrlLib2.dll#WGSys.WGCtrlLib.CtrlPrint",还有一种情况是Bin目录下边的Dll是不能直接引用的,所以这个控件不要放在bin下面,我是放在网站根目录下面的。
错误2控件加载,但是不能判断出具体类型,控件状态如下图
5.jpg
在脚本中测试 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安全性 里边调节相应区域的安全级别

转载于:https://www.cnblogs.com/wormday/archive/2006/07/23/457691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值