gpt4 book ai didi

telerik-reporting - 如何根据传入的参数动态设置Telerik Report Picture框

转载 作者:行者123 更新时间:2023-12-02 09:07:28 25 4
gpt4 key购买 nike

我正在 MVC 应用程序中生成 Telerik 报告。该报告直接呈现为 pdf 格式,而不是使用报告查看器。我认为我正在从 Controller 正确传递参数,但我无法弄清楚在渲染报表时如何或在哪里获取报表代码中的参数值。我想使用用户函数根据传入参数的值动态填充图片框。

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

 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";
}


}
}
}

问题是我从 Controller 发送的买家参数从未真正进入报告。在调试期间,一旦代码到达以下行,报告就会运行 InitializeComponent():

var reportDocument = new LogisticsReports.Report1 

我需要能够在报告呈现之前捕获并评估传入的参数,但我不知道如何做到这一点。有什么想法吗?

最佳答案

终于解决了这个问题。 Controller 代码很好(除了参数必须动态确定,而不是像我的示例那样硬编码)。

报告需要有一个参数,在本例中其名称为“Buyer”

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

=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: 驱动器上,您可以将 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");
}
}

}

关于telerik-reporting - 如何根据传入的参数动态设置Telerik Report Picture框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com