Web Page Query Conditions to Open Reporting Service(SSRS) using Url transfer Access Parameters
The result:
實作:
ReportServer Address: http://you-Server/ReportServer/you-folder/you-filename (ex: http://iverson-pc/ReportServer/Ch5/連動式參數)
Reports Adderss: http://you-Server/Reports/you-folder/you-filename (ex: http://iverson-pc/Reports/Ch5/連動式參數)
Web.config
<appSettings> <add key="ReportWebServer" value="http://you-Server" /><!-- 依環境設定 According to the environment setting --> <add key="ReportVirtualDirectory" value="/ReportServer" /><!-- 依環境設定 According to the environment setting --> <add key="ReportWebAppDirectoryDefault" value="/you-folder/" /><!-- 依環境設定 According to the environment setting --> <!--this is My Setting--> <add key="ReportWebServer" value="http://IVERSON-PC" /><!-- 依環境設定 According to the environment setting --> <add key="ReportVirtualDirectory" value="/ReportServer" /><!-- 依環境設定 According to the environment setting --> <add key="ReportWebAppDirectoryDefault" value="/PSLK/" /><!-- 依環境設定 According to the environment setting --> </appSettings>
Site.Master
<script src="<%: Url.Content("~/Scripts/ReportingService_Base.js") %>" type="text/javascript"></script>
include My
ReportingService_Base.js
function openReport(ajaxReport) {
var theForm = $('form#queryform');
var chunk = theForm.serialize();
$.ajax({
type: "POST",
url: ajaxReport,
data: chunk,
timeout: 500000,
error: function (xhr) {
alert('error:' + xhr);
},
success: function (resultData) {
openReportWin(resultData);
}
});
}
function openReportWin(resultData) {
jsonObj = eval("(" + resultData + ")");
if (undefined == jsonObj) {
return;
}
var reportWin = window.open("",jsonObj.PGMID, "location=no,menubar=no,status=no,toolbar=no,resizable=yes");
reportWin.document.write("<html><body><form id='reportForm' name='reportForm' action=''
method='post' target='_self'></form></body></html>");
reportWin.document.reportForm.action = jsonObj.RpURL + encodeURIComponent(jsonObj.RpFilePath);
var rpForm = $(reportWin.document.reportForm);
for (i = 0; i < jsonObj.Parameters.length; i++) {
rpForm.append("<input type='hidden' name='" + jsonObj.Parameters[i].Name + "'
value='" + jsonObj.Parameters[i].Value + "'>");
}
reportWin.document.reportForm.submit();
setTimeout(function () { reportWin.close(); }, 60000);
}
ReportController
public class ReportController { private string _ReportWebServer = ""; private string _ReportVirtualDirectory = ""; private string _ReportWebAppDirectoryDefault = ""; private string _ReportFilePath = ""; private Dictionary<string, object> _ReportParameters = new System.Collections.Generic.Dictionary<string, object>(); //指定用於轉譯報表的格式(HTML4.0、MHTML、IMAGE、EXCEL、WORD、CSV、PDF、XML 和 NULL) public enum ReportFormats { HTML40, MHTML, IMAGE, EXCEL, WORD, CSV, PDF, XML, NULL }; public ReportFormats ReportFormat = ReportFormats.HTML40; //顯示或隱藏工具列的參數區 public bool bShowParameters = false; public ReportController() { if (null != System.Web.Configuration.WebConfigurationManager.AppSettings["ReportWebServer"]) { _ReportWebServer = System.Web.Configuration.WebConfigurationManager.AppSettings["ReportWebServer"]; _ReportWebServer = _ReportWebServer.Trim(new char[] { ' ', '/' }); } if (null != System.Web.Configuration.WebConfigurationManager.AppSettings["ReportVirtualDirectory"]) { _ReportVirtualDirectory = System.Web.Configuration.WebConfigurationManager. AppSettings["ReportVirtualDirectory"]; _ReportVirtualDirectory = _ReportVirtualDirectory.Trim(new char[] { ' ', '/' }); if ("" != _ReportVirtualDirectory) { _ReportVirtualDirectory = "/" + _ReportVirtualDirectory; } } if (null != System.Web.Configuration.WebConfigurationManager.AppSettings["ReportWebAppDirectoryDefault"]) { _ReportWebAppDirectoryDefault = System.Web.Configuration.WebConfigurationManager. AppSettings["ReportWebAppDirectoryDefault"]; _ReportWebAppDirectoryDefault = _ReportWebAppDirectoryDefault.Trim(new char[] { ' ', '/' }); if ("" == _ReportWebAppDirectoryDefault) { _ReportWebAppDirectoryDefault = "/"; } else { _ReportWebAppDirectoryDefault = "/" + _ReportWebAppDirectoryDefault + "/"; } } } public string ReportWebServer { get { return _ReportWebServer; } } public string ReportWebAppDirectoryDefault { set { _ReportWebAppDirectoryDefault = value.Trim(new char[] { ' ', '/' }); if ("" == _ReportWebAppDirectoryDefault) { _ReportWebAppDirectoryDefault = "/"; } else { _ReportWebAppDirectoryDefault = "/" + _ReportWebAppDirectoryDefault + "/"; } } get { return _ReportWebAppDirectoryDefault; } } public string ReportFilePath { set { _ReportFilePath = value.Trim(new char[] { ' ', '/' }); } get { return _ReportFilePath; } } public string ReportVirtualDirectory { set { _ReportVirtualDirectory = "/" + value.Trim(new char[] { ' ', '/' }); } get { return _ReportVirtualDirectory; } } public void AddParameters(string strKey, object objValue) { try { strKey = strKey.Trim(); if (_ReportParameters.ContainsKey(strKey)) { _ReportParameters[strKey] = null == objValue ? "" : objValue.ToString().Trim(); } else { _ReportParameters.Add(strKey, null == objValue ? "" : objValue.ToString().Trim()); } } catch (Exception ex) { throw ex; } } public Dictionary<string, object> ReportParameters { get { return _ReportParameters; } } public void ClearParameters() { _ReportParameters.Clear(); } public string OpenReport() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); #region 檢查參數 if ("" == ReportWebServer) { throw(new Exception("未設定報表伺服器!")); } else if ("" == ReportFilePath) { throw(new Exception("未設定報表檔!")); } #endregion #region 加入固定的參數 AddParameters("rc:Parameters", bShowParameters); //顯示或隱藏工具列的參數區 AddParameters("rc:Stylesheet", "HtmlViewer"); //指定要套用至 HTML 檢視器的樣式表 AddParameters("rs:ParameterLanguage", "zh-TW"); //指定要套用至 HTML 檢視器的樣式表 AddParameters("rs:Command", "Render"); //轉譯指定的報表 AddParameters("rs:Format", (ReportFormat == ReportFormats.HTML40 ? "HTML4.0" : Enum.GetName(typeof(ReportFormats), ReportFormat))); //指定用於轉譯報表的格式 #endregion #region 組報表URL和參數 //報表URL sb.AppendFormat("{{\"RpURL\":\"{0}{1}?\",", ReportWebServer, ReportVirtualDirectory); //開報表的作業,用來控制不同作業開的報表都在不同視窗 sb.AppendFormat("\"RpFilePath\":\"{0}\",", ReportWebAppDirectoryDefault + ReportFilePath); sb.AppendFormat("\"PGMID\":\"{0}\",", "you-filename"); //參數 sb.Append("\"Parameters\":["); foreach (KeyValuePair<string, object> kvp in ReportParameters) { sb.AppendFormat("{{\"Name\":\"{0}\",\"Value\":\"{1}\"}},", kvp.Key, kvp.Value); } sb.Remove(sb.Length - 1, 1); sb.Append("]}"); #endregion return sb.ToString(); } }
ProductController 繼承 ReportController
public class ProductController : ReportController { public ActionResult Product() { return View(); } public string AJAX_Report() { #region 單隻作業自定參數 //報表檔 base.ReportFilePath = "you-filename"; base.ReportWebAppDirectoryDefault = "you-folder"; //畫面參數 base.AddParameters("ProductCategory", null == Request["DropDownList_ProductCategory"] ? "" : Request["DropDownList_ProductCategory"].Trim());//產品類別 base.AddParameters("ProductSubcategory", null == Request["DropDownList_ProductSubcategory"] ? "" : Request["DropDownList_ProductSubcategory"].Trim());//產品型別 #endregion //開啟報表 try { return base.OpenReport(); } catch (Exception ex) { return ex.ToString(); } } }
Product.aspx
<h2>Product</h2> <%using (Html.BeginForm("", "Telerik", FormMethod.Post, new { id = "queryform", name = "queryfrom" })) { %> <table> <tr> <td> 產品類別: </td> <td> <%= Html.Telerik().DropDownList() .Name("DropDownList_ProductCategory") .DataBinding(binding => binding.Ajax().Select("Ajax_DropDownList_ProductCategory", "Ajax")) .Placeholder("Select ...") .DropDownHtmlAttributes(new { @class = "validate[required]" }) .CascadeTo("DropDownList_ProductSubcategory") %> </td> </tr> <tr> <td> 產品型別: </td> <td> <%= Html.Telerik().DropDownList() .Name("DropDownList_ProductSubcategory") .DataBinding(binding => binding.Ajax().Select("Ajax_DropDownList_ProductSubcategory", "Ajax")) .Placeholder("Select ...") .DropDownHtmlAttributes(new { @class = "validate[required]" }) .CascadeTo("") %> </td> </tr> <tr> <td> <input type="button" value="Open Report" onclick="doPrint()" /> </td> </tr> </table> <%} %> <script type="text/javascript"> function doPrint() { openReport("AJAX_Report"); } </script>
架構設計者: ChihHao(ThinkPower-- 志豪)
參與設計者: Iverson (ThinkPower-- iverson.huang)
iverson.huang 筆記