Page.aspx.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Microsoft.Reporting.WebForms;
  8. using System.Net;
  9. using EMIS.Utility;
  10. namespace EMIS.Web.ReportViewer
  11. {
  12. public partial class Page : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!IsPostBack)
  17. {
  18. if (!string.IsNullOrEmpty(Request.Params["ReportFolder"]))
  19. {
  20. string reportpath = HttpUtility.HtmlDecode(Request.Params["ReportFolder"]);
  21. int aantalKeys = Request.Params.AllKeys.Length;
  22. List<ReportParameter> parameters = new List<ReportParameter>();
  23. for (int i = 1; i < aantalKeys; i++)
  24. {
  25. string value = Request.Params[i];
  26. string key = Request.Params.Keys[i];
  27. if (key.Contains("_RAP"))
  28. {
  29. int index = key.IndexOf('_');
  30. key = key.Substring(0, index);
  31. if (string.IsNullOrEmpty(value))
  32. {
  33. ReportParameter parameter = new ReportParameter(key, (string)null);
  34. parameters.Add(parameter);
  35. }
  36. else
  37. {
  38. ReportParameter parameter = new ReportParameter(key, HttpUtility.HtmlDecode(value));
  39. parameters.Add(parameter);
  40. }
  41. }
  42. }
  43. this.RenderReport(reportpath, parameters);
  44. }
  45. }
  46. }
  47. private void RenderReport(string reportpath, List<ReportParameter> parameters = null)
  48. {
  49. string User = Const.LOCAL_SETTING_REPORT_USER_NAME;
  50. string Pass = Const.LOCAL_SETTING_REPORT_PASSWORD;
  51. string ReportServerUrl = Const.LOCAL_SETTING_REPORT_URL;
  52. IReportServerCredentials irsc = new CustomReportCredentials(User, Pass, "");
  53. Uri uri = new Uri(ReportServerUrl);
  54. // EVENTS
  55. //reportViewer.Load += reportViewer_Load;
  56. //reportViewer.Unload += reportViewer_Unload;
  57. if (Request.Browser.Browser.Contains("InternetExplorer"))
  58. {
  59. reportViewer.SizeToReportContent = false;
  60. }
  61. else
  62. {
  63. reportViewer.SizeToReportContent = true;
  64. }
  65. reportViewer.ShowExportControls = true;
  66. reportViewer.ShowPrintButton = true;
  67. reportViewer.ShowParameterPrompts = false;
  68. //reportViewer.Visible = true;
  69. reportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
  70. reportViewer.ServerReport.ReportServerCredentials = irsc;
  71. reportViewer.ServerReport.ReportServerUrl = uri;
  72. reportViewer.ServerReport.ReportPath = reportpath;
  73. if (parameters != null && parameters.Count != 0)
  74. {
  75. reportViewer.ServerReport.SetParameters(parameters);
  76. }
  77. reportViewer.ServerReport.Refresh();
  78. }
  79. private Dictionary<string, object> GetCurrentParameters()
  80. {
  81. var parameterCollection = reportViewer.ServerReport.GetParameters();
  82. var param = new Dictionary<string, object>();
  83. foreach (var p in parameterCollection)
  84. {
  85. var name = p.Name;
  86. if (p.DataType == ParameterDataType.DateTime)
  87. {
  88. var d = Convert.ToDateTime(p.Values[0]);
  89. param[name] = d.ToString("dd-MM-yyyy");
  90. }
  91. else
  92. {
  93. var values = p.Values.ToList();
  94. param[name] = values;
  95. }
  96. }
  97. return param;
  98. }
  99. }
  100. public class CustomReportCredentials : IReportServerCredentials
  101. {
  102. private string _UserName;
  103. private string _PassWord;
  104. private string _DomainName;
  105. public CustomReportCredentials(string UserName, string PassWord, string DomainName)
  106. {
  107. _UserName = UserName;
  108. _PassWord = PassWord;
  109. _DomainName = DomainName;
  110. }
  111. public System.Security.Principal.WindowsIdentity ImpersonationUser
  112. {
  113. get { return null; }
  114. }
  115. public ICredentials NetworkCredentials
  116. {
  117. get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }
  118. }
  119. public bool GetFormsCredentials(out Cookie authCookie, out string user,
  120. out string password, out string authority)
  121. {
  122. authCookie = null;
  123. user = password = authority = null;
  124. return false;
  125. }
  126. }
  127. }