I'm working on a series of ASP.Net web pages for an existing site, and part of that task is to provide a way for the user to print the content of the page. Due to the architecture of the pages, it was convenient to create a base class that handled common functionality. I initially got the printing working on all of the pages via a button on each page) and its associated click handler, but I decided that I wanted to display the printable content in a separate window.
I found that you couldn't "redirect" to a different window, and the javascript solutions I found were - umm... - javascript (and some were problematic to boot). I started messing around in intellisense and found the
PreviousPage
property. In order to implement my idea, I had to make a couple of changes:
0) Create a new
PrintContent
page.
1) Add the PostbackUrl property to the aspx Button element on all of my other pages, and set the value to the newly created
PrintContent
page.
2) Modify the base page object to include an (empty) overridable method called
PrepareToPrint()
method
3) Override that new method in any page that had the ability to print its content.
4) Moved my print button event handler code to the new overriding method
After all that was done, I added the following code to the
Page_Load
event handler in the
PrintContent
code behind (sorry, I'm using VB so that's what you're gonna get here):
CType(Page.PreviousPage, MyBasePage).PrepareToPrint()
CreateWordDoc()
Essentially, we're using polymorphism to execute the appropriate version of the
PrepareToPrint()
method on the previous page. If I was only using this aspx page from a single page, I could just cast
PreviousPage
to the actual class in the code behind for the given page.
This tip illustartes that a) you can have your cake and eat it to, and b) don't be afraid to look outside the box to which you're currently confining yourself.