Showing posts with label postback. Show all posts
Showing posts with label postback. Show all posts

Monday, 12 November 2012

SSRS Automatically View Report after selecting drop down parameter value

In SQL Server Reporting Services (SSRS), I had a client who wanted to automatically rerun their report after selecting a value in a drop down without having to click on the "View Report" button. This can be done using JQuery and below is how it is done. 1.) Go to the "Report.aspx" page in the ReportManager directory. On My machine it is here. C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportManager\Pages\Report.aspx" 2) At the top of the file after the "Page" directive in asp.net add the following script.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script language = "Javascript">

 $(function(){
 $('body').on('change', 'select', function () {
 $('input[value="View Report"]').click();
 });
});
</script>
3) Now every time you click on the value in the drop box it will automatically postback and refresh the form with your selected parameter.

Postscript
After various feedback, people have asked for how to make the report refresh only on a particular control. This can be done with jQuery. Naturally with jQuery there are lots of ways of achieving this, so I have opted for the simplest. The short answer is here is the code below.
<%@ Register TagPrefix="MSRS" Namespace="Microsoft.ReportingServices.UI" Assembly="ReportingServicesWebUserInterface" %>

<%@ Page Language="c#" CodeBehind="Report.aspx.cs" AutoEventWireup="false" Inherits="Microsoft.ReportingServices.UI.ReportWrapperPage" EnableEventValidation="false" %>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('body').on('change', 'select', function () {
            var currentControl = this
            //Need to add checks here that we have selected values etc.
            var allBoxes = $('select');
            var fire = true
            allBoxes.each(function () {
                if (this.value == "0") {
                    fire = false;
                }
            });
            if (fire == true) {
                var relevantControlPrompt = 'Asset Type';
                var relaventspanText = $('label[for="' + currentControl.id + '"]').children("span").html();
                if (relaventspanText == relevantControlPrompt) {
                    fire = true;
                } else {
                    fire = false;
                }
            }
            if (fire == true) {
                $('input[value="View Report"]').click();
            }
        });
    });
</script>

How does it work? It relies on the way that SSRS renders the report page. SSRS programmatically generates the controls on the form. We don't know the name/id of the controls before we start looking at the HTML that is generated. However Microsoft kindly took advantage of the HTML "for" attribute which links form prompt labels with their relevant input control. So in jQuery I can take advantage of this.
SSRS a) Creates a list of controls with prompts stored in labels. b) the labels are flagged with "for" with a pointer to the relevant select drop down list box. c) using jQuery we can now find out if the current Select is the one that we want to watch. In the example below, we have chosen to look at the "Asset Type" drop down. ...snippet from outputted html by SSRS
<label for="ctl32_ctl04_ctl06_ddValue"><span>Asset Type</span></label>
the relevant select looks like this
<select name="ctl32$ctl04$ctl06$ddValue" id="ctl32_ctl04_ctl06_ddValue" style="width: 182px;">
So the label has a "for" attribute "ctl32_ctl04_ctl06_ddValue" which points to the id of the relevant select. The code will run for every select on the form but will only refresh the report for the chosen select. The selector uses the jquery syntax to search for a label with the for attribute = to my control id. I then get the child span and then get its html. This should then say "Asset Type" or whatever is the relevant select for your requirement.
var relaventspanText = $('label[for="' + currentControl.id + '"]').children("span").html();
Note: The javascript also includes logic to suppress refreshing until all values have been selected, if that is required.