Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
Here my problem is i design one page for printing in asp but when i load that page it loaded with css but when i try to print that page in javascript css not loaded

here my code

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <link rel="shortcut icon" href="Images\abileweb.ico" />
    <%--<meta charset="utf-8">--%>
        <link rel="stylesheet" href="css/invoice.css">
    <style type="text/css">
        .style1{width: 100%;}
        </style>
      <script type = "text/javascript">
        function PrintPanel() {
            var panel = document.getElementById("<%=panelprint.ClientID %>");
            var printWindow = window.open('', '', '');
            printWindow.document.write('<html><head><title>Print Invoice</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;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="btnprint" runat="server" Text="print" OnClientClick="PrintPanel();" CssClass="button2"/>
         <asp:Panel ID="panelprint" runat="server">
    <div>
    <header>
            <h1>Invoice</h1>

            <address>
            <p style=" font-family:Arial; font-weight:bold; font-size:medium;">Abile Business Solutions Pvt Ltd </p>
                <p>&nbsp;A 1/3 Kush Kumar Road,Nungambakkam, Chennai, Tamilnadu, India</p>
                <p>&nbsp;Phone No : +91-44-44117595</p>
            </address>
            <span><img alt="" src="Images/Abillog.jpg"></span>
        </header>
        <article>
        <address>
        <div style=" line-height:16px">
        <p style="font-family:Arial; font-size:small;">&nbsp;<asp:Label ID="lblname" runat="server"></asp:Label></p>
            <p style="font-size:12px; font-weight:normal;">&nbsp;<asp:Label ID="lbladdress" runat="server"></asp:Label></p>
            <p style="font-size:12px; font-weight:normal;"><asp:Label ID="lblphone" runat="server"></asp:Label></p>
            </div>
        </address>
            <table class="meta">
                <tr>
                    <th>Invoice #</span></th>
                    <td><span contenteditable> <asp:Label ID="lblinvoiceno" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></span></td>
                </tr>
                <tr>
                    <th><span>Date</span></th>
                    <td><span contenteditable><asp:Label ID="lbldate" runat="server"></asp:Label></span></td>
                </tr>
            </table>
            <table class="inventory">
            <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
            <thead>
                    <tr>
                        <th><span contenteditable>Qty</span></th>
                        <th><span contenteditable>Description</span></th>
                        <th><span contenteditable>Unit Price</span></th>
                        <th><span contenteditable>Total</span></th>
                    </tr>
                </thead>
            </HeaderTemplate>
            <ItemTemplate>
            <tbody>
                    <tr>
                        <td><span contenteditable><%#Eval("quantity") %></span></td>
                        <td><span contenteditable style=" font-weight:bold;"><%#Eval("category") %></span><br />
                        <span contenteditable>&nbsp;&nbsp;Sno: <%#Eval("serialnumber")%></span></td>
                        <td><span data-prefix><%#Eval("price") %></span></span></td>
                        <td><span data-prefix><%#Eval("tot") %></span></td>
                    </tr>
                </tbody>
            </ItemTemplate>
            </asp:Repeater>
            </table>
            <table class="balance">
                <tr>
                    <th><span>Discount</span></th>
                    <td><span data-prefix><asp:Label ID="lbldiscount" runat="server"></asp:Label></span></td>
                </tr>
                <tr>
                    <th><span>Sub Total</span></th>
                    <td><span data-prefix><asp:Label ID="lblsubtotal" runat="server"></asp:Label></span></td>
                </tr>
                <tr>
                    <th><span contenteditable>Tax Rate</span></th>
                    <td><span data-prefix></span><span style="float:left"><asp:Label ID="lbltax" runat="server"></asp:Label></span>
                    <span>Rs.<asp:Label ID="lbltaxamount" runat="server"></asp:Label></span>
                    </td>
                </tr>
                <tr>
                    <th><span contenteditable>Grand Total</span></th>
                    <td><span data-prefix> <asp:Label ID="lblgrandtotal" runat="server" Font-Bold="True"></asp:Label></span></td>
                </tr>
            </table>
        </article>
    </div>
    <div class="Invoice_Footer">
        <table class="style1" style="border-style:dotted;">
            <tr>
                <td colspan="2" style="background:#F7F7F7;"><p style="font-weight:bold; margin:0 3.2in;">Bank Details</p></td>
                </tr>
            <tr>
                <th><span>Account Name</span>
                <td>
                    <asp:Label ID="lblaccountname" runat="server"></asp:Label>
                </td>
            </tr>
            <tr><th><span>Account Number</span></th>
            <td>
                <asp:Label ID="lblaccountnumber" runat="server"></asp:Label>
                    </td>
            </tr>
             <tr><th><span>Bank</span></th>
                <td>
                    <asp:Label ID="lblbank" runat="server"></asp:Label>
                    </td>
            </tr>
             <tr>
             <th><span>Branch Details</span></th>
                <td>
                    <asp:Label ID="lblbranchdetails" runat="server"></asp:Label>
                    </td>
            </tr>
              <tr>
              <th><span>SWFT Code</span></th>
                <td>
                    <asp:Label ID="lblifsccode" runat="server"></asp:Label>
                    </td>
            </tr>
              <tr>
                <td colspan="2" style="background:#F7F7F7;"><p style="font-weight:bold; margin:0 2.9in;">Terms and Conditions</p></td>
            </tr>
              <tr>
                <td colspan="2">1. <asp:Label ID="lblterms1" runat="server"></asp:Label>
                </td>
            </tr>
              <tr>
                <td colspan="2">2. <asp:Label ID="lblterms2" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <br />
        <hr class="linehr" />
        <br />
        <p align="center" style="font-size:small;">Thank You For Your Business!</p>
    </div>
    </asp:Panel>
    </form>
</body>
</html>
Posted
Updated 13-Jul-22 20:23pm

1 solution

Your javascript function is opening a new window, and creating a new document which doesn't contain any link to your stylesheet.

Update your function so that it adds the relevant stylesheet link to the new document:
JavaScript
function PrintPanel() {
    var panel = document.getElementById("<%=panelprint.ClientID %>");
    var printWindow = window.open('', '', '');
    printWindow.document.write('<html><head><title>Print Invoice</title>');
    
    // Make sure the relative URL to the stylesheet works:
    printWindow.document.write('<base href="' + location.origin + location.pathname + '">');
    
    // Add the stylesheet link and inline styles to the new document:
    printWindow.document.write('<link rel="stylesheet" href="css/invoice.css">');
    printWindow.document.write('<style type="text/css">.style1{width: 100%;}</style>');
    
    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;
}</base>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900