Quantcast
Channel: Getting ReportViewer client parameters at runtime
Viewing all articles
Browse latest Browse all 20

Re: Getting ReportViewer client parameters at runtime

$
0
0

The values of the parameters are stored in the view state of the parameter controls. Unfortunately, as you mentioned, you don't have direct access to those controls at run time. However, you do have access to them through the Controls collection of the ReportViewer, if you know what to look for. The following code will extract the currently selected parameter values. NOTE: this code uses reflection and is dependent on the internal implementation of the parameter controls. This is necessary since the ReportViewer does not expose the values of these controls in any other way.

Public Function GetCurrentParameters(ByVal viewer As Microsoft.Reporting.WebForms.ReportViewer) As ReportParameter()
		Dim paramsArea As Control = FindParametersArea(viewer)
		Dim params As New List(Of ReportParameter)()
		FindParameters(paramsArea, params)
		Return params.ToArray()
	End Function


	Private Function FindParametersArea(ByVal viewer As Microsoft.Reporting.WebForms.ReportViewer) As Control
		For Each child As Control In viewer.Controls
			If child.GetType().Name = "ParametersArea" Then
				Return child
			End If
		Next
		Return Nothing
	End Function


	Private _ParameterControlType As Type = System.Reflection.Assembly.GetAssembly(GetType(Microsoft.Reporting.WebForms.ReportViewer)).GetType("Microsoft.Reporting.WebForms.ParameterControl")
	Private Sub FindParameters(ByVal parent As Control, ByVal params As List(Of ReportParameter))
		Dim param As ReportParameter
		Dim paramInfo As ReportParameterInfo
		Dim paramValues As String()
		For Each child As Control In parent.Controls
			If _ParameterControlType.IsAssignableFrom(child.GetType()) Then
				paramInfo = CType(GetPropertyValue(child, "ReportParameter"), ReportParameterInfo)
				If paramInfo Is Nothing Then Continue For
				paramValues = CType(GetPropertyValue(child, "CurrentValue"), String())
				If Not paramValues Is Nothing AndAlso paramValues.Length > 0 Then
					param = New ReportParameter()
					param.Name = paramInfo.Name
					param.Values.AddRange(paramValues)
					params.Add(param)
				End If
			End If
			FindParameters(child, params)
		Next
	End Sub


	Public Function GetPropertyValue(ByVal target As Object, ByVal propertyName As String) As Object
		Return target.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public).GetValue(target, Nothing)
	End Function
 

Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>