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?
I had the same problem. I could get the unwanted export options to hide when the report rendered, but it didn't work in the case of a drillthrough report. The following code worked for both the parent and drillthrough reports, using a LocalReport:
private void SuppressExportButton(ReportViewer rv, string optionToSuppress)
{
var reList = rv.LocalReport.ListRenderingExtensions();
foreach (var re in reList)
{
if (re.Name.Trim().ToUpper() == optionToSuppress.Trim().ToUpper()) // Hide the option
{
re.GetType().GetField("m_isVisible", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(re, false);
}
}
}
The trick is to call the method from the page PreRender method:
protected void Page_PreRender(object sender, System.EventArgs e)
{
SuppressExportButton(rvMain, "PDF");
SuppressExportButton(rvMain, "Word");
}
Using the jon's code above as a reference, I manage to hide "Excel" in the program at runtime. However, I am not good a VB.net so I put a sample in C#. Sorry about that but I hope this helps. One more thing, the report the embedded inside an ASP.net page.
// This is the Load event of the reports itself.
// Call the recursive method.
protected void ReportViewerResults_Load(object sender, EventArgs e)
{
CustomizeRV((System.Web.UI.Control)sender);
}
// Patterned from Jon.
// Traverse all controls/child controls to get the dropdownlist.
// The first dropdown list is the ZoomGroup, followed by the ExportGroup.
// We just wanted the ExportGroup.
// When a dropdownlist is found, create a event handler to be used upon rendering.
private void CustomizeRV(System.Web.UI.Control reportControl)
{
foreach (System.Web.UI.Control childControl in reportControl.Controls)
{
if (childControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)childControl;
ddList.PreRender += new EventHandler(ddList_PreRender);
}
if (childControl.Controls.Count > 0)
{
CustomizeRV(childControl);
}
}
}
// This is the event handler added from CustomizeRV
// We just check the object type to get what we needed.
// Once the dropdownlist is found, we check if it is for the ExportGroup.
// Meaning, the "Excel" text should exists.
// Then, just traverse the list and disable the "Excel".
// When the report is shown, "Excel" will no longer be on the list.
// You can also do this to "PDF" or if you want to change the text.
void ddList_PreRender(object sender, EventArgs e)
{
if (sender.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)sender;
System.Web.UI.WebControls.ListItemCollection listItems = ddList.Items;
if ((listItems != null) && (listItems.Count > 0) && (listItems.FindByText("Excel") != null))
{
foreach (System.Web.UI.WebControls.ListItem list in listItems)
{
if (list.Text.Equals("Excel"))
{
list.Enabled = false;
}
}
}
}
}
I was trying to select the default item to "PDF" but could not find a way to enable the "Export" text button. :-(
After 4 hours of search I found the solution. I made some little changes to marol's code to be more little:
Control ReportViewerControl = ReportViewer1.FindControl("Ctl01");
Control ExportGroupControl = ReportViewerControl.FindControl("Ctl05");
DropDownList DropDownControl = (DropDownList)ExportGroupControl.FindControl("Ctl00");
DropDownControl.PreRender += new EventHandler(ddList_PreRender);
This is how you disable a export option, just mark all the ones except Excel to false.
*Don't forget to restart the Reporting Services service.
File: InstallPath\Reporting Services\ReportServer\rsreportserver.config
Enabled:
<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"/>
Disabled:
<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"
Visible="false"/>
If it helps... the code to hide the excel item em VB.Net (.Net 3.5)
Private Sub CustomizeRV(ByVal ctrl As ReportViewer)
For Each c As Control In ctrl.Controls
If c.GetType.ToString = "Microsoft.Reporting.WebForms.ToolbarControl" Then
For Each ct In c.Controls
If ct.GetType.ToString = "Microsoft.Reporting.WebForms.ExportGroup" Then
Dim cbo As DropDownList = CType(ct.controls(0), DropDownList)
AddHandler cbo.PreRender, AddressOf cboExportReportViewer_PreRender
End If
Next
End If
Next
End Sub
Protected Sub cboExportReportViewer_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cbo = CType(sender, DropDownList)
For i As Integer = 0 To cbo.Items.Count - 1
If cbo.Items(i).Text.ToLower = "excel" Then
cbo.Items.Remove(cbo.Items(i))
Exit Sub
End If
Next
End Sub
...
and put a call to CustomizeRV(ReportViewer1)
in the page_load
event
I've managed to do this on the client side using JavaScript at the bottom of the page.
var exportSelectBox = document.getElementById("ReportViewer1__ctl1__ctl5__ctl0");
exportSelectBox.remove(7);
exportSelectBox.remove(6);
exportSelectBox.remove(5);
exportSelectBox.remove(4);
exportSelectBox.remove(1);
exportSelectBox.remove(1);