Http://lwl0606.cmszs.com/archives/asp-net-print-reportviewer-rdlc.html
The new control ReportViewer in. net 2.0 can easily create and display reports, but it does not directly support printing on webpages. On the basis of analyzing the HTML source code of the webpage, I found the tip of direct printing. First, I made a function to facilitate direct use.
1. In the final html dom structure of the webpage that contains the ReportViewer report, the report is put into a <iframe>, and its id is named as "ReportFrame" + report Control id;
2. The report content is put into another <iframe> in 1, and its id is fixed as: "report ";
3. for printing, we only need to get the content <iframe> object, set the focus, and then call the print method to print the content.
4. The encapsulated javascript functions are as follows:
// JScript File
// To print the content of the ReportView report, you only need to reference this file and then call the PrintReportView () function.
// For example, the Code is included in the Click Event of a button. onclick = "PrintReportView (window, 'reportviewerysqd ');"
// Obtain the FRAME object in the report content area of the client code generated by the ReportView control.
// Parameter: objWindow -- window object containing the ReportView Control
// StrReportViewerId -- ID of the ReportViewer control to be printed
// Return: The returned FRAME object in the report content area. If the result fails to be obtained, null is returned.
Function GetReportViewContentFrame (objWindow, strReportViewerId)
{
Var frmContent = null; // FRAME object of the report content area object
Var strFrameId = "ReportFrame" + strReportViewerId; // The iframe id automatically generated by asp.net is: ReportFrame + report Control id
Try
{
FrmContent = window. frames [strFrameId]. frames ["report"]; // The report content framework id is report
}
Catch (e)
{
}
Return frmContent;
}
// Print the report content in the ReportView Control
// Parameter: objWindow -- window object containing the ReportView Control
// StrReportViewerId -- ID of the ReportViewer control to be printed
// Return: (none)
Function PrintReportView (objWindow, strReportViewerId)
{
Var frmContent = GetReportViewContentFrame (objWindow, strReportViewerId );
If (frmContent! = Null & frmContent! = Undefined)
{
FrmContent. focus ();
FrmContent. print ();
}
Else
{
Alert ("failed to get report content and cannot be printed by program. To manually print a report, right-click the report content area and select a print item from the menu. ");
}
}
5. The following is a complete example of a test and print report:
<% @ Page Language = "C #" %>
<% @ Register Assembly = "Microsoft. ReportViewer. WebForms, Version = 8.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"
Namespace = "Microsoft. Reporting. WebForms" TagPrefix = "rsweb" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Rsweb: ReportViewer ID = "rvYhqd" runat = "server" Font-Names = "Verdana" Font-Size = "8pt" Height = "484px" Width = "718px">
<LocalReport ReportPath = "yyhqd. rdlc">
</LocalReport>
</Rsweb: ReportViewer>
<Br/>
<Asp: Button ID = "Button1" runat = "server" Text = "print" OnClientClick = "return PrintReport ();"/>
</Div>
</Form>
<! -- Reference the js file containing the print report function here -->
<Script language = "javascript" type = "text/javascript" src = "ReportView. js"> </script>
<Script language = "javascript" type = "text/javascript">
<! --
// Print the report
Function PrintReport ()
{
PrintReportView (window, "rvYhqd ");
Return false;
}
// -->
</Script>
</Body>
</Html>