This is a migrated thread and some comments may be shown as answers.

Unable to display report viewer in ASP.Net MVC application

5 Answers 432 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 30 Mar 2012, 03:13 AM
I'm attempting to use the report viewer ASP.Net control in an ASP.Net MVC application. When I include the viewer control on a page I get the folwwoing error.


The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).


I'm using the simplest possible view to display the report viewer and which I copied from a Telerik sample that demonstrates how to use the report viewer in an ASP.Net MVC application (the sample does not run).

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
 
<%@ Register Assembly="Telerik.ReportViewer.WebForms, Version=6.0.12.302, Culture=neutral, PublicKeyToken=a9d7983dfcc261be" Namespace="Telerik.ReportViewer.WebForms" TagPrefix="telerik" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Reports
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Test Report</h2>
 
     <script runat="server">
         
        public override void VerifyRenderingInServerForm(Control control)
        {
            // to avoid the server form (<form runat="server">) requirement
        }
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
             
            // bind the report viewer
            ReportViewer1.Report = new Mine.Web.UI.Reports.HelloWorld();
        }
    </script>
    <telerik:ReportViewer runat="server" ID="ReportViewer1" />
</asp:Content>


The error is being caused by the line below in my Site.Master file. Removing that line allows the report viewer to display but breaks the application because now the stylesheets needed for the Telerik ASP.NEt MVC controls aren't being loaded.

<%: Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.metro.css").Combined(true).Compress(true)) %>

This seems like a really common scenario? Surely I'm not the first person to try and use the report viewer and the MVC extensions at the same time?

5 Answers, 1 is accepted

Sort by
0
IvanY
Telerik team
answered on 02 Apr 2012, 04:08 PM
Hello Aaron,

This is a common problem not only related to a particular product, but to the ASP.NET MVC framework in general. There is a quite simple solution for the problem. First instead of <%= use <%# - this changes the code block from a Response.Write code block to a databinding expression. Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page add the following: 
<script runat="server">
     protected void Page_Load(object sender, EventArgs e)
     {
         this.DataBind();
     }
</script>

(or add it to the code-behind of the master page).

I hope this helps.

Kind regards,
IvanY
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
0
Aaron
Top achievements
Rank 1
answered on 03 Apr 2012, 12:40 AM
Hi Ivan,

I did see that suggestion in other forums posts and had already tried it. And while using <%# .. %> does stop the error from occurring it also breaks the Telerik style sheet registration statement which means I don't get the Telerik stylesheets loading on pages anymore.

The full block I am using on my Site.Master is:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/bootstrap-responsive.min.css" rel="stylesheet" type="text/css" />
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <%# Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.metro.css").Combined(true).Compress(true)) %>
</head>

Changing the <%# .. %> back to <%: .. %> results in the stylesheets loading but causes the error when using the report viewer.

Perhaps I need to use the Html.Telerik().StyleSheetRegistrar() call differently?

Thanks,
Aaron

0
Aaron
Top achievements
Rank 1
answered on 03 Apr 2012, 01:15 AM
I finally fixed it by using the code block below.

<head>
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="<%= Url.Content("~/Content/bootstrap.min.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%= Url.Content("~/Content/bootstrap-responsive.min.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />
    <%: Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.metro.css").Combined(true).Compress(true)) %>
</head>

I'm not sure why the default MVC template sets runat="server" on the head element but since it does this will probably trip up other people as well.
0
Aaron Lewis
Top achievements
Rank 2
answered on 08 May 2012, 07:25 PM
I know this doesn't have anything to do with the post, but I thought I'd give you a heads-up anyway -- you don't need to put any controls in the title element of the head section to set the title, as long as head is set to runat="server" (and yours is). You can just set the title using Page.Title="stuff" :)
0
Vassil Petev
Telerik team
answered on 26 Mar 2013, 05:11 PM
Hi all,

We have produced a blog + video on how to use Telerik Reporting in MVC 4. Check it out. FYI, we will be working on a new web viewer for Q2 2013 which will work with MVC without the extra steps.

Enjoy.


Regards,
Vassil Petev
the Telerik team

Telerik Reporting Q1 2013 available for download with impressive new visualizations. Download today from your account.

Tags
General Discussions
Asked by
Aaron
Top achievements
Rank 1
Answers by
IvanY
Telerik team
Aaron
Top achievements
Rank 1
Aaron Lewis
Top achievements
Rank 2
Vassil Petev
Telerik team
Share this question
or