Geeks With Blogs
Ilya Verbitskiy Thoughts and links about computer programming

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

Posted on Wednesday, October 16, 2013 11:34 PM Web Development , SQL Server , ASP.NET | Back to top


Comments on this post: Microsoft ReportViewer SetParameters continuous refresh issue

# re: Microsoft ReportViewer SetParameters continuous refresh issue
Requesting Gravatar...
Yes It is working fine. Thank you so much.
Left by Muhammed Anas on Mar 17, 2014 8:15 AM

# re: Microsoft ReportViewer SetParameters continuous refresh issue
Requesting Gravatar...
I like so much this information on the reportviewer
Left by Musabile Serge on Jun 06, 2014 11:38 AM

# re: Microsoft ReportViewer SetParameters continuous refresh issue
Requesting Gravatar...
Hello,
Thanks for this blog post. I've noticed that SetParameters requires setting all parameters on report. Is there a way to just send few parameters and not all of them? Thanks
Left by Bilal Haidar on Dec 13, 2014 8:15 AM

# re: Microsoft ReportViewer SetParameters continuous refresh issue
Requesting Gravatar...
Thanks for this post. It saved my time.
Left by Mahesh on Apr 21, 2015 12:52 AM

# re: Microsoft ReportViewer SetParameters continuous refresh issue
Requesting Gravatar...
Hi i'm doing this and opening report view in a new tab but what happens is all the other page links stop working/loading until the report is loaded completely. I even set asyncrendering to true but still no result
Left by rohit on May 05, 2015 1:54 PM

Your comment:
 (will show your gravatar)


Copyright © Ilya Verbitskiy | Powered by: GeeksWithBlogs.net