Listening Has Always Been Our Strongest Tool!

Just Give Us a Try, we respond to all messages within 1 business day. Really!


How can we reach out to you?






NDA From The Start
We keep your idea safe. We're always willing to sign a Non-Disclosure Agreement with you and fully protect your intellectual property.

captcha

Other Ways To Meet Us :info@zealousys.com
Our Blog
Oct 17
2013

How to Load Telerik Report in ASP.NET MVC Portable Area Project

By zealousys

We will here try to explain how one can implement “.trdx” extension Telerik report in ASP.NET MVC Portable Area application. The big question here was how we can create Telerik reports that are strongly typed and how to package them up. Since we would need controller actions for rendering or identifying Telerik report from assembly, it would be very difficult as when portable area project compiles it would generate a DLL. But we are using that DLL in another MVC application so we made a MVC project as a portable area with a use of MVCCONTRIB.

Following steps can help you to achieve it…

Step1: Create a Telerik report through Telerik report designer tool and save it within MVC application with .trdx extension.
telerik-report

Step2: Create _ARReportViewer.aspx view under view->shared folder in mvc application. Here as you can see we have picked .aspx view instead of razor view because we need to load Telerik report in TelerikReportViewer control inside MVC application. As TelerikReportViewer is a server control so we will have to set runat=”server” properties.

  1. <%@ Page Language=”C#” Inherits=”System.Web.Mvc.ViewPage<dynamic>” %>
  2. <%@ Register assembly=”Telerik.ReportViewer.WebForms” namespace=”Telerik.ReportViewer.WebForms” tagprefix=”telerik” %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head id=”Head1″ runat=”server”/>
  6. <body>
  7. <form id=”form1″ runat=”server”>
  8. <script runat=”server”>
  9. public override void VerifyRenderingInServerForm(Control control)
  10. {
  11. }
  12. protected override void OnLoad(EventArgs e)
  13. {
  14. base.OnLoad(e);
  15. ReportViewer1.ReportSource = Model.ReportDocument;
  16. Page.Title = Model.ReportPageTitle;
  17. }
  18. </script>
  19. <telerik:ReportViewer ID=”ReportViewer1″ runat=”server” Width=”100%” Height=”800px”></telerik:ReportViewer>
  20. </form>
  21. </body>
  22. </html>

Steps 3: Create ARReportViewer.cshtml razor view under view->shared folder in mvc application and implement _ARReportViewer.aspx view as a partial view because when controller action is fired, it will find ARReportViewer view and load Telerik report.

  1. @{
  2. Layout = null;
  3. }
  4. @Html.Partial(“_ARReportViewer”)

Step 4: Create same action method name as razor view name in appropriate controller. One More thing you should notice is that we have created new object of ReportBusinessModel and passed relevant entity object. – See more at: http://www.zealousys.com/blog/how-to-load-telerik-report-in-asp-net-mvc-portable-area-project/#sthash.f01vQ2tA.dpuf

  1. public ActionResult ARReportViewer()
  2. {
  3. var report = new ReportBusinessModel
  4. {
  5. ReportName = ReportName,
  6. ReportPageTitle = ReportTitle,
  7. ReportPath = ReportPath
  8. };
  9. string[] performanceIds = (string[])Session[“ExportPerformance”];
  10. PerformanceOutcomeBusinessClass objPerformance = new PerformanceOutcomeBusinessClass(UnitOfWork);
  11. var performances = (from p in objPerformance.GetAllPerformanceIndictors()
  12. where performanceIds.Contains(p.PerformanceIndicatorId) select p).ToList();
  13. report.ReportInfo = performances;
  14. return View(report);
  15. }

Step 5: In ReportBusinessModel,we have defined the name, Title of the Telerik Report which is represented relevant entity object and displayed in a Telerik report. In above controller action method, you have seen that we have passed the result data as “performances” object to report.ReportInfo variable.

