Skip to main content

Specify Parameter Values in a Web Forms 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 the Parameters Panel to Specify Parameter Values in a Web Forms application

Use Custom UI Elements

You can create custom UI elements and use them to submit parameter values to the report.

Use custom UI elements to specify parameter values in a Web Forms application

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:

  1. Create a page with a document viewer and button.
  2. Create a string with the report name and a predefined parameter value and pass this string to the viewer’s OpenReport method on a button click.
<asp:Content ID="Content" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function OnClick(s, e) {
            var reportName = "XtraReport1";
            var paramName = "intParam";
            var paramValue = 42;

            WebDocumentViewer1.OpenReport(reportName + '?' + paramName + '=' + paramValue);
        }
    </script>

    <dx:ASPxButton ID="ASPxButton1" runat="server" Text="Submit Parameter" AutoPostBack="False">
        <ClientSideEvents Click="OnClick" />
    </dx:ASPxButton>

    <dx:ASPxWebDocumentViewer ClientInstanceName="WebDocumentViewer1"
        ID="ASPxWebDocumentViewer1" runat="server">
    </dx:ASPxWebDocumentViewer>
</asp:Content>

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:

  1. Parse the string argument to extract the report name and parameter values.
  2. 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.

  3. 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.

  4. 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.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Web;
using DevExpress.XtraReports.Web.Extensions;
using DevExpress.XtraReports.UI;
// ...
        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 (ReportsFactory.Reports.ContainsKey(reportName)) {
                    report = ReportsFactory.Reports[reportName]();
                }

                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) {
                throw new FaultException(
                    new FaultReason("Could not get report data."), 
                    new FaultCode("Server"), "GetData");
            }
            throw new FaultException(
                new FaultReason(string.Format("Could not find report '{0}'.", url)), 
                new FaultCode("Server"), "GetData");
        }

Submit Parameter Values in a URL Query String

Use a URL's query string to submit parameter values in a Web Forms application

To apply parameter values from a URL query string to a report, handle the viewer’s Page_Load event as follows:

  1. 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.

  2. 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.

  3. Call the viewer’s OpenReport(url) method and pass the report to this method.

The example below demonstrates how to handle the viewer’s Page_Load event to apply the parameter value from the URL query string (…Viewer.aspx?intParam=42):

public partial class Viewer : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        var report = new XtraReport1();
        var paramValue = Convert.ToInt32(Request.QueryString["intParam"]);
        report.Parameters["intParam"].Value = paramValue;
        report.Parameters["intParam"].Visible = false;
        ASPxWebDocumentViewer1.OpenReport(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;
See Also