Home > .NET, ASP.NET > Printable or ReadOnly pages in ASP.NET (code snippet)

Printable or ReadOnly pages in ASP.NET (code snippet)

You have an ASP.NET page that is full of great controls, functionality, etc. but now you need to print it or render a “readonly” view of the page. Wouldn’t it be nice if there was a little ReadOnlyproperty that you could flip to true on the page and that was it? Well … there isn’t. However with a little recursive code, you can achieve the same effect.

This code snippet is provided to help a few other soulswith our same need:
DISCLAIMER: This code is not perfect but does work as intended and is unit tested (in our application).

Click here for code snippet

The basic approach is loop through all Controls from a certain control down (Page if you choose, since Page is-a Control) and make certain control replacements based on the control that is being evaluated.

The use of Reflection can probably be avoided entirely with a bit of refactoring and use of interfaces instead. Using the string representation of the type name of the Control is a little ugly and could be improved. Also note that this code makes certain controls not visible such as Button which may not be desirable. Another approach is to derive your own controls from the base controls and provide a “ReadOnly” rendering – this is probably morework than a simple recursive loop as above.

Comments are welcome.

Jonathan Cogley isthe CEO and founder of thycotic, a .NET consulting company and ISV in Washington DC. Our product, myclockwatcher.comis a time andexpense tracking system specializedfor billable professionals built on ASP.NET, C# and SQL Server using Test Driven Development.

Categories: .NET, ASP.NET
  1. http://
    September 22, 2005 at 5:30 am

    As far as printing goes, it seems like a lot of work to do what a print CSS can do. Other than that, I got nothin’ 😉

  2. Jonathan Cogley
    September 23, 2005 at 7:20 pm

    That makes sense for many of the properties of the page but how do you convert form elements to text (such as dropdownlists, textboxes and radiobuttons) using CSS?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: