Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Call A Page Method from a Subsequent Page

0.00/5 (No votes)
17 Jun 2011 1  
Back-tracking to make sure code is processed before the current page executes
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):

VB
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here