Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET

An Open Source RDL Engine

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
20 Dec 2010CPOL3 min read 83.7K   3.5K   55  
An Open Source RDL engine for rendering reports to WinForms or ASP.NET
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using System.IO;

namespace RdlAsp
{
    public partial class ReportViewer : System.Web.UI.WebControls.WebControl, ICallbackEventHandler
    {
        protected string _RenderType = "html";
        protected Rdl.Render.RenderToHtml _renderedReport = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            _renderedReport = (Rdl.Render.RenderToHtml)Page.Session["RenderedReport"];

            if (_renderedReport != null)
            {
                Page.Header.Controls.Add(new LiteralControl(
                    "<style type='text/css'>\n" +
                    _renderedReport.Style +
                    "</style>"));

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ReportScript", 
                    "<script language=\"javascript\">\n" +
                    _renderedReport.Script +
                    "</script>");
            }

            string cbReference = Page.ClientScript.GetCallbackEventReference(
                this, "arguments", "ToggleStateData", "context");
            Page.ClientScript.RegisterClientScriptBlock(
                this.GetType(), "ToggleState",
                "function ToggleStateCallback(arguments, context) {" + cbReference + "}", true);
        }

        #region ICallbackEventHandler Members

        protected override void RenderContents(HtmlTextWriter writer)
        {
            string body = html;
            if (_renderedReport != null)
                body = body.Replace("<%report%>", _renderedReport.Body);
            else
                body = body.Replace("<%report%>", string.Empty);
            writer.Write(body);
        }

        public void SetReport(Rdl.Render.RenderToHtml renderedReport)
        {
            _renderedReport = renderedReport;
            Page.Session["RenderedReport"] = _renderedReport;

            Page_Load(null, null);
        }

        public string GetCallbackResult()
        {
            return null;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            string[] args = eventArgument.Split(null);

            Rdl.Render.RenderToHtml htmlReport = (Rdl.Render.RenderToHtml)Context.Session["RenderedReport"];

            // Find the named text element and set the toggle state.
            Rdl.Render.TextElement te = (Rdl.Render.TextElement)htmlReport.SourceReport.FindNamedElement(args[1]);

            if (te != null)
                te.ToggleState = (Rdl.Render.TextElement.ToggleStateEnum)Enum.Parse(typeof(Rdl.Render.TextElement.ToggleStateEnum), args[2]);
        }

        #endregion

        private string html = @"
<script language=""javascript"">
function ToggleStateData(value)
{
}

function ToggleState(textBoxName, toggleState)
{
    ToggleStateCallback( textBoxName + ' ' + toggleState );
}

function ExportOnChange(selectedIndex) 
{
    var reportKey = document.getElementById(""LabelReportID"").innerText;
    if (selectedIndex == 1)
    {
        var url = ""PdfExport.rdlx"";
        window.open(url,""_blank"","""");
        window.opener=top;
    }
    if (selectedIndex == 2)
    {
        var url = ""XlsExport.rdlx"";
        window.open(url,""_blank"","""");
        window.opener=top;
    }
    if (selectedIndex == 3)
    {
        var url = ""TxtExport.rdlx"";
        window.open(url,""_blank"","""");
        window.opener=top;
    }
    if (selectedIndex == 4)
    {
        var url = ""CsvExport.rdlx"";
        window.open(url,""_blank"","""");
        window.opener=top;
    }
    document.getElementById(""Export"").selectedIndex = 0;
}    

function printClick()
{
    var url = ""Print.rdlx"";
    window.open(url,""_blank"","""");
    window.opener=top;
}

</script>

<asp:Label ID=""LabelReportID"" runat=""server"" Text="""" style=""display:none;""></asp:Label>
Export To:<select id=""Export"" onchange=""ExportOnChange(this.selectedIndex)"">
    <option selected=""selected"">---</option>
    <option>PDF</option>
    <option>Excel</option>
    <option>Text</option>
    <option>CSV</option>
</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type=""button"" id=""btnPrint"" onclick=""printClick()"" value=""Print..."">
<div style=""display: inline; position: static;"">
    <%report%>
</div>
";
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sawiki Software
United States United States
I have been a professional software developer for 25+ years, most of it supporting the business community of Maine. I have been working with MS dotnet since 1.0 beta. I organized Sawiki Software LLC as an outlet for some open source software that I have been working on.

Comments and Discussions