Microsoft ReportViewer SetParameters continuous refresh issue
Posted
by Ilya Verbitskiy
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Ilya Verbitskiy
Published on Wed, 16 Oct 2013 16:34:54 GMT
Indexed on
2013/10/17
16:00 UTC
Read the original article
Hit count: 559
Originally posted on: http://geekswithblogs.net/ilich/archive/2013/10/16/microsoft-reportviewer-setparameters-continuous-refresh-issue.aspx
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: <!DOCTYPE html>
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
© Geeks with Blogs or respective owner