简单应用ReportViewer控件
首先,将开发好的report部署到Reporting Service上。
1. 写一个类实现接口Microsoft.Reporting.WebForms.IReportServerCredentials,用于Reporting Service的验证,有1个方法,2个属性,根据运行环境来实现这个接口,我这里使用环境的Web程序,Reporting Service需要Window身份验证:
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
{
userName = String.Empty;
password = String.Empty;
authority = String.Empty;
authCookie = null;
return false;
}
public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}
public System.Net.ICredentials NetworkCredentials
{
get
{
return new NetworkCredential(userName, password, domain);
}
}
{
userName = String.Empty;
password = String.Empty;
authority = String.Empty;
authCookie = null;
return false;
}
public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}
public System.Net.ICredentials NetworkCredentials
{
get
{
return new NetworkCredential(userName, password, domain);
}
}
2. 在页面上添加ReportViewer控件,设置属性:
<rsweb:ReportViewer ID="ctl_rv_Report" runat="server" Font-Names="Verdana" Font-Size="8pt"
Height="400px" ProcessingMode="Remote" ShowToolBar="False" Width="100%" AsyncRendering="False" InternalBorderColor="Transparent" PromptAreaCollapsed="True" ShowParameterPrompts="False">
<ServerReport ReportPath="/Folder/Name" ReportServerUrl="http://site/reportserver" />
</rsweb:ReportViewer>
Height="400px" ProcessingMode="Remote" ShowToolBar="False" Width="100%" AsyncRendering="False" InternalBorderColor="Transparent" PromptAreaCollapsed="True" ShowParameterPrompts="False">
<ServerReport ReportPath="/Folder/Name" ReportServerUrl="http://site/reportserver" />
</rsweb:ReportViewer>
主要的是ProcessingMode="Remote",有两种模式,另外一种是Local。如果是Local,下面就是<LocalReport.....
另外重要的是ReportPath和ReportServerUrl,设置正确。
3. 代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReportCredential report = new ReportCredential();
ctl_rv_Report.ServerReport.ReportServerCredentials = report;
IList<ReportParameter> parms = new List<ReportParameter>();
ReportParameter p = new ReportParameter("LocationsLocationID", "[Locations].[Location ID].&[" + LocationID + "]");
parms.Add(p);
ctl_rv_Report.ServerReport.SetParameters(parms);
}
}
{
if (!IsPostBack)
{
ReportCredential report = new ReportCredential();
ctl_rv_Report.ServerReport.ReportServerCredentials = report;
IList<ReportParameter> parms = new List<ReportParameter>();
ReportParameter p = new ReportParameter("LocationsLocationID", "[Locations].[Location ID].&[" + LocationID + "]");
parms.Add(p);
ctl_rv_Report.ServerReport.SetParameters(parms);
}
}
设置验证,report是实现了接口的类的一个实例,
ReportCredential report = new ReportCredential();
ctl_rv_Report.ServerReport.ReportServerCredentials = report;
ctl_rv_Report.ServerReport.ReportServerCredentials = report;
添加参数,根据report service里的report情况添加参数:
IList<ReportParameter> parms = new List<ReportParameter>();
ReportParameter p = new ReportParameter("LocationsLocationID", "[Locations].[Location ID].&[" + LocationID + "]");
parms.Add(p);
ctl_rv_Report.ServerReport.SetParameters(parms);
ReportParameter p = new ReportParameter("LocationsLocationID", "[Locations].[Location ID].&[" + LocationID + "]");
parms.Add(p);
ctl_rv_Report.ServerReport.SetParameters(parms);
可以在某个postback后设置参数,比如点击button,dropdownlist selectedindexchanged事件,只有设置了参数后,reportViewer控件才会将RS中的结果显示在页面上了。