Solved

Cross browser printing

Posted on 2014-09-17
3
230 Views
Last Modified: 2014-09-18
Hi Experts,

I am attempting to following this post http://www.codicode.com/art/cross_browser_printing_with_asp_net_reportviewer.aspx on cross browser printing.  I keep getting an error when I run the debugger.  

JavaScript runtime error: '$' is undefined and highlighting this code

$('#printreport').click(function () {
    printReport('rv1');
});

I'm not very familiar with javascript.

// Print function (require the reportviewer client ID)
function printReport(report_ID) {
    var rv1 = $('#' + report_ID);
    var iDoc = rv1.parents('html');

    // Reading the report styles
    var styles = iDoc.find("head style[id$='ReportControl_styles']").html();
    if ((styles == undefined) || (styles == '')) {
        iDoc.find('head script').each(function () {
            var cnt = $(this).html();
            var p1 = cnt.indexOf('ReportStyles":"');
            if (p1 > 0) {
                p1 += 15;
                var p2 = cnt.indexOf('"', p1);
                styles = cnt.substr(p1, p2 - p1);
            }
        });
    }
    if (styles == '') { alert("Cannot generate styles, Displaying without styles.."); }
    styles = '<style type="text/css">' + styles + "</style>";

    // Reading the report html
    var table = rv1.find("div[id$='_oReportDiv']");
    if (table == undefined) {
        alert("Report source not found.");
        return;
    }

    // Generating a copy of the report in a new window
    var docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">';
    var docCnt = styles + table.parent().html();
    var docHead = '<head><title>Printing ...</title><style>body{margin:5;padding:0;}</style></head>';
    var winAttr = "location=yes, statusbar=no, directories=no, menubar=no, titlebar=no, toolbar=no, dependent=no, width=720, height=600, resizable=yes, screenX=200, screenY=200, personalbar=no, scrollbars=yes";;
    var newWin = window.open("", "_blank", winAttr);
    writeDoc = newWin.document;
    writeDoc.open();
    writeDoc.write(docType + '<html>' + docHead + '<body onload="window.print();">' + docCnt + '</body></html>');
    writeDoc.close();

    // The print event will fire as soon as the window loads
    newWin.focus();
    // uncomment to autoclose the preview window when printing is confirmed or canceled.
    // newWin.close();
};

Open in new window


@ Page Title="" Language="VB" MasterPageFile="../MasterPage.master" AutoEventWireup="false" CodeFile="ContributionReport.aspx.vb" Inherits="Accounting_ContributionReport" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

    <script type="text/javascript">
    // Linking the print function to the print button
$('#printreport').click(function () {
    printReport('rv1');
});
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main_container" Runat="Server">
    <h1>Contribution Report</h1>
    <asp:Button ID="Button1" runat="server" Text="Button" 
        onclientclick="printReport()" />    
    <table style="width: 100%;">
        </table>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
        Font-Size="8pt" Height="600px" ProcessingMode="Remote" Width="800px">
        <ServerReport ReportPath="/POSReports/ContributionReport" 
            ReportServerUrl="http://localhost:80/reportserver" />
    </rsweb:ReportViewer>
</asp:Content>

Open in new window

0
Comment
Question by:rcowen00
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
You normally have to load jQuery before you use '$'.  One of the comments on that page says it doesn't work with a Master Page.
0
 

Author Comment

by:rcowen00
Comment Utility
Ok, I will give it a try without the master page.
0
 

Author Comment

by:rcowen00
Comment Utility
That did it. Thank you!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now