Sunday, November 15, 2009

SQL Server Report Viewer Autosize

In Visual Studio, report viewer control for web is used control for displaying Microsoft SQL Server Reporting Services reports on an ASP.NET page. When the report viewer control is dropped on a web page, it will be displayed in the default size; width:400px and height:400px. That will not fit for most of the live reports.




We can set the width of the report viewer control to 100% or to a desired size, because the width of the report won't change dynamically. if the width is specified as 100%, that means the report viewer should fit to 100% of the browser width. If the report's width exceeds the browsers width, scroll bar will be displayed inside the report viewer control. Setting the height property to 100% will not work and we are not able to predict how many records will come on a single page (that is the required height of the report viewer control). So this will bring a horizontal scroll bar inside the report viewer control. This is not the expected behavior of a report page normally. If the report exceeds the width and height of the browser, the scroll bars should come inside the browser.



So what is the solution? Either report viewer should resize automatically to fit the content or we have to set the size of the report viewer dynamically.

Report Viewer control has a property FitToContent. The description of this property says it will expand/shrink according to the report content. However it will work only with another property AsyncRendering. This should be true.

When I worked with report viewer and experienced the same situation, I tried to use the above properties. Unfortunately a host of javscript errors prevented me to use these properties for my purpose. I searched all over the internet for a solution. But nothing solved my issue. So I started to investigate. I knew that writing some javascript to dynamically set the size of the report control is the solution to the problem. But how do I know the size of the report content? That was the big issue.

I examined the client side source of the page after the report is rendered. I scratched my head for some days to find the following information. Report viewer uses a number of iframes and divs to display reports. Report viewer itself is a div with id as same as the report viewer control. So setting the height & width to this div will set the size of the report. Again, reporting services report has two sections. The first one displays the input fields (parameters) and the view report button. The second part is the actual report. The next part of the investigation was to find the container of the report. It was not an easy task, because the report viewer control generates a lot of script on client side. This container is an iframe with id "ReportFrame" + ReportViewer1.ClientID. Here ReportViewer1 is the name of the report viewer control. But that was not the end of the research. This control again contained other containers. Finally I found that one div with id="oReportDiv" is the actual report container and it has the actual width and height. Then I had to establish the relation between the report frame and the report div. After hours of experiments the relation evolved as
div=reportFrame.document.getElementById("report").contentDocument.getElementById("oReportDiv").

Okay, now I have to implement this in the onload event. The resultant script was a very simple script as follows.

window.onload=function()
{
var viewer = document.getElementById("ReportViewer1");
var frame = document.getElementById("ReportFrame<%=ReportViewer1.ClientID %>");
if (frame != null && viewer != null)
{
var reportDiv = eval("ReportFrame<%=ReportViewer1.ClientID %>").document.getElementById("report").contentDocument.getElementById("oReportDiv");
viewer.style.height = reportDiv.scrollHeight;
viewer.style.width = reportDiv.scrollWidth;
}

}


And this solved my issue.

20 comments:

Anonymous said...

How to use this??

podamone said...

Thanks. This helped me.

Anonymous said...

Hi Shyju,

how to use this?? please let me know. i have the same issue.

Regards,
Umesh

Unknown said...

Thanks!
Really good work!

Aparna Nittala said...

Thank you very very much !!

I have been fighting to resolve this problem of scroll bars in IE and the entire report getting smashed in Firefox.

After much search, I stumbled on your website, and with few tweaks and JQuery, the scrollbars are done for and in Firefox, reports look splendid.

I used content editor webpart on sharepoitn to add javascript. I also have "PrintFrame" and "oReportDiv" when I saw the source code (with Asynchronous rendering off). With Async rendering turned on, I had only ReportFrame and no oReportDiv.

I turned it off, and with Jquery, I got the width and height of each oReportDiv element and applied that to each report viewer.

Thank you so very much, once again.
Best,
Aparna

Unknown said...

test

naba1407 said...

Hi Shyju,

Thanks a lot.I am searching it for a long time.It solved my problem by little change in code, passing clintid from serverside and keep my report name rptViewer instead of ReportViewer1 as all my code done with rptViewer


I change in your javascript

function func(id)
{

var viewer=document.getElementById(id);
.......



server side code:

string func = "func(" + Convert.ToString(rptViewer.ClientID) + ")";
rptViewer.Attributes.Add("onload", func);

Anonymous said...

Hi,

I have a similar problem with Report Viewer 2010... But we don't understand completely your Solution... Can You post a complete demo from this?

Thank you!!!

Unknown said...

hi
how to vary the report header height dynamically in a report viewer control.

can u plz help!

Anonymous said...

Hi Shyju's

Can u pls explain what " document.getElementById("Report") " stands for???

Anonymous said...

Great! Thank You very much

Anonymous said...

Thank you. Fantastic job.

EDI VAN said...
This comment has been removed by a blog administrator.
POS Software said...
This comment has been removed by a blog administrator.
osushi japanes restaurant said...
This comment has been removed by a blog administrator.
Ashrafur Rahaman said...

That is great. Thank you for sharing.

I needed to modify the code a little bit for cross browser support. Here it is:

var viewer = document.getElementById("ReportViewer1");
var frame = document.getElementById("ReportFrame<%=ReportViewer1.ClientID %>");
if (frame != null && viewer != null)
{
var iframeDiv = eval("ReportFrame<%=ReportViewer1.ClientID %>").document.getElementById("report");
var innerDoc = iframeDiv.contentDocument || iframeDiv.contentWindow.document;
var reportDiv = innerDoc.getElementById("oReportDiv");
$("#ReportViewer1").height(reportDiv.scrollHeight);
}

I am still facing one problem. If I have multiple page, the page is not resizing when I an going to next page. I am not sure where from I need to call the code after next or previous button click.

Do you have any idea?

Anonymous said...

I do not know if it's just me or if everyone else encountering problems with your blog. It looks like some of the text within your content are running off the screen. Can someone else please comment and let me know if this is happening to them as well? This may be a issue with my internet browser because I've had this happen before.
Cheers

Feel free to visit my site :: http://www.elitescort.me/ticodujxomo346

Hany Ouf said...

using jquery try this magic line:
$('div[id*="oReportDiv"]').eq(0).css('overflow', 'auto');

Anonymous said...

My namе's Ellie from Moncestino, Italia and I have to say your article is very thoughtful. The clarity of your article is quite good and I can suppose you are an expert on this subject. With your authorization, would you allow me to grab your RSS feed to keep up-to-date with forthcoming articles? Thanks a million and please carry on the fantastic work.

my weblog ... wounded

Anonymous said...

electronic cigarettes, electronic cigarette, ecig, electronic cigarette, electronic cigarettes, e cigarette