Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET
Tip/Trick

Call A Page Method from a Subsequent Page

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Jun 2011CPOL2 min read 10.8K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
-- There are no messages in this forum --