3

我正在应用程序中生成Telerik报告MVC。报告直接呈现为 pdf 格式,而不是使用Report Viewer. 我认为我从控制器正确传递了参数,但我无法弄清楚在呈现报表时如何或在何处获取报表代码中的参数值。我想根据传入参数的值使用User Function.

这是我打开报告的控制器代码。如果我对买方变量进行硬编码,我会得到正确的图像以显示在图片框中:

 public ActionResult PrintPoReport()
    {
        byte[] contents;
        Telerik.Reporting.Processing.RenderingResult result;

        using (var reportDocument = new LogisticsReports.Report1())
        {
            var buyerID = "999999";  //hard code buyerId for testing
            var irs = new InstanceReportSource();
            irs.ReportDocument = reportDocument;

            irs.Parameters.Add(new Parameter("Buyer", "buyerID")); // parameter to determine which jpg will populate picture box. **Never gets to Report1** 
            Telerik.Reporting.Processing.ReportProcessor rp = new Telerik.Reporting.Processing.ReportProcessor();
            result = rp.RenderReport("PDF", irs, null);
            contents = result.DocumentBytes;
        }

        return File(contents, "application/pdf", "P0 #" + id + ".pdf");
    }

报告背后的代码:

public partial class Report1 : Telerik.Reporting.Report
{
    public Report1()
    {

        InitializeComponent();

        var buyer = "999999"; //hard coded for testing...this works!
        //Need to capture the passed in parameter here
        if (buyer == "111111"){

            this.pictureBox1.Value = "http://www.arctecalaska.com/images/signatures/111111.bmp";
        }
        if (buyer == "999999")
        {

            this.pictureBox1.Value = "http://www.arctecalaska.com/images/signatures/Ike.jpg";
        }


     }
   }
}

问题是我从控制器发送的买家参数实际上从未进入报告。在调试期间,报告 InitializeComponent() 会在代码到达该行时立即运行:

var reportDocument = new LogisticsReports.Report1 

我需要能够在报告呈现之前捕获和评估传入的参数,但我不知道该怎么做。有任何想法吗?

4

1 回答 1

3

最终想通了这一点。控制器代码很好(除了必须动态确定参数而不是像我的示例那样硬编码)。

报告需要有一个参数,在这种情况下它被命名为“买家”

Telerik 报告中的 picturebox.value 将是一个返回图片 URL 的用户函数。调用该函数会将 hte 报告参数传递给它,如下所示:

=MyNameSpace.Report1.ResolveURL(Parameters.Buyer.Value)

用户函数将存在于报告的代码后面。这是我开始工作的一个例子:

  public partial class Report1 : Telerik.Reporting.Report
{
    public Report1()    {

             InitializeComponent();
     }

    public static string ResolveUrl(string paramValue)
    {

        string imagePath = "";

        if (paramValue == "111111")
        {
            imagePath = "http://www.arctecalaska.com/images/signatures/111111.jpg";

        }
            if (paramValue == "999999")

                    {
            imagePath = "http://www.arctecalaska.com/images/signatures/999999.jpg";

        }

        return (imagePath);
      }
   }
}

如果图像来自 URL 以外的其他地方,例如文件系统,则必须更改用户函数的输出类型并更改代码。例如,如果图像是在 C: Drive 上加载的,您可以将 th3e 函数更改为如下所示:

public class Report1 : Telerik.Reporting.Report
{
 public Report1()    {

         InitializeComponent();
 }
public static System.Drawing.Image ResolveUrl(string paramValue)
{
   if(paramValue=="111111")
    {
        return System.Drawing.Image.FromFile("C:\\111111.jpg");
    }
     else
    {
         return System.Drawing.Image.FromFile("C:\\888888.jpg");
    }
}

} 
于 2013-11-09T21:00:40.767 回答