Articles in this section
Category / Section

How to customize the ReportViewer in ASP.NET MVC?

6 mins read

This article describes how to customize the standard toolbar in ASP.Net MVC ReportViewer. The toolbar customization is the process of changing standard functionalities to meet the requirement.

Refer to the Getting Started with MVC ReportViewer from the existing User Guide that is available in the following link.

https://help.syncfusion.com/ug/js/documents/createyourfirstrepor1.htm

Toolbar Customization in MVC Reportviewer

Syncfusion MVC ReportViewer supports Toolbar Customization. By using toolbarSettings property, you can customize the toolbar of ReportViewer either with the template customization or without the template customization.

Toolbar Customization

You can show or hide the toolbar items of ReportViewer as per the requirement with the help of toolbarSettings property. To customize the toolbar, set the items for toolbarSettings property as the following code example.

CSHTML

<div style="width:100%">
    @(Html.EJ().ReportViewer("reportviewer")         .ProcessingMode(Syncfusion.JavaScript.ReportViewerEnums.ProcessingMode.Remote)
              .ReportPath("~/App_Data/InvoiceTemplate.rdl")
              .ReportServiceUrl(VirtualPathUtility.ToAbsolute("~/api/ReportAPI"))              .ToolbarSettings(tb=>tb.Items(Syncfusion.JavaScript.ReportViewerEnums.ToolbarItems.All
               & ~Syncfusion.JavaScript.ReportViewerEnums.ToolbarItems.Export
               & ~Syncfusion.JavaScript.ReportViewerEnums.ToolbarItems.Parameters
               & ~Syncfusion.JavaScript.ReportViewerEnums.ToolbarItems.DocumentMap
               & ~Syncfusion.JavaScript.ReportViewerEnums.ToolbarItems.PageNavigation ))           
    )
</div>

 

Run the Application

Run the sample application to see the customized toolbar of JavaScript ReportViewer as displayed in the following screenshot.

 

C:\Users\sundaraj\Desktop\ToolbarMVC.PNG

Figure 1: Toolbar customization without template

Demo Sample

You can download the Demo Sample from the following link.

http://www.syncfusion.com/downloads/support/directtrac/general/ToolbarCustomizewithouttemplate-1548070730.zip

 

Toolbar Template Customization

Toolbar Template customization helps you to customize the toolbar UI instead of standard toolbar. By using the templateId property of ReportViewer, you can hide the standard toolbar and replace the new toolbar. The following code example explains how to customize the toolbar using the template.

CSHTML

 <div id="toolbarTemplate">
     <ul>
           <li id="Print">
                  <div class="ctrlbutton">
                       <span class="printpage" style="display:block;"></span>
                   </div>
              </li>
        </ul>        
   <ul>
       <li id="Drop down" title="Dropdown for export list">
                @Html.EJ().SplitButton("select").ShowRoundedCorner(true).Size(ButtonSize.Normal)
                 .ContentType(ContentType.ImageOnly).PrefixIcon("export").TargetID("exportlist")
                 .ClientSideEvents(e => e.ItemSelected("Itemclick"))
        <ul id="exportlist">
             <li id="PDF"><span class="pdfexport" style="display:block;">Pdf</span></li>
             <li id="Word"><span class="wordexport" style="display:block;">Word</span></li>
             <li id="Html"><span class="htmlexport" style="display:block;">Html</span></li>
         </ul>
        </li>
     </ul>
   <ul>
         <li id="Gotofirst">
              <div class="ctrlgotofirst">
                  <span class="gotofirstpage" style="display:block;"></span>
               </div>
          </li>
          <li id="GotoPrevious">
               <div class="Previous">
                   <span class="gotopreviouspage" style="display:block;"></span>
                </div>
           </li>
           <li id="GotoPageNo">
                <div class="ctrlltextbox">
                   <input type="text" id="textBox" onkeyup="GotopageNo()" value="1" />
                </div>
            </li>
           <li id="GotoNext">
               <div class="Next" style="width:20px;height:20px;">
                    <span class="gotonextpage" style="display:block;"></span>
               </div>
           </li>
          <li id="GotoLast">
               <div class="ctrlgotoLast">
                    <span class="gotolastpage" style="display:block;"></span>
               </div>
           </li>
       </ul>
       <ul>
           <li id="zoomin">
               <div class="ctrlzoomin">
                    <span class="zoominpage" style="display:block;"></span>
               </div>
           </li>
           <li id="zoomout">
               <div class="ctrlzoomout">
                    <span class="zoomoutpage" style="display:block;"></span>
               </div>
           </li>
       </ul>
       <ul>
           <li id="FitToPage" title="Toggle button">
                <div class="ctrlfittopage">
                    @Html.EJ().ToggleButton("fittopage").Size(ButtonSize.Normal)
                    .ShowRoundedCorner(true).ContentType(ContentType.ImageOnly)
                    .DefaultPrefixIcon("Fittopage").ClientSideEvents(e => e.Click("fitopage"))
                </div>
           </li>
           <li id="Refresh">
               <div class="ctrlreferesh">
                    <span class="refereshpage" style="display:block;"></span>
               </div>
           </li>
       </ul>
   </div>

 

Add the following CSS code example inside the <style> tag of the razor page.

CSS

