简体   繁体   English

如何将参数传递给在ASP.NET Web应用程序中具有对象数据源的本地报表?

[英]How to pass parameters to a Local report that has object data source in asp.net web application?

I want to pass parameters to local reports that have Object Data source(gets list of data from c#method that needs parameters) , how can I do that ? 我想将参数传递给具有对象数据源的本地报表(从需要参数的c#方法获取数据列表),该怎么办?

    #region Parameterized Report
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            string FolderPath = Server.MapPath("~/LocalReports");
            string ReportName = "ParameterizedReportWizard.rdlc";
            string DataSetName = "DataSet1Parameter";
            string SelectedMethod = "GetSomeEmployees";
            string Parameter = "ID";

            ReportViewer1.LocalReport.ReportPath = Path.Combine(FolderPath, ReportName);
            ObjectDataSource objDataSource = new ObjectDataSource() { ID = DataSetName, TypeName = "BussinessLogic.Custom", SelectMethod = SelectedMethod};
            ReportParameter p1 = new ReportParameter("ReportParameter1", "1"); //I pass the same parameter name tha exist in report definition and it's value is on 1 as  static value for now

            this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1});

            ReportDataSource datasource = new ReportDataSource(DataSetName, objDataSource);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);

            #endregion

All data access for local reports should be performed before binding it to your report. 在将本地报表绑定到您的报表之前,应先执行对本地报表的所有数据访问。 If possible, filter at the source. 如果可能,在源处进行过滤。

You need to add to the ObjectDataSource.SelectParameter collection. 您需要添加到ObjectDataSource.SelectParameter集合。

string DataSetName = "DataSet1Parameter";
string SelectedMethod = "GetSomeEmployees";
string Parameter = "ID";

ObjectDataSource objDataSource = new ObjectDataSource() { ID = DataSetName, TypeName = "BussinessLogic.Custom", SelectMethod = SelectedMethod};
    objDataSource.SelectParameters.Add(new Parameter("id"));

then subscribe to the ObjectDataSource.Selecting event to set the parameter. 然后订阅ObjectDataSource.Selecting事件以设置参数。

protected void objDataSource_Selecting
    (object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["id"] = "1";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM