Reputation: 2055
I am generating a Telerik
report in an MVC
application. The report is being rendered straight to pdf format, not using a Report Viewer
. I think I am passing the parameter correctly from the controller but I can not figure out how or where to get the parameters value in the report code behind when the report is being rendered. I want to dynamically populate a picture box depending on the value of the passed in parameter using a User Function
.
Here is my Controller code that opens the report. If I hard code the buyer variable I get the correct image to display in the picturebox:
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");
}
The Code Behind for the report:
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";
}
}
}
}
The problem is that the buyer parameter I send from the Controller never actually makes it to the report. During debug the report InitializeComponent() runs as soon as the code hits the line:
var reportDocument = new LogisticsReports.Report1
I need to be able to capture and evaluate the passed in parameter before the report renders but I don't see how to do that. Any Ideas?
Upvotes: 3
Views: 7323
Reputation: 2055
Fianlly figured this one out. Controller code is fine (except that the parameter will have to be determined dynamically rather than be hardcoded like my example).
The report will need to have a Parameter, in this case it's named "Buyer"
The picturebox.value on the Telerik report will be a User Function that will return the URL of the picture. Invoking the function will pass hte report parameter to the it as follows:
=MyNameSpace.Report1.ResolveURL(Parameters.Buyer.Value)
The User Function will live in the Code Behind of the report. Here is an example I got to work:
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);
}
}
}
If an image will come from someplace other than a URL like maybe a file system, You would have to change the output type of the User Function and change the code. For instance if the images were loaded on the C: Drive you would change th3e function to look like this:
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");
}
}
}
Upvotes: 3