As you have observed ReportDocument in ReportBusinessModel, It is represents report data source. Here we have given the report assembly name as a report path.

  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Xml;
  6. using Telerik.Reporting;
  7. namespace AcademicReporting.BusinessModels
  8. {
  9. public class ReportBusinessModel
  10. {
  11. private InstanceReportSource reportDocument;
  12. private string reportName;
  13. private readonly Dictionary<string, string> parameterList = new Dictionary<string, string>();
  14. public List ReportInfo { get; set; }
  15. public string ReportPageTitle { get; set; }
  16. public string ReportName
  17. {
  18. get { return reportName; }
  19. set
  20. {
  21. reportName = value;
  22. reportDocument = null;
  23. }
  24. }
  25. public string ReportPath { get; set; }
  26. private readonly string ARReportsPath = System.Web.Configuration.WebConfigurationManager.Appsettings[“ARReportsPath”];
  27. private readonly string ReportsExtension = System.Web.Configuration.WebConfigurationManager.Appsettings[“ReportsExtension”];
  28. public InstanceReportSource ReportDocument
  29. {
  30. get
  31. {
  32. Report report;
  33. if (reportDocument == null)
  34. {
  35. var streamData = this.getType().Assembly.getManifestResourceStream(ReportPath);
  36. XmlDocument xmlDoc = null;
  37. if (ReportPath.Contains(“SingleSubjectReportPrint.trdx”) ||
  38. ReportPath.Contains(“SingleSubjectSubReport.trdx”) ||
  39. ReportPath.Contains(“MultipleSubjectReportPrint.trdx”) ||
  40. ReportPath.Contains(“MultipleSubjectSubReport.trdx”))
  41. xmlDoc = GetReportFileXml(ReportPath);
  42. else
  43. xmlDoc = GetReportXml(ReportPath);
  44. var xmlsettings = new XmlReadersettings { IgnoreWhitespace = true };
  45. var xmlReader = XmlReader.Create(new stringReader(xmlDoc.DocumentElement.OuterXml), xmlsettings);
  46. var xmlSerializer = new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();
  47. report = (Report)xmlSerializer.Deserialize(xmlReader);
  48. report.ItemDataBinding += TelerikReport_ItemDataBinding;
  49. var tables = report.Items.SelectMany(x => x.Items.Find(typeof(Table), true)).ToList();
  50. if (!tables.Any())
  51. report.NeedDataSource += TelerikReport_NeedDataSource;
  52. reportDocument = new InstanceReportSource { ReportDocument = report };
  53. }
  54. else
  55. {
  56. report = (Report)reportDocument.ReportDocument;
  57. }
  58. foreach (var param in parameterList)
  59. {
  60. report.ReportParameters[param.Key].value = param.value;
  61. }
  62. return reportDocument;
  63. }
  64. set { reportDocument = value; }
  65. }
  66. public XmlDocument getReportXml(string ReportPath)
  67. {
  68. var streamData = this.getType().Assembly.getManifestResourceStream(ReportPath);
  69. StreamReader streamReader = new StreamReader(streamData);
  70. XmlDocument xmlDoc = new XmlDocument();
  71. xmlDoc.Load(streamReader);
  72. return xmlDoc;
  73. }
  74. public XmlDocument getReportFileXml(string ReportPath)
  75. {
  76. XmlDocument xmlDoc = new XmlDocument();
  77. xmlDoc.Load(ReportPath);
  78. return xmlDoc;
  79. }
  80. private void TelerikReport_ItemDataBinding(object sender, System.EventArgs e)
  81. {
  82. var report = (Telerik.Reporting.Processing.Report)sender;
  83. var tables = report.ItemDefinition.Items.SelectMany(x => x.Items.Find(typeof(Table), true)).ToList();
  84. tables.foreach(t => ((Table)t).NeedDataSource += TelerikReport_NeedDataSource);
  85. var charts = report.ItemDefinition.Items.SelectMany(x => x.Items.Find(typeof(Chart), true)).ToList();
  86. charts.foreach(t => ((Chart)t).NeedDataSource += TelerikReport_NeedDataSource);
  87. }
  88. void TelerikReport_NeedDataSource(object sender, System.EventArgs e)
  89. {
  90. var senderType = sender.getType();
  91. IList props = new List(senderType.getProperties());
  92. var firstOrDefault = props.FirstOrDefault(info => info.Name == “DataSource”);
  93. if (firstOrDefault != null)
  94. firstOrDefault.SetValue(sender, ReportInfo);
  95. }
  96. public void AddReportParameters(string key, string value)
  97. {
  98. parameterList.Add(key, value);
  99. }
  100. }
  101. }
View My Stats