自己做了一个在客户端调用第三方驱动进行打印的ActiveX,类BPrinter提供的SetTicketInfo()方法进行打印。
在aspx中用一下代码可以调用,成功打印。
<input type="button" id="PrintBtn" value="打印" onclick="BPrinter.SetTicketInfo(''北京', 003, 1, 2100, '南京', '', '补');}" />
但是当我一如下的方式调用时,却显示BPrinter未定义:
function A() { BPrinter.SetTicketInfo('北京', 003, 1, 2100, '南京', '', '补'); } <input type="button" id="PrintBtn" value="打印" onclick="A()" />
请问是什么原因,觉得好诡异啊……
你的 BPrinter 定义在哪里的?
我给你段能正确运行的脚本吧。
HTML> <HEAD> <TITLE>Activex Demo</TITLE> </HEAD> <BODY> <script language=JavaScript> function SetTicketInfo() { BPrinter.SetTicketInfo(); } </script> <table border=1> <tr> <td> <OBJECT ID="BPrinter" WIDTH=0 HEIGHT=0 CODEBASE="BPrinter.CAB#Version=1,0,0,1" CLASSID="CLSID:BE362B78-BABC-494E-9BB9-567AE6D93384"></OBJECT> </td> </tr> </table> <input type="button" id="PrintBtn" value="打印" onclick="SetTicketInfo()"> </body> </html>
还是你啊,大神,谢谢。我的BPrinter类是定义在Activex里面的。
BPrinter.SetTicketInfo(); 这里的BPrinter是对应下面Object的ID="BPrinter"吗?
@spuerdaddy: 是的,JS 是运行在客户端的,因此你的 ActiveX 控件是在浏览器渲染页面的时候创建的实例,不是在服务器端创建的。
@Launcher: 还是有我上面说的这个问题唉。
你给的html确实是可以正确执行的,但是我把这个放在aspx里面就不行了,就提示“BPrinter未定义”。
代码是这样的:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TollDetail.aspx.cs" Inherits="ZTP.RPT.QueryToll.TollDetail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script type="text/javascript" src="../Scripts/jBox/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/jquery.jBox-2.3.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/i18n/jquery.jBox-zh-CN.js"></script> <link type="text/css" rel="stylesheet" href="../Scripts/jBox/Skins/Blue/jbox.css" /> <script type="text/javascript" src="../Scripts/My97DatePicker/WdatePicker.js"></script> <link type="text/css" rel="stylesheet" href="../css/css.css" /> <title></title> <script type="text/javascript"> $(document).ready(function () { document.getElementById("SetParaBtn").click(); }); function SetTicketInfo() { BPrinter.SetTicketInfo('北京', 2060005003, 1, 2100, '南京', '', '补'); } </script> </head> <body> <form id="form1" runat="server" style="text-align: center"> <asp:ScriptManager ID="ScriptManager1" runat="Server"> </asp:ScriptManager> <asp:UpdatePanel ID="updatePanel" runat="server"> <ContentTemplate> <div style="width: 490px; padding: 5px 5px;"> <table align="center" width="480px" cellpadding="0" cellspacing="0" border="0"> <tr> <td colspan="2"> <div> <fieldset> <tr> <td colspan="2" height="2px"> </td> </tr> <tr align="right" valign="middle" style="height: 30px"> <td colspan="2"> <input type="button" id="SetParaBtn" style=" display:none" value="设置参数" onclick="BPrinter.SetInitPara()" /> <input type="button" id="PrintBtn" value="打印" onclick="SetTicketInfo()" /> <object id="BPrinter" classid="clsid:8FE22B96-0AE2-49FE-AD63-3AA0EE38B202" codebase="../DownLoad/BPrinter.cab#version=1,0,0,0"></object> </td> </tr> </table> </div> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html>
上面的代码里有两个Inpute 都调用BPrinter里的方法,第一种是直接在onclick=“BPrinter.SetInitPara()",这样是可以的。
另一种就是onClick=”SetTicketInfo()",然后在SetTicketInfo()里面调BPtrinter,确实不能成功……
@spuerdaddy: 你应该把浏览器中此页面的源码贴出来,而不是给我贴你的 aspx 页面。
@Launcher:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1"> <script type="text/javascript" src="../Scripts/jBox/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/jquery.jBox-2.3.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/i18n/jquery.jBox-zh-CN.js"></script> <link type="text/css" rel="stylesheet" href="../Scripts/jBox/Skins/Blue/jbox.css" /> <script type="text/javascript" src="../Scripts/My97DatePicker/WdatePicker.js"></script> <link type="text/css" rel="stylesheet" href="../css/css.css" /><title> </title> <script type="text/javascript"> $(document).ready(function () { document.getElementById("SetParaBtn").click(); }); function SetTicketInfo() { BPrinter.SetTicketInfo('北京', 2060005003, 1, 2100, '南京', '', '补'); } </script> </head> <body> <form name="form1" method="post" action="TollDetail.aspx?CommandArgment=2013-06-03+21%3a33%3a22%2c1395%2c2060005%2c107%2c0%2c&___t0.20701539887739345" id="form1" style="text-align: center"> <div> <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE5NzEwNjUzNjkPZBYCAgMPZBYCAgMPZBYCZg9kFhQCAQ8PFgIeBFRleHQFEjIwMTMtMDYtMDUgMDowNTowN2RkAgMPDxYCHwAFATBkZAIFDw8WAh8ABQnlpoLnmovmuK9kZAIHDw8WAh8ABQzljZfkuqzlm5vmoaVkZAIJDw8WAh8ABQbmoJbpnJ5kZAILDw8WAh8ABQMxMDdkZAINDw8WAh8ABQIxNGRkAg8PDxYCHwAFCeealk44NDIxMGRkAhEPDxYCHwAFCjE1MDUwMzg0NTRkZAITDw8WAh8ABQU1NDUwMGRkZAjSWI+N3ouPPXIfMt+I/mPUz1SB" /> </div> <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['form1']; if (!theForm) { theForm = document.form1; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> </script> <script src="/WebResource.axd?d=rgVtQcS56CdKPED1c2wQHQRRBzb2rlKii1nQ45wDmJtHqGjwY_-rTVcz4JByCjgLFhX__Um0DLzO2xzlgmUM8GF_hP81&t=635181938996372001" type="text/javascript"></script> <script src="/ScriptResource.axd?d=wvmNJJs_36nMq448j8V1RQ1tza5_MSkj-2adXHA2UHIzqrnlibyDeY-EaLJ99d_be_sU5YFG-f9xHI_oUdu99KEIBa4Qg-WzHpRGiOUNysnFsmpj1RhZu5hTmZRJLaC_Bbei09fyuve6c5MjDF3LrPCv7q1WZP1OSpIdHlghdM9zo2p70&t=635062091978934521" type="text/javascript"></script> <script src="/ScriptResource.axd?d=6PBTcHm67sxdhNnvN5nDWtxK8KgpSu1CYaKMo0nNYzw2xYjcemuLBCum8suXOlJTKLywpgIXlLlctqUWPN6b-IAdysMpAVt34z7NvrHmsaC5N3PMLmWGOo5xtfCezcRehp29-RHvDf9I2jtXEH3InXz3v6MYLme41t6P0AK6tuPEZJe40&t=635062091978934521" type="text/javascript"></script> <div> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="4E2A4A08" /> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWCwLxv4yzAgKu4/H4BQLKpcCtDALN1tGzDgKj7bu+CQKBqZL4BwK//vatCAKI3JmXCwLcjpnDAwLm6pj/DQLhluvLAW6BF+e4IRM0/GDVJXrlpas+XXWx" /> </div> <script type="text/javascript"> //<![CDATA[ Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1')); Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tupdatePanel'], [], [], 90); //]]> </script> <div id="updatePanel"> <div style="width: 490px; padding: 5px 5px;"> <table align="center" width="480px" cellpadding="0" cellspacing="0" border="0"> <tr> <td colspan="2"> <div> <fieldset> <legend style="font-weight: bold">详细信息</legend> <table width="100%" cellpadding="0" cellspacing="5" border="0"> <tr> <td align="right"> 出口时间 </td> <td > <input name="ExitTime_tb" type="text" value="2013-06-05 0:05:07" id="ExitTime_tb" disabled="disabled" /> </td> <td align="right"> 标识站 </td> <td colspan="3"> <input name="FlagStation_tb" type="text" value="0" id="FlagStation_tb" disabled="disabled" /> </td> </tr> <tr> <td align="right"> 入口 </td> <td> <input name="EntryStation_tb" type="text" value="如皋港" id="EntryStation_tb" disabled="disabled" /> </td> <td align="right"> 过江通道 </td> <td> <input name="ComputeStation_tb" type="text" value="南京四桥" id="ComputeStation_tb" disabled="disabled" /> </td> </tr> <tr> <td align="right"> 出口 </td> <td> <input name="ExitStation_tb" type="text" value="栖霞" id="ExitStation_tb" disabled="disabled" /> </td> <td align="right"> 出口车道 </td> <td> <input name="ExitLane_tb" type="text" value="107" id="ExitLane_tb" disabled="disabled" /> </td> </tr> <tr> <td align="right"> 车型 </td> <td> <input name="VehicleClass_tb" type="text" value="14" id="VehicleClass_tb" disabled="disabled" /> </td> <td align="right"> 车牌 </td> <td> <input name="VehicleLicense_tb" type="text" value="皖N84210" id="VehicleLicense_tb" disabled="disabled" /> </td> </tr> <tr> <td align="right"> 卡号 </td> <td> <input name="CardId_tb" type="text" value="1505038454" id="CardId_tb" disabled="disabled" /> </td> <td align="right"> 金额 </td> <td> <input name="TotalToll_tb" type="text" value="54500" id="TotalToll_tb" disabled="disabled" /> </td> </tr> </table> </fieldset> </div> </td> </tr> <tr> <td colspan="2" height="2px"> </td> </tr> <tr align="right" valign="middle" style="height: 30px"> <td colspan="2"> <input type="button" id="SetParaBtn" style=" display:none" value="设置参数" onclick="BPrinter.SetInitPara()" /> <input type="button" id="InitDeviceBtn" style=" display:none" value="初始化" onclick="BPrinter.InitDevice()" /> <input type="button" id="PrintBtn" value="打印" onclick="SetTicketInfo()" /> <object id="BPrinter" classid="clsid:8FE22B96-0AE2-49FE-AD63-3AA0EE38B202" codebase="../DownLoad/BPrinter.cab#version=1,0,0,0"></object> <input type="button" id="ExitDeviceBtn" style=" display:none" value="关闭打印机" onclick="BPrinter.ExitDevice()" /> </td> </tr> </table> </div> </div> <script type="text/javascript"> //<![CDATA[ Sys.Application.initialize(); //]]> </script> </form> </body> </html>
嗯,这个是浏览器源码
@spuerdaddy: 很好,现在请你把该页面保存成 HTML 文件,然后把一些跟 Active X 控件不相关脚本去掉,尽量简化成我的例子那样,然后直接用浏览器从本地打开此文件。
@Launcher:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1"> <script type="text/javascript" src="../Scripts/jBox/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/jquery.jBox-2.3.min.js"></script> <script type="text/javascript" src="../Scripts/jBox/i18n/jquery.jBox-zh-CN.js"></script> <link type="text/css" rel="stylesheet" href="../Scripts/jBox/Skins/Blue/jbox.css" /> <script type="text/javascript" src="../Scripts/My97DatePicker/WdatePicker.js"></script> <link type="text/css" rel="stylesheet" href="../css/css.css" /><title> </title> <script type="text/javascript"> function SetTicketInfo() { BPrinter.SetInitPara(); BPrinter.SetTicketInfo('北京', 2060005003, 1, 2100, '南京', '', '补'); } </script> </head> <body> <table > <tr > <td > <input type="button" id="PrintBtn" value="打印" onclick="SetTicketInfo()" /> <object id="BPrinter" classid="clsid:8FE22B96-0AE2-49FE-AD63-3AA0EE38B202" codebase="../DownLoad/BPrinter.cab#version=1,0,0,0"></object> </td> </tr> </table> </body> </html>
删成这样之后就可以了,问题出在哪里啊?难道和我使用WebForm不兼容?
@spuerdaddy: 这个我不知道,这一步只是测试你的 ActiveX 控件正确安装了。你原来的 aspx 页面是通过 VS 调试启动的吗?
@Launcher: 嗯,通过VS调试启动。和在IIS里面发布之后用浏览器调用,两种方式我都试过,情况是一样的。
@spuerdaddy: 那你可以把这个简化后的 html 改成 aspx 页面,然后测试下,如果能够成功,你就逐渐的增加一些原来文件中的内容,并逐步测试。
@spuerdaddy: http://q.cnblogs.com/q/57950/
我看到他这里是这么写的 document.BPrinter ,你试试。
@Launcher: 确实这样就可以了,谢谢啊!大神见多识广~嘿嘿。满意结贴!