Wednesday, January 25, 2012

Disabling Export to Excel In Reportviewer


' In the Reportviewer Load call the CustomizeRV function
  Protected Sub ReportViewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ReportViewer1.Load
        CustomizeRV(CType(sender, System.Web.UI.Control))
    End Sub


  'CustomizeRV definition
  Private Sub CustomizeRV(ByVal reportControl As System.Web.UI.Control)
        For Each childControl As System.Web.UI.Control In reportControl.Controls
            If childControl.[GetType]() Is GetType(System.Web.UI.WebControls.DropDownList) Then
                Dim ddList As System.Web.UI.WebControls.DropDownList = DirectCast(childControl, System.Web.UI.WebControls.DropDownList)
                AddHandler ddList.PreRender, New EventHandler(AddressOf ddList_PreRender)
            End If
            If childControl.Controls.Count > 0 Then
                CustomizeRV(childControl)
            End If
        Next
    End Sub

    'Event handler definition
Private Sub ddList_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        If sender.[GetType]() Is GetType(System.Web.UI.WebControls.DropDownList) Then
            Dim ddList As System.Web.UI.WebControls.DropDownList = DirectCast(sender, System.Web.UI.WebControls.DropDownList)
            Dim listItems As System.Web.UI.WebControls.ListItemCollection = ddList.Items

            If (listItems IsNot Nothing) AndAlso (listItems.Count > 0) AndAlso (listItems.FindByText("Excel") IsNot Nothing) Then
                For Each list As System.Web.UI.WebControls.ListItem In listItems
                    If list.Text.Equals("Excel") Then
                        list.Enabled = False
                    End If
                Next
            End If
        End If
    End Sub