I make use of a ReportView component in a VB.Net 2005 app. How can I disable the PDF export functionality, only keeping the MS Excel format?
Regards.
I managed to disable the PDF Export button with some tinkering. The ReportViewer class does not have any public facing functions to disable the Export to PDF toolbar button. In order to do it, take a look at the following code:
Call this function during the OnLoad event of your reportViewer page:
Private Sub CustomizeRV(ByVal ctrl As Control)
For Each c As Control In ctrl.Controls
If TypeOf c Is ToolStrip Then
Dim ts As ToolStrip = DirectCast(c, ToolStrip)
For i As Integer = 0 To ts.Items.Count - 1
If ts.Items(i).Name = "export" Then
Dim exp As ToolStripDropDownButton = ts.Items(i)
AddHandler exp.DropDownOpening, AddressOf disableButton
End If
Next
End If
If c.HasChildren Then
CustomizeRV(c)
End If
Next
End Sub
I couldn't set the Visible property of the toolstrip button here, since the Export options are loaded OnDropDownOpened. Instead, I added a handler to take care of disabling the export option when the toolstrip Dropdown is clicked. The handler function is as follows:
Private Sub disableButton(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As ToolStripDropDownButton = DirectCast(sender, ToolStripDropDownButton)
btn.DropDownItems(1).Visible = False
End Sub
So basically, Onload you are adding an Event Handler so that when the Export Drop Down button is clicked, the above function will run - making the Export to PDF invisible.
The solution will work for sure, I just finished making it work.
If you have any questions, let me know.
Jquery solution for reportviewer 2010: Put this in the aspx file containing the reportviewer control (Assuming your reportviewer is called ReportViewer1)
<script type="text/javascript">
$(document).ready(function () {
hideExportOptions();
});
function hideExportOptions() {
//Find the menu id by getting the parent of the parent of one of the export links
var menuID = $("a[onclick=\"$find('ReportViewer1').exportReport('PDF');\"]").parent().parent().attr("id");
if ($("#" + menuID).length > 0) {
$("#" + menuID + " div:nth-child(3)").css('display', 'none');
}
else {
setTimeout("hideExportOptions()", 1000);
}
}
</script>
It waits till the dropdown is rendered, then hides the chosen option. Typically the setTimeout only occurs only once. You can hide more/others by adding more nth-childs, the number being the 1-based position in the dropdown of the option you want to hide.
For ReportViewer >2010 i use this aproach made with jQuery
function HideExtension(ext) {
var $reportViewer = $("[id*=ReportViewer1]");
var $botons = $reportViewer.find("a");
$botons.each(function (index,element) {
if($(element).html()==ext)
{
$(element).parent().css("display", "none");
}
});
}
Just change the selector for your own and call the function from $(document).ready(function(){//here})
public void DisableUnwantedExportFormats()
{
FieldInfo info;
foreach (RenderingExtension extension in reportViewer.ServerReport.ListRenderingExtensions())
{
if (extension.Name != "PDF" && extension.Name != "EXCEL") // only PDF and Excel - remove the other options
{
info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
info.SetValue(extension, false);
}
if (extension.Name == "EXCEL") // change "Excel" name on the list to "Excel 97-2003 Workbook"
{
info = extension.GetType().GetField("m_localizedName", BindingFlags.Instance | BindingFlags.NonPublic);
if (info != null) info.SetValue(extension, "Excel 97-2003 Workbook");
}
}
}
I've tried by adding above said method DisableUnwantedExportFormats()
for hiding Export to Excel option. When report loaded first time the Excel option not getting visible.
However, When I used to call the same method inside of Drillthrough()
event "Excel" & PDF option getting visible in Export controls dropdown. I've tried by calling your method in the first statement of my Drillthrough()
event (like what I used in Page load method).
Please let me know, How can I hide the excel option in Drillthrough()
event of Reportviewer.
If you're interested in a quick javascript solution using jQuery ..
Simply replace the reportViewer selector below with your dropdown ID.
jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').children().remove(); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("- Select export format -"); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("EXCEL");
This removes all options and then adds back in EXCEL as the only option.