在winfrom中用reportview显示reporting service报表,该怎么处理
展开全部
The Report Viewer Page
All report prompts will be created dynamically in the page load
event. Therefore, we only need to put the placeholders that will host
them in the web page. Here is the HTML code:
Hide Copy Code
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="ReportViewer.aspx.vb"
Inherits="SQLRS_ReportViewer.ReportViewer"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ReportViewer</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="get" runat="server">
<input type="hidden" name="Report" id="Report" runat="server">
<TABLE id="TablePrompts" runat="server"
class="MenuBarBkGnd ParamsGrid" width="550"></TABLE>
<div id="ReportPlaceholder" runat ="server"></div>
</form>
</body>
</HTML>
The Code Behind
First, we call GetReportParameters method to get a list of parameters. The ForRendering parameter is set to be False.
Hide Copy Code
reportParametersArray =
rs.GetReportParameters(reportPath, Nothing, False, Nothing, Nothing)
If there are any parameters associated with the report, we'll create a dynamic array of ParameterValue
to collect their values. They may come from three sources: URL
parameters, previous page submission, or inside report definition.
Parameters with blank values are not stored in this array. We use this
array to call GetReportParameters method again, this time specifying ForRendering parameter to be True.
This will retrieve a list of available values based on the given
parameter values, as well as the status for each parameter, which can
be: HasValidValue, HasOutstandingDependencies, DynamicValuesUnavailable or MissingValidValue.
Hide Shrink Copy Code
'hash table with dependecies
Dim Dependencies As New System.Collections.Hashtable
'we need a dynamic array for storing parameter values
Dim reportParameterValues As ArrayList
If reportParametersArray.Length > 0 Then
reportParameterValues = New ArrayList
'loop over each parameter and build the array of values
For Each parameter As ReportParameter In reportParametersArray
Dim value As ParameterValue = New ParameterValue
value.Name = parameter.Name
value.Label = parameter.Name
'if the report value is coming from URL or previous submission
'then it is part of the Request
If Not Request(Parameter.Name) Is Nothing Then
'set the value from URL or submission
value.Value = Request(Parameter.Name)
ElseIf Not Parameter.DefaultValues Is Nothing Then
'set the value to the one defined inside report
value.Value = Parameter.DefaultValues(0)
Else
'in this case we dont have a value for this parameter
'we cannot render this report
value.Value = ""
reportCanBeRendered = False
End If
If value.Value <> "" Then
reportParameterValues.Add(value)
End If
'check if this parameter is dependent of another one
If Not (parameter.Dependencies Is Nothing) Then
For Each d As String In parameter.Dependencies
'add the parameter as a dependecy if was not already added
If Not Dependencies.Contains(d) Then
Dependencies.Add(d, Nothing)
End If
Next
End If
Next 'from: For Each Parameter As ReportParameter In Parameters
'Get the list of parameters this time specifying to
'render the report (ForRender set to true)
'valid values for these parameters will be populated
reportParametersArray =
rs.GetReportParameters(reportPath, Nothing, True, _
reportParameterValues.ToArray(GetType(ParameterValue)), Nothing)
Then I loop again over the parameters array and build dynamically the
HTML for prompts. I use HTML Controls rather than ASP Web Controls
because in this way I can capture the values more easily when the form
is submitted. Inside the loop, it'll be determined whether the report
can be rendered based on the value of the Parameter.State property. Some parameters may depend on other parameters and their valid values may not be specified yet.
Hide Shrink Copy Code
Dim i As Integer = 0
For Each parameter As ReportParameter In reportParametersArray
' if any of parameters doesnt have valid values the
' report cannot be rendered
If parameter.State <> ParameterStateEnum.HasValidValue Then
reportCanBeRendered = False
End If
If parameter.DefaultValues(0) =
"" And parameter.AllowBlankSpecified And Not parameter.AllowBlank Then
reportCanBeRendered = False
End If
If parameter.DefaultValues(0) Is Nothing And _
parameter.NullableSpecified And Not parameter.Nullable Then
reportCanBeRendered = False
End If
If i Mod 2 = 0 Then
TR = New System.Web.UI.HtmlControls.HtmlTableRow
End If
'set the propmt text
TD = New System.Web.UI.HtmlControls.HtmlTableCell
TD.InnerHtml = Parameter.Prompt
TR.Controls.Add(TD)
'set the prompt input control
TD = New System.Web.UI.HtmlControls.HtmlTableCell
Dim promptType As String = "textbox"
If Parameter.ValidValuesQueryBased Then
promptType = "dropdown"
End If
If Not parameter.ValidValues Is Nothing Then
If parameter.ValidValues.Length > 1 Then
promptType = "dropdown"
End If
End If
Select Case promptType
Case "dropdown"
'in this case we set a drop down select list
S = New System.Web.UI.HtmlControls.HtmlSelect
S.ID = Parameter.Name
S.Name = Parameter.Name
If Not Parameter.ValidValues Is Nothing _
And Parameter.ValidValues.Length > 0 Then
If Parameter.State =
ParameterStateEnum.MissingValidValue Or
Parameter.State =
ParameterStateEnum.HasOutstandingDependencies Then
reportCanBeRendered = False
LI =
New System.Web.UI.WebControls.ListItem("<Select a Value>", "")
S.Items.Add(LI)
End If
For Each vv As ValidValue In Parameter.ValidValues
LI = New System.Web.UI.WebControls.ListItem(vv.Label, vv.Value)
'see if this value is the same with the default value
'in which case we make the current list item selected
If vv.Value = parameter.DefaultValues(0) And Parameter.State =
ParameterStateEnum.HasValidValue Then
LI.Selected = True
End If
S.Items.Add(LI)
Next
End If
'check if this parameter have dependencies.
'If it does then we need to reload the page to
'reload the value of dependent params
If Dependencies.Contains(Parameter.Name) Then
S.Attributes.Add("OnChange", "this.form.submit();return true")
End If
TD.Controls.Add(S)
Case "textbox"
'in this case we set an input text box
T = New System.Web.UI.HtmlControls.HtmlInputText
T.ID = Parameter.Name
T.Name = Parameter.Name
If parameter.DefaultValues.Length > 0 Then
T.Value = Parameter.DefaultValues(0)
End If
TD.Controls.Add(T)
End Select
TR.Controls.Add(TD)
'add this row to the table
If i Mod 2 = 1 Then
TablePrompts.Controls.Add(TR)
End If
i += 1
Next 'from: For Each Parameter As ReportParameter In Parameters
If i Mod 2 = 1 Then
TablePrompts.Controls.Add(TR)
End If
End If
The last step is to render the report according to the format parameter and display it in the page. If the format is HTML4.0 or HTML3.2, then we build a specific DeviceInfo parameter. StreamRoot parameter should point to GetImage.aspx page. This is very important:
All report prompts will be created dynamically in the page load
event. Therefore, we only need to put the placeholders that will host
them in the web page. Here is the HTML code:
Hide Copy Code
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="ReportViewer.aspx.vb"
Inherits="SQLRS_ReportViewer.ReportViewer"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ReportViewer</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="get" runat="server">
<input type="hidden" name="Report" id="Report" runat="server">
<TABLE id="TablePrompts" runat="server"
class="MenuBarBkGnd ParamsGrid" width="550"></TABLE>
<div id="ReportPlaceholder" runat ="server"></div>
</form>
</body>
</HTML>
The Code Behind
First, we call GetReportParameters method to get a list of parameters. The ForRendering parameter is set to be False.
Hide Copy Code
reportParametersArray =
rs.GetReportParameters(reportPath, Nothing, False, Nothing, Nothing)
If there are any parameters associated with the report, we'll create a dynamic array of ParameterValue
to collect their values. They may come from three sources: URL
parameters, previous page submission, or inside report definition.
Parameters with blank values are not stored in this array. We use this
array to call GetReportParameters method again, this time specifying ForRendering parameter to be True.
This will retrieve a list of available values based on the given
parameter values, as well as the status for each parameter, which can
be: HasValidValue, HasOutstandingDependencies, DynamicValuesUnavailable or MissingValidValue.
Hide Shrink Copy Code
'hash table with dependecies
Dim Dependencies As New System.Collections.Hashtable
'we need a dynamic array for storing parameter values
Dim reportParameterValues As ArrayList
If reportParametersArray.Length > 0 Then
reportParameterValues = New ArrayList
'loop over each parameter and build the array of values
For Each parameter As ReportParameter In reportParametersArray
Dim value As ParameterValue = New ParameterValue
value.Name = parameter.Name
value.Label = parameter.Name
'if the report value is coming from URL or previous submission
'then it is part of the Request
If Not Request(Parameter.Name) Is Nothing Then
'set the value from URL or submission
value.Value = Request(Parameter.Name)
ElseIf Not Parameter.DefaultValues Is Nothing Then
'set the value to the one defined inside report
value.Value = Parameter.DefaultValues(0)
Else
'in this case we dont have a value for this parameter
'we cannot render this report
value.Value = ""
reportCanBeRendered = False
End If
If value.Value <> "" Then
reportParameterValues.Add(value)
End If
'check if this parameter is dependent of another one
If Not (parameter.Dependencies Is Nothing) Then
For Each d As String In parameter.Dependencies
'add the parameter as a dependecy if was not already added
If Not Dependencies.Contains(d) Then
Dependencies.Add(d, Nothing)
End If
Next
End If
Next 'from: For Each Parameter As ReportParameter In Parameters
'Get the list of parameters this time specifying to
'render the report (ForRender set to true)
'valid values for these parameters will be populated
reportParametersArray =
rs.GetReportParameters(reportPath, Nothing, True, _
reportParameterValues.ToArray(GetType(ParameterValue)), Nothing)
Then I loop again over the parameters array and build dynamically the
HTML for prompts. I use HTML Controls rather than ASP Web Controls
because in this way I can capture the values more easily when the form
is submitted. Inside the loop, it'll be determined whether the report
can be rendered based on the value of the Parameter.State property. Some parameters may depend on other parameters and their valid values may not be specified yet.
Hide Shrink Copy Code
Dim i As Integer = 0
For Each parameter As ReportParameter In reportParametersArray
' if any of parameters doesnt have valid values the
' report cannot be rendered
If parameter.State <> ParameterStateEnum.HasValidValue Then
reportCanBeRendered = False
End If
If parameter.DefaultValues(0) =
"" And parameter.AllowBlankSpecified And Not parameter.AllowBlank Then
reportCanBeRendered = False
End If
If parameter.DefaultValues(0) Is Nothing And _
parameter.NullableSpecified And Not parameter.Nullable Then
reportCanBeRendered = False
End If
If i Mod 2 = 0 Then
TR = New System.Web.UI.HtmlControls.HtmlTableRow
End If
'set the propmt text
TD = New System.Web.UI.HtmlControls.HtmlTableCell
TD.InnerHtml = Parameter.Prompt
TR.Controls.Add(TD)
'set the prompt input control
TD = New System.Web.UI.HtmlControls.HtmlTableCell
Dim promptType As String = "textbox"
If Parameter.ValidValuesQueryBased Then
promptType = "dropdown"
End If
If Not parameter.ValidValues Is Nothing Then
If parameter.ValidValues.Length > 1 Then
promptType = "dropdown"
End If
End If
Select Case promptType
Case "dropdown"
'in this case we set a drop down select list
S = New System.Web.UI.HtmlControls.HtmlSelect
S.ID = Parameter.Name
S.Name = Parameter.Name
If Not Parameter.ValidValues Is Nothing _
And Parameter.ValidValues.Length > 0 Then
If Parameter.State =
ParameterStateEnum.MissingValidValue Or
Parameter.State =
ParameterStateEnum.HasOutstandingDependencies Then
reportCanBeRendered = False
LI =
New System.Web.UI.WebControls.ListItem("<Select a Value>", "")
S.Items.Add(LI)
End If
For Each vv As ValidValue In Parameter.ValidValues
LI = New System.Web.UI.WebControls.ListItem(vv.Label, vv.Value)
'see if this value is the same with the default value
'in which case we make the current list item selected
If vv.Value = parameter.DefaultValues(0) And Parameter.State =
ParameterStateEnum.HasValidValue Then
LI.Selected = True
End If
S.Items.Add(LI)
Next
End If
'check if this parameter have dependencies.
'If it does then we need to reload the page to
'reload the value of dependent params
If Dependencies.Contains(Parameter.Name) Then
S.Attributes.Add("OnChange", "this.form.submit();return true")
End If
TD.Controls.Add(S)
Case "textbox"
'in this case we set an input text box
T = New System.Web.UI.HtmlControls.HtmlInputText
T.ID = Parameter.Name
T.Name = Parameter.Name
If parameter.DefaultValues.Length > 0 Then
T.Value = Parameter.DefaultValues(0)
End If
TD.Controls.Add(T)
End Select
TR.Controls.Add(TD)
'add this row to the table
If i Mod 2 = 1 Then
TablePrompts.Controls.Add(TR)
End If
i += 1
Next 'from: For Each Parameter As ReportParameter In Parameters
If i Mod 2 = 1 Then
TablePrompts.Controls.Add(TR)
End If
End If
The last step is to render the report according to the format parameter and display it in the page. If the format is HTML4.0 or HTML3.2, then we build a specific DeviceInfo parameter. StreamRoot parameter should point to GetImage.aspx page. This is very important:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询