2015年12月25日 星期五

How-to-add-Cross-Browser-Printing-to-ASP-NET-ReportViewer-toolbar

http://www.neodynamic.com/articles/How-to-add-Cross-Browser-Printing-to-ASP-NET-ReportViewer-toolbar/

引用

In this article I will explain how to hide (disable) specific export option i.e. Word, Excel or PDF from the export button DropDown in RDLC (Local SSRS) ReportViewer control.
By default the RDLC (Local SSRS) ReportViewer control displays all the three export options i.e. Word, Excel or PDF and hence in order to hide a specific export option, one has to make use of Reflection.
 
Creating and populating RDLC Report using ReportViewer from Database
I have already explained how to create and populate RDLC Report using ReportViewer control from database in my following article.
 
Hide (Disable) specific export option (Word / Excel / PDF) from Export button DropDown
We will be using ReportViewer control’s OnLoad event handler and hence we need to assign an OnLoad event handler to the ReportViewer control as shown below.
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600" OnLoad="ReportViewer_OnLoad">
</rsweb:ReportViewer>
 
Then inside the event handler we will write code to hide the Export option in the export button DropDown.
All the export options in ReportViewer control are available through the ListRenderingExtentionsList method. From this list we need to find the extension of the Export option we wish to hide, thus I have done that by matching the extension name in the Lambda expression query.
Once the extension is found we need to access its m_isVisible property using Reflection and set it to false.
C#
protected void ReportViewer_OnLoad(object sender, EventArgs e)
{
    //string exportOption = "Excel";
    //string exportOption = "Word";
    string exportOption = "PDF";
    RenderingExtension extension = ReportViewer1.LocalReport.ListRenderingExtensions().ToList().Find(x => x.Name.Equals(exportOption, StringComparison.CurrentCultureIgnoreCase));
    if (extension != null)
    {
        System.Reflection.FieldInfo fieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        fieldInfo.SetValue(extension, false);
    }
}
 
VB.Net
Protected Sub ReportViewer_OnLoad(sender As Object, e As EventArgs)
    'string exportOption = "Excel";
    'string exportOption = "Word";
    Dim exportOption As String = "PDF"
    Dim extension As RenderingExtension = ReportViewer1.LocalReport.ListRenderingExtensions().ToList().Find(Function(x) x.Name.Equals(exportOption, StringComparison.CurrentCultureIgnoreCase))
    If extension IsNot Nothing Then
        Dim fieldInfo As System.Reflection.FieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
        fieldInfo.SetValue(extension, False)
    End If
End Sub
 
 
Screenshot
The following screenshot displays the Report Viewer control with the hidden PDF export option.
ASP.Net RDLC (Local SSRS) Report Viewer: Hide (Disable) specific export option (Word / Excel / PDF) from Export button

沒有留言:

張貼留言