Specify Parameter Values in an ASP.NET Core Reporting Application
- 5 minutes to read
Use the Parameters Panel
Open the Parameters Panel and use its editors to specify parameter values. Click Submit to apply the values to the report and display the document.
Use Custom UI Elements
You can create custom UI elements and use them to submit parameter values to the report.
To apply the values to the report, create a string that contains the report name and the submitted values, and pass this string to the viewer’s OpenReport method.
The example below demonstrates how to do the following:
- Create a page with a document viewer, editor, and button.
- Create a string with the report name and a value from the editor, and pass this string to the viewer’s OpenReport method on a button click.
<script type="text/javascript">
function OnClick() {
var reportName = "XtraReport1";
var paramName = "strParam";
var paramValue = document.getElementById("paramValueField").value;
var paramQueryString = paramName + '=' + paramValue;
DocumentViewer.OpenReport(reportName + "?" + paramQueryString);
}
</script>
<input id="paramValueField" type="text" />
<button onclick="OnClick()" type="button">Submit</button>
@{
var viewerRender = Html.DevExpress().WebDocumentViewer("DocumentViewer")
.Height("1000px")
.Bind("XtraReport1");
@viewerRender.RenderHtml()
}
When you call the viewer’s OpenReport method, the reporting engine executes a report name resolution service. This service creates a report instance and returns it to the viewer. Implement this service to apply the parameter values to the report. The string argument from the OpenReport method is passed to the service’s method that returns a report instance. In this method, do the following:
- Parse the string argument to extract the report name and parameter values.
Create a report instance and apply the parameter values to it. Reference each parameter by name in the report’s Parameters collection and assign the value to the parameter’s Value property.
If you want custom UI elements to be the only way to submit parameter values, hide the Parameters Panel. To do this, disable the Visible property for all report parameters. If you want users to submit parameter values from both the panel and custom UI elements, disable the report’s RequestParameters property.
- Return the report instance.
When you create a Web Reporting application from the DevExpress Template Gallery, you can add the Report Storage service to the application. This service will be utilized as a report name resolution service in your application. For this, implement the service’s GetData method as follows:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using DevExpress.XtraReports.UI;
using Microsoft.AspNetCore.Hosting;
// ...
public override byte[] GetData(string url) {
try {
// Parse the string with the report name and parameter values.
string[] parts = url.Split('?');
string reportName = parts[0];
string parametersQueryString = parts.Length > 1 ? parts[1] : String.Empty;
// Create a report instance.
XtraReport report = null;
if (Directory.EnumerateFiles(ReportDirectory).
Select(Path.GetFileNameWithoutExtension).Contains(reportName)) {
byte[] reportBytes = File.ReadAllBytes(Path.Combine(ReportDirectory, reportName + FileExtension));
using (MemoryStream ms = new MemoryStream(reportBytes))
report = XtraReport.FromStream(ms);
}
if (report != null) {
// Apply the parameter values to the report.
var parameters = HttpUtility.ParseQueryString(parametersQueryString);
foreach (string parameterName in parameters.AllKeys) {
report.Parameters[parameterName].Value = Convert.ChangeType(
parameters.Get(parameterName), report.Parameters[parameterName].Type);
}
// Disable the Visible property for all report parameters
// to hide the Parameters Panel in the viewer.
foreach (var parameter in report.Parameters) {
parameter.Visible = false;
}
// If you do not hide the panel, disable the report's RequestParameters property.
// report.RequestParameters = false;
using (MemoryStream ms = new MemoryStream()) {
report.SaveLayoutToXml(ms);
return ms.ToArray();
}
}
} catch (Exception ex) {
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
"Could not get report data.", ex);
}
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
string.Format("Could not find report '{0}'.", url));
}
Submit Parameter Values in a URL Query String
To apply parameter values from a URL query string to a report, implement the viewer’s action method as follows:
Create a report instance and apply the parameter values to it. Reference each parameter by name in the report’s Parameters collection and assign the value to the parameter’s Value property.
If you want a URL’s query string to be the only way to submit parameter values, hide the Parameters Panel. To do this, disable the Visible property for all report parameters. If you want users to submit parameter values from both the panel and a URL’s query string, disable the report’s RequestParameters property.
Return a report view.
The example below demonstrates how to implement the viewer’s action method to apply the parameter value from the query string of the following URL: https://localhost:5001/Home/Viewer?strParam=abc
.
public IActionResult Viewer(string? strParam) {
var report = new XtraReport1();
report.Parameters["strParam"].Value = strParam;
report.Parameters["strParam"].Visible = false;
return View(report);
}
If the parameter value is initially set dynamically with an expression that uses a function, you should remove the expression from the ExpressionBindings collection before assigning a new parameter value:
var parameter = report.Parameters["MyParameter"];
var paramValueExpression = parameter.ExpressionBindings
.FirstOrDefault(p => p.PropertyName == "Value");
if (paramValueExpression != null)
{
parameter.ExpressionBindings.Remove(paramValueExpression);
}
report.Parameters["MyParameter"].Value = parameterFromQueryString;
Handle the Client-Side ParametersInitialized Event
The ParametersInitialized event allows you to get the client-side parameter values when they are initialized, subscribe to their changes, and fill parameter editors with custom parameter values.