Telerik Reporting R1 2017

How To: Pass Values to Report Parameters

This topic explains how to use custom parameters UI to update the report parameters instead of using the report viewer's default implementation of the parameters area. The report and all required parameters for it are packed in a ReportSource object. To update the report source the ReportViewer.reportSource(rs) method is used.

To give an example we will use the Invoice report from our examples and will update its OrderNumber parameter from a custom parameter UI.

Pass values to report parameters

Tip Tip

All path references in the described steps should be adapted according to your project setup. For more information please refer to the MSDN article ASP.NET Web Project Paths

  1. Add a new html page CustomParameters.html to the CSharp.Html5Demo or VB.Html5Demo project.

  2. Add the references to all required JavaScript libraries and stylesheets:

    Html
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Telerik HTML5 Report Viewer</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    
        <link href="/kendo/styles/kendo.common.min.css" rel="stylesheet" />
        <link href="/kendo/styles/kendo.blueopal.min.css" rel="stylesheet" />
    
        <script src="/ReportViewer/js/kendo.subset.2015.3.930.min.js"></script>
        <script src="/ReportViewer/js/telerikReportViewer-9.2.15.1105.js"></script>
    
        <style>
            #reportViewer1 {
                position: absolute;
                left: 5px;
                right: 5px;
                top: 40px;
                bottom: 5px;
    
                font-family: 'segoe ui', 'ms sans serif';
    
                overflow: hidden;
            }
        </style>
    </head>
  3. Add the custom parameter UI - a dropdown selector with a few values:

    Html
    <div id="invoiceIdSelector">
        <label for="invoiceId">Invoices</label>
        <select id="invoiceId" title="Select the Invoice ID">
            <option value="SO51081">SO51081</option>
            <option value="SO51082" selected="selected">SO51082</option>
            <option value="SO51083">SO51083</option>
        </select>
    </div>
  4. Add the ReportViewer placeholder

    Html
    <div id="reportViewer1">
        loading...
    </div>
  5. Now initialize the report viewer. We will use the minimal set of all possible options. Please note how the value from the custom UI is used to set the OrderNumber report parameter initially:

    JavaScript
    $(document).ready(function () {
        $("#reportViewer1").telerik_ReportViewer({
            serviceUrl: "api/reports/",
            reportSource: {
                report: "Telerik.Reporting.Examples.CSharp.Invoice, CSharp.ReportLibrary",
                parameters: { OrderNumber: $('#invoiceId option:selected').val() }
            },
            ready: function () {
                //this.refreshReport();
            }
        });
    });
  6. Add code that updates the ReportSource parameters collection with the selected Invoice Id from the dropdown box:

    JavaScript
    $('#invoiceId').change(function () {
        var viewer = $("#reportViewer1").data("telerik_ReportViewer");
        viewer.reportSource({
            report: viewer.reportSource().report,
            parameters: { OrderNumber: $(this).val() } 
        });
        //setting the HTML5 Viewer's reportSource, causes a refresh automatically
        //if you need to force a refresh for other case, use:
        //viewer.refreshReport();
    });
  7. The HTML page that we have just created should looks like this:

    Html
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Telerik HTML5 Report Viewer Demo With Custom Parameter</title>
    
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    
        <link href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.common.min.css" rel="stylesheet" />
        <link href="http://kendo.cdn.telerik.com/2015.3.930/styles/kendo.blueopal.min.css" rel="stylesheet" />
    
        <script src="/ReportViewer/js/kendo.subset.2015.3.930.min.js"></script>
        <script src="ReportViewer/js/telerikReportViewer-8.1.14.816.js"></script>
    
        <style>
            #reportViewer1 {
                position: absolute;
                left: 5px;
                right: 5px;
                top: 40px;
                bottom: 5px;
                overflow: hidden;
                font-family: Verdana, Arial;
            }
        </style>
    </head>
    <body>
        <div id="invoiceIdSelector">
            <label for="invoiceId">Invoices</label>
            <select id="invoiceId" title="Select the Invoice ID">
                <option value="SO51081">SO51081</option>
                <option value="SO51082" selected="selected">SO51082</option>
                <option value="SO51083">SO51083</option>
            </select>
        </div>
    
        <div id="reportViewer1">
            loading...
        </div>
    
        <script type="text/javascript">
            $(document).ready(function () {
                $("#reportViewer1").telerik_ReportViewer({
                        serviceUrl: "api/reports/",
                        reportSource: {
                            report: "Telerik.Reporting.Examples.CSharp.Invoice, CSharp.ReportLibrary",
                            parameters: { OrderNumber: $('#invoiceId option:selected').val() }
                        },                    
                  });
              });
    
                $('#invoiceId').change(function () {
                    var viewer = $("#reportViewer1").data("telerik_ReportViewer");
                    viewer.reportSource({
                        report: viewer.reportSource().report,
                        parameters: { OrderNumber: $(this).val() } 
                    });
                    //setting the HTML5 Viewer's reportSource, causes a refresh automatically
                    //if you need to force a refresh for other case, use:
                    //viewer.refreshReport();
                });
        </script>
    
    </body>
    </html>
  8. Run the project and verify that the Invoice Id selection really updates the report.

See Also