ReportViewer control in MVC

Continuing from my previous post, I wanted to make my reports available in my ASP.NET MVC site. I read about the ReportViewer control that it works only on webformst (ASPX pages), due to viewstate requirements. Luckily, webforms ges can be added to an MVC project. But when I started passing values to the report parameters, the page started some kind of inifite loop. That was because I had forgotten about webforms development and that you must often have a check for postback in code-behind. Like this:

    public partial class ReportWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReportViewer1.ServerReport.ReportServerUrl = new Uri(Properties.Settings.Default.ReportServerUrl);
                var userUnit = Session[LoginModel.EnhetSessionKey] as Enhet;
                if (userUnit == null)
                {
                    ReportViewer1.ServerReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter("Unit", "[Organisation].[Organisation-Enhet].[All]"));
                }
                else
                {
                    ReportViewer1.ServerReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter("Unit", string.Format("[Organisation].[Organisation-Enhet].[Enhet].&[{0}]", userUnit.Id)));
                }
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s