Microsoft ReportViewer SetParameters continuous refresh issue

I am a big fun of using ASP.NET MVC for building web-applications. It allows us to create simple, robust and testable solutions. However, .NET world is not perfect. There is tons of code written in ASP.NET web-forms. You cannot simply ignore it, even if you want to.

Sometimes ASP.NET web-forms controls bring us non-obvious issues. The good example is Microsoft ReportViewer control. I have an example for you.

1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

2: <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

3:  

4:

5:  

6: <html xmlns="http://www.w3.org/1999/xhtml"\>

7: <head runat="server">

8: <title>Report Viewer Continiuse Resfresh Issue Example</title>

9: </head>

10: <body>

11: <form id="form1" runat="server">

12: <div>

13: <asp:ScriptManager runat="server"></asp:ScriptManager>

14: <rsweb:ReportViewer ID="_reportViewer" runat="server" Width="100%" Height="100%"></rsweb:ReportViewer>

15: </div>

16: </form>

17: </body>

18: </html>

The back-end code is simple as well. I want to show a report with some parameters to a user.

1: protected void Page_Load(object sender, EventArgs e)

2: {

3: _reportViewer.ProcessingMode = ProcessingMode.Remote;

4: _reportViewer.ShowParameterPrompts = false;

5:  

6: var serverReport = _reportViewer.ServerReport;

7: serverReport.ReportServerUrl = new Uri("http://localhost/ReportServer\_SQLEXPRESS");

8: serverReport.ReportPath = "/Reports/TestReport";

9:  

10: var reportParameter1 = new ReportParameter("Parameter1");

11: reportParameter1.Values.Add("Hello World!");

12:  

13: var reportParameter2 = new ReportParameter("Parameter2");

14: reportParameter2.Values.Add("10/16/2013");

15:  

16: var reportParameter3 = new ReportParameter("Parameter3");

17: reportParameter3.Values.Add("10");

18:  

19: serverReport.SetParameters(new[] { reportParameter1, reportParameter2, reportParameter3 });

20: }

I set ShowParametersPrompts to false because I do not want user to refine the search. It looks good until you run the report. The report will refresh itself all the time.

The problem caused by ServerReport.SetParameters method in Page_Load. The method cause ReportViewer control to execute the report on the NEXT post back. That is why the page has continuous post-backs.

The fix is very simple: do nothing if Page_Load method executed during post-back.

1: protected void Page_Load(object sender, EventArgs e)

2: {

3: if (IsPostBack)

4: {

5: return;

6: }

7:  

8: _reportViewer.ProcessingMode = ProcessingMode.Remote;

9: _reportViewer.ShowParameterPrompts = false;

10:  

11: var serverReport = _reportViewer.ServerReport;

12: serverReport.ReportServerUrl = new Uri("http://localhost/ReportServer\_SQLEXPRESS");

13: serverReport.ReportPath = "/Reports/TestReport";

14:  

15: var reportParameter1 = new ReportParameter("Parameter1");

16: reportParameter1.Values.Add("Hello World!");

17:  

18: var reportParameter2 = new ReportParameter("Parameter2");

19: reportParameter2.Values.Add("10/16/2013");

20:  

21: var reportParameter3 = new ReportParameter("Parameter3");

22: reportParameter3.Values.Add("10");

23:  

24: serverReport.SetParameters(new[] { reportParameter1, reportParameter2, reportParameter3 });

25: }

You can download sample code from GitHub - https://github.com/ilich/Examples/tree/master/ReportViewerContinuousRefresh

This article is part of the GWB Archives. Original Author: Ilya Verbitskiy

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.