asp.net如何打印动态表

时间:2012-11-07 12:36:45

标签: javascript asp.net

我在Literal下面有这个显示我的动态表

<div id="Print">     
    <asp:Literal ID="Literal1" runat="server"></asp:Literal> 
</div> 

我放置了一个图像按钮来打印

<asp:ImageButton ID="ImageButton5" runat="server" 
ImageUrl="~/Image/Print_Icon_Design.jpg" Height="25px" 
Width="27px" style="margin-left:20px" OnClientClick="CallPrint"/> 

我有下面的代码可以打印,但它无法正常工作,请你帮忙

<script language="javascript" type="text/javascript">
    function CallPrint(Print) {
        var prtContent = document.getElementById(Print);
        var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
</script>

4 个答案:

答案 0 :(得分:0)

跳过参数Print for the function并添加更改getElementById

var prtContent = document.getElementById('Print');

答案 1 :(得分:0)

您需要将Print放在引号内,并且您的函数缺少最后的大括号。 此外,您不需要Print参数(因为您没有从ImageButton传入它,并且您已经使用getElementById()检索它:

function CallPrint() {
            var prtContent = document.getElementById("Print");
            var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
}

答案 2 :(得分:0)

试试这个

<script language="javascript" type="text/javascript">

function CallPrint() {
                var prtContent = document.getElementById('Print');
                var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
                WinPrint.document.open();
                WinPrint.document.write(prtContent.innerHTML);
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                return false;
}
 </script>

<asp:ImageButton ID="ImageButton5" runat="server" 
                        ImageUrl="~/Image/Print_Icon_Design.jpg" Height="25px" 
            Width="27px" style="margin-left:20px" OnClientClick="return CallPrint()"/>

返回false给OnClientClick会阻止回发

答案 3 :(得分:0)

prtContent.innerHTML将为null,因为您使用的是Print而不是&#39; Print&#39;

        function CallPrint() {
        var prtContent = document.getElementById('Print');
        var htmlcontent = prtContent.innerHTML;
        var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(htmlcontent);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
    }