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

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.



User Comments

Title: Open PDF in new Window   
Name: Miles
Date: 2008-04-04 1:30:40 PM
Comment:
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?

TIA,

Miles
miles@milestone.ca
Title: good   
Name: steven
Date: 2008-03-31 7:21:15 AM
Comment:
very good
Title: Good One   
Name: Ravindra
Date: 2008-02-27 1:20:46 AM
Comment:
Its very useful article for crystal report demonstration.
Title: does anyone have this code in vb.net   
Name: smita
Date: 2008-02-19 11:48:23 PM
Comment:
i need the code for binding a crystal report to webapplication using vb.net 2005. am using backend as sql server 2000
Title: using master page   
Name: angel
Date: 2007-12-17 12:34:43 PM
Comment:
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
Comment:
hi
how to Create Header and Footer Pages in Asp.net 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
Comment:
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
Comment:
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.
Cheers
Title: ASP.NET popping new windows   
Name: Steve
Date: 2006-08-18 6:52:47 AM
Comment:
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
Comment:
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
vedcyrus@yahoo.com
Title: does anyone have this code in c#   
Name: Reinart Geronimo
Date: 2006-03-07 3:00:32 AM
Comment:
Mahesh,

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

See http://aspadvice.com/blogs/rjdudley/archive/2005/12/15/14341.aspx.
Title: Not working with Windows XP SP2   
Name: Don
Date: 2005-12-15 5:44:50 AM
Comment:
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
Comment:
Can we open subreport like that...in 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
Comment:
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
Comment:
I have tried this code but it's not working in my project
Please let me know.

Thanks
jay
jay_email04@yahoo.com
Title: Thanks   
Name: Linh Ta
Date: 2005-05-18 2:53:06 PM
Comment:
work great
Thanks,
Linh Ta
Title: SeparatePages and Header/Footer   
Name: Richard J. Dudley
Date: 2005-04-20 8:45:59 AM
Comment:
ashug@aztec.soft.net,
Please see:
http://blogs.aspadvice.com/rjdudley/archive/2005/04/20/3442.aspx
Title: CodeSnip: Opening Crystal Reports in a New Window in ASP.NET   
Name: ashug@aztec.soft.net
Date: 2005-04-20 3:43:39 AM
Comment:
HI,

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
Comment:
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
Comment:
i need one help.i hv to design crystal report in asp.net.this 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.

thanks
keshav...
Title: Works Great!   
Name: JG
Date: 2005-03-14 10:36:20 PM
Comment:
Excellent! Thanks so much.
Title: CodeSnip: Opening Crystal Reports in a New Window in ASP.NET   
Name: KWWONG
Date: 2005-03-13 9:08:02 PM
Comment:
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
Comment:
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
Comment:
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 ASPAlliance.com  |  Page Processed at 2024-04-19 6:59:18 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search