打印网页ASP.NET的特定部分



我正在尝试打印应用程序的特定部分。我的div。它在HTML单页中起作用,但在我的应用程序中不起作用。我的页面在ASP.NET和C#中,但是由于我无法将功能嵌入C#,因此我正在尝试使用JavaScript。但是,属性Inner.html

存在问题

错误:

0x800A138F -JavaScript运行时错误:无法获取未定义或null Reference的属性'InnerHtml'

代码:

<script type="text/javascript">
    function printdiv
        var prtContent = document.getElementById("specific");
        var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
</script>
<asp:Button ID="printButton" runat="server" Text="Print Results" OnClientClick="printdiv()"/>

我的页面在ASP中,也许是问题?我该如何等同于打印我想要的Div?

编辑,我不知道它是否相关:div的特定部分

使用以下代码。愉快的编码。

<!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>
    <title></title>
    <script type="text/javascript">
        function PrintDiv() {
            var contents = document.getElementById("dvContents").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head><title>DIV Contents</title>');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                document.body.removeChild(frame1);
            }, 500);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1">
    <span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Manoj Bhardwaj</span>
    <hr />
    <div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px">
        <span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
            <br />
            This is <span style="color: #18B5F0">a sample code my manoj</span>.<br />
            i hope this will help you to print a div</span>
    </div>
    <br />
    <input type="button" onclick="PrintDiv();" value="Print" />
    </form>
</body>
</html>

我希望这对您有帮助。

您必须将OnClientClick从OnClientClick =" printDiv()"更改为onclientClick =" return printDiv()"

所以您的代码假设

    <script type="text/javascript">
        function PrintDiv() {
            var contents = document.getElementById("dvContents").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head><title>DIV Contents</title>');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                document.body.removeChild(frame1);
            }, 500);
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Dharmin Shah</span>
        <hr />
        <div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px" runat="server">
            <span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
            <br />
                This is <span style="color: #18B5F0">Dharmin's sample code</span>.<br />
                i hope this will help you to print a div</span>
        </div>
        <br />
        <asp:Button ID="Button" runat="server" Text="Button" OnClientClick="return PrintDiv();" value="Print" />
    </form>
</body>

最新更新