我有一个在asp.net中打印的面板。我已经尝试了很多java脚本代码,但我一直得到内部html错误。以下是代码。 错误代码
Microsoft JScript runtime error: Unable to get value of the property 'innerHTML': object is null or undefined
按钮代码
<asp:Button ID="Savebutton" runat="server" OnClick="Button1_Click" OnClientClick="return PrintPanel();"/>
java脚本代码
function PrintPanel() {
var panel = document.getElementById("<%=pnlContents.ClientID %>");
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(panel.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
setTimeout(function () {
printWindow.print();
}, 500);
return false;
}
div我要打印
<div id="printdiv" class="printable" runat="server">
<asp:Panel ID="pnlContents" runat="server">
<asp:Label ID="Label9" runat="server" Text="Medical Record No"></asp:Label>
<asp:Label ID="MRNLBLp" runat="server"></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text="Patient Name"></asp:Label>
<asp:Label ID="Namelblp" runat="server"></asp:Label>
<br />
<asp:Label ID="Label6" runat="server" Text="Visit Date"></asp:Label>
<asp:Label ID="visitdatelblp" runat="server"></asp:Label>
<br />
<asp:Label ID="Label10" runat="server" Text="Symptoms"></asp:Label>
<asp:Label ID="symptomlblp" runat="server"></asp:Label>
<br />
<asp:Label ID="Label5" runat="server" Text="Test Prescribed"></asp:Label>
<asp:Label ID="testlblp" runat="server"></asp:Label>
</asp:Panel>
</div>
答案 0 :(得分:2)
很可能这是失败的,因为以下行
var panel = document.getElementById("<%=pnlContents.ClientID %>");
不返回有效元素。我会调试它来验证