Sunday, 1 April 2012

Double scrollbars in ReportViewer for IE8

Today I came across a very strange bug with reportviewer (there seem to be many) which only manifested itself in IE8.  The problem was that it created a scrollpane within a scrollpane so there were 2 sets of scrollbars.

Upon viewing the source I was able to track the problem down to the oReportDiv element which had a style attribute containing "overflow: auto;".  I could see that by removing this overflow style or setting it to "visible" the extra scroll bars would disappear.  The final solution was a challenge was the code here is contained within an iframe and I struggled with many JQuery attempts to remove the style.  Eventually I was able to remove it using the following:

    window.onload = function () {
        var viewer = document.getElementById("<%=reportViewer.ClientID %>");
        var frame = document.getElementById("ReportFrame<%=reportViewer.ClientID %>");
        if (frame != null && viewer != null) {
            var reportDiv = eval("ReportFrame<%=reportViewer.ClientID %>").document.getElementById("report").contentDocument.getElementById("oReportDiv");

An event which fires in ReportViewer after the report is generated

I searched for quite a while to find a way to run a client side script whenever the ASP.Net SSRS ReportViewer control has finished rendering/generating a report.  I could not find one documented but when looking in the report viewers resulting html source I found mention of ClientController.CustomOnReportLoaded which was set to an empty function.  Using the JQuery ready function I was able to attach code to this event and have it run only after the report was generated.

$(document).ready(function () {

document.getElementById('ctl00_ctl00_DefaultContent_AdminContent_reportViewer').ClientController.CustomOnReportLoaded = function () {

        alert('You see this after Report is Generated');

Thursday, 15 March 2012

Inline HtmlEncode for Gridview and Repeater ASP.Net Controls

Today I had an issue where some non-standard characters needed to be displayed in a table which was created by an ASP:Repeater control.  The problem was some of these symbols were angle brackets so they were messing up the HTML of the table.

I wanted a clean and simple way to HtmlEncode these so they would render correctly.  I first tried to make the Server.HtmlEncode method work inline but I didn't have much luck.  Quite a few search results on Google said it was not possible and most people suggested encoding the text inside a DataBound event.

I eventually found the answer to doing it inline, which I think is much cleaner.  My original markup was:
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>

To get this encoded I use:
<%#Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem, "FirstName"))%>

I tried many variations on this but it was the cast to string that I was missing  It's not immediately obvious that cast is required as the documentation for Eval suggests a string is returned.