<style type="text/css">
    #reportviewer {
        width: 1025px !important;
        height: 570px !important;
      }
    .printpage {
        background: url('../Content/Icons/Print.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .gotofirstpage {
        background: url('../Content/Icons/Gotofirst.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .gotopreviouspage {
        background: url('../Content/Icons/Previous.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .gotonextpage {
        background: url('../Content/Icons/Next.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .gotolastpage {
        background: url('../Content/Icons/Gotolast.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .export {
        background: url('../Content/Icons/Export.png');
        width: 25px;
        height: 25px;
        margin-top: 5px;
        background-position: top;
        background-repeat: no-repeat;
    }
    .pdfexport {
        background: url('../Content/Icons/Pdf.png');
        width: 25px;
        height: 25px;
        background-position: left;
        background-repeat: no-repeat;
    }
    .wordexport {
        background: url('../Content/Icons/Word.png');
        width: 25px;
        height: 25px;
        background-position: left;
        background-repeat: no-repeat;
    }
    .excelexport {
        background: url('../Content/Icons/Excel.png');
        width: 25px;
        height: 25px;
        background-position: left;
        background-repeat: no-repeat;
    }
    .htmlexport {
        background: url('../Content/Icons/Html.png');
        width: 25px;
        height: 25px;
        background-position: left;
        background-repeat: no-repeat;
    }
    .zoominpage {
        background: url('../Content/Icons/Zoomin.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .zoomoutpage {
        background: url('../Content/Icons/Zoomout.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .Fittopage {
        background: url('../Content/Icons/FittoPage.png');
        width: 25px;
        height: 18px;
        background-position: center;
        background-repeat: no-repeat;
    }
    .refereshpage {
        background: url('../Content/Icons/Refresh.png');
        width: 25px;
        height: 25px;
        background-position: center;
        background-repeat: no-repeat;
    }
    #toolbarTemplate div {
        display: inline-block;
    }
    #toolbarTemplate div ul li {
            display: inline-block;
    }
    #toolbarTemplate {
        height: 40px;
        padding-top: 5px;
    }
    #textBox {
        width: 30px; height: 20px;
         text-align: center;
    }
</style>

Add the following code example inside the <script> tag of the razor page. Also set the desired ReportPath and ReportServiceUrl to ReportViewer.

JS

<script type="text/javascript">
    function OnToolbarClick(args) {
        var reportObject = $("#reportviewer").data("ejReportViewer");
        var currentTarget = args.currentTarget.id;
        var zoomvalue = reportObject.model.zoomFactor;
        switch (currentTarget) {
            case "Print":
                {
                    reportObject.print();
                    break;
                }
            case "Gotofirst":
                {
                    reportObject.gotoFirstPage();
                    document.getElementById('textBox').value = reportObject._currentPage;
                    break;
                }
            case "GotoPrevious":
                {
                    reportObject.gotoPreviousPage();
                    document.getElementById('textBox').value = reportObject._currentPage;
                    break;
                }
            case "GotoNext":
                {
                    reportObject.gotoNextPage();
                    document.getElementById('textBox').value = reportObject._currentPage;
                    break;
                }
            case "GotoLast":
                {
                    reportObject.gotoLastPage();
                    document.getElementById('textBox').value = reportObject._currentPage;
                    break;
                }
            case "zoomin":
                {
                    if (zoomvalue >= 6) {
                        reportObject.setModel({ 'zoomFactor': 6 });
                    }
                    else if (zoomvalue <= 0.5) {
                        reportObject.setModel({ 'zoomFactor': 0.75 });
                    }
                    else {
                        reportObject.setModel({ 'zoomFactor': ++zoomvalue - 0.75 });
                    }
                    break;
                }
            case "zoomout":
                {
                    if (zoomvalue <= 0.75) {
                        reportObject.setModel({ 'zoomFactor': 0.5 });
                    } else if (zoomvalue <= 1) {
                        reportObject.setModel({ 'zoomFactor': 0.75 });
                    } else {
                        reportObject.setModel({ 'zoomFactor': --zoomvalue + 0.75 });
                    }
                    break;
                }
            case "Refresh":
                {
                    reportObject.refresh();
                    document.getElementById('textBox').value = reportObject._currentPage;
                    reportObject.setModel({ 'zoomFactor': 1 });
                    break;
                }
        }
    }
    function GotopageNo() {
        var reportObject = $("#reportviewer").data("ejReportViewer");
        var currenttext = parseInt(document.getElementById('textBox').value);
        if (currenttext != null) {
            var temp = reportObject._pageModel.TotalPages;
            if (currenttext <= temp) {
                reportObject.gotoPageIndex(currenttext);
            }
        }
    }
    function fitopage(args) {
        var reportObject = $("#reportviewer").data("ejReportViewer");
        if (args.isChecked == true) {
            reportObject.fitToPage();
        }
        else {
            reportObject._resetPage();
        }
    }
    function Itemclick(args) {
        var reportObject = $("#reportviewer").data("ejReportViewer");
        var currentTarget = args.ID;
        switch (currentTarget) {
            case "Word":
                reportObject.exportReport("Word_Word2010");
                break
            case "Excel":
                reportObject.exportReport("Excel_Excel2010");
                break
            case "PDF":
            case "Html":
                reportObject.exportReport(currentTarget);
                break
        }
    }
</script>

 

C:\Users\sundaraj\Desktop\ToolbarMVC1.PNG

Figure 2: Toolbar Template Customization

Demo Sample

You can download the Demo Sample from the following link.

http://www.syncfusion.com/downloads/support/directtrac/general/ToolbarCustomization.MVC-467950952.zip

 

Further References

You can find the online User Guide Documentation and online Demo Samples for MVC ReportViewer control from the following link.

http://mvc.syncfusion.com/demos/web/reportviewer/default

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied