AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=525&pId=-1
CodeSnip: Opening Crystal Reports in a New Window in ASP.NET
page
by Richard Dudley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26942/ 23

To accomplish this, you need two ASPX pages and a Crystal Report.

The first page is our "launch" page.  This can be a simple listing of reports with a button that will run the report, or it can be an advanced page where you collect report parameters.  Whatever you do, you will need some event to actually launch the report.  In my projects, I use a button labelled "Run Report". In the subroutine that handles the button's OnClick event, I assign any parameters to session variables or create a querystring, and use the following JavaScript to open a new browser window containing the viewer page:

Dim strPop As String = "< s c r i p t language='javascript'>" & vbCrLf _
& "window.open('viewpage.aspx','Report');" & vbCrLf _
& "< / s c r i p t >" & vbCrLf


Page.RegisterStartupScript("Pop", strPop)

The first part of this code creates a small JavaScript that will automatically open a new window containing viewerpage.aspx.  This method also has a second parameter, which is the name of the new window.  This name is not the title of the new page, but rather is used in the DOM hierarchy to call the new window for additional information, status or events, none of which we are doing, but you still need to name the window.  

In the example above, we are simply using the name "Report." You can edit the script to use a different window name assigned at runtime.  You will want to do this if you are opening more than one report from a launch page, since browsers will not open a second window with the same name as the one that is already open (and most browsers will replace the contents of the window).

The second part of the code above tells the ASP.NET engine to add a script to be executed when the page is loaded.  Keep in mind that the launch page will be reloaded in the client's browser after the postback, and this is what allows us to use a startup script to open a new window.

The View page should have all the code to render your report in its Page_Init.  If any parameters are used, they should be read from the session variables or querystring and passed to the report.  The View page can export reports and be used to navigate the report.

Remember on your Launch page to use If Not IsPostBack around your code that originally populates the page so you don't reset any user input.

Bonus Technique:
The window.open method can accept two more parameters.  One parameter is a string of key-value pairs that control the appearance of the new window.  You can turn off the address bar, set the height and width, etc.  A good reference for the window.open method can be found at http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/open_0.asp.

Double Bonus:
You can use this code snippet to open any type of file in a new window.  This technique is not limited to just Crystal Reports.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 12:45:18 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search