CodeSnip: Opening Crystal Reports in a New Window in ASP.NET
page 1 of 1
Published: 15 Sep 2004
Unedited - Community Contributed
This snippet briefly demonstrates how to open new windows with Javascript and ASP.NET.
by Richard Dudley
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26773/ 33

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 _
& "'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 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 method can be found at

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.

User Comments

Title: Open PDF in new Window   
Name: Miles
Date: 2008-04-04 1:30:40 PM
I would like to be able to generate a PDF in Crystal, then have that PDF open in a new window, can anyone help me out?


Title: good   
Name: steven
Date: 2008-03-31 7:21:15 AM
very good
Title: Good One   
Name: Ravindra
Date: 2008-02-27 1:20:46 AM
Its very useful article for crystal report demonstration.
Title: does anyone have this code in   
Name: smita
Date: 2008-02-19 11:48:23 PM
i need the code for binding a crystal report to webapplication using 2005. am using backend as sql server 2000
Title: using master page   
Name: angel
Date: 2007-12-17 12:34:43 PM
Hii..this is the nice solution bt wht we have to do when we r using master pages n wht do we have to do if we want to open a new window from button's click event and tht button is on the master page.
Title: Help about Header and Footer Page design   
Name: askdevelopers
Date: 2007-01-12 7:20:39 AM
how to Create Header and Footer Pages in 2.0( with out using Master Pages concept ) , to call Header and Footer Pages in My Application
Title: Anchor Tag   
Name: Richard Dudley
Date: 2006-10-12 9:40:48 AM
The problem with a link is getting the values from the settings page to the viewer page. You need some sort of postback, and the target on a link won't cause a postback.
Title: Nice solution - Accessibility Issues?   
Name: Lost n Dizzy
Date: 2006-09-07 9:28:16 AM
I have a report in vs 2005.
The report runs a couple of steps away from a search.
The search finds customers by CustID.
CustID is stored in Session.
I want the report to run when a link is clicked.
It will use the CustID to execute the report.
I would like the report to open in a new window, but I don't like the idea of using javascript (accessibility issues).
What about the target attribute of the anchor tag?
I realise that it is deprecated, but I use Transitional DOCTYPE, so can still validate (I think).
How do you make the website map hold target attribute values.
This is killing me.
Title: ASP.NET popping new windows   
Name: Steve
Date: 2006-08-18 6:52:47 AM
I searched for ages to find just what you've got here and this is exactly what I needed. Thank you
Title: OPEN link in new window   
Name: chandan
Date: 2006-05-29 12:25:58 AM
hi there
how can I OPEN link in new window by right clicking on link button and say open in new window

please reply at
Title: does anyone have this code in c#   
Name: Reinart Geronimo
Date: 2006-03-07 3:00:32 AM

Could you please post the code in c#?
Title: XP SP2   
Name: Richard Dudley
Date: 2005-12-15 9:10:54 AM

Title: Not working with Windows XP SP2   
Name: Don
Date: 2005-12-15 5:44:50 AM
When I tried this, it was blocked by Windows XP SP2. When I said to temporary allow blocked items, it stated it had to refresh, and then nothing happen. I got the original page back, not the new page containing the report.

Any suggestions.
Title: Sub report   
Name: kalpesh Rathod
Date: 2005-09-28 1:48:56 PM
Can we open subreport like new of page of particular empid. when click on this empid's subreport link
Title: Works Fine   
Name: Mahesh
Date: 2005-08-01 3:04:35 AM
Its working great,very useful code.But it would have been greater, if there was a c# sample code with this.I struggled a bit to make it work in c#.
Title: Mr   
Name: Jay
Date: 2005-05-30 10:12:29 PM
I have tried this code but it's not working in my project
Please let me know.

Title: Thanks   
Name: Linh Ta
Date: 2005-05-18 2:53:06 PM
work great
Linh Ta
Title: SeparatePages and Header/Footer   
Name: Richard J. Dudley
Date: 2005-04-20 8:45:59 AM
Please see:
Title: CodeSnip: Opening Crystal Reports in a New Window in ASP.NET   
Date: 2005-04-20 3:43:39 AM

If i set the 'SeperatePages' property of crystal report viewer to 'false' and open the report in second page, will i get the page header and footer for each page??? Or it will come only once at the top and bottom of the report.

Thanks and Regards
Title: not working   
Name: Lesviper
Date: 2005-04-12 7:02:42 AM
I am trying to use your code on a button but it is coming up with error within the browser of invalid argument. Any help please
Title: crystal reports   
Name: Kesavan
Date: 2005-04-03 11:30:40 PM
i need one help.i hv to design crystal report in report hv main and sub reports and i hv get the data in both main and subreport i browser.

But i want to come back from sub report to main report in the browser.

Title: Works Great!   
Name: JG
Date: 2005-03-14 10:36:20 PM
Excellent! Thanks so much.
Title: CodeSnip: Opening Crystal Reports in a New Window in ASP.NET   
Date: 2005-03-13 9:08:02 PM
If my report is located in another report server, Is it I can only use the URL to pass the report parameters? How to hidden these parameters from user? Because user can display the report by pass my application by copy and paste the url. Thanks
Title: Useful Snip   
Name: John
Date: 2004-09-22 2:47:39 PM
Works like a charm. Thanks for the useful code and article!
Title: CodeSnip: Opening Crystal Reports in a New Window in ASP.NET   
Name: Richard Dudley
Date: 2004-09-15 11:39:34 AM
When cutting and pasting the code, remember to remove the spaces between the letters in the 'script' tags. The spaces are there only so this CMS system will display the code properly.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-18 4:02:02 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search