Practical ASP.NET

Supporting a Printer-Friendly Page Button (Part 2)

Peter wraps up his solutions for getting junk off the page when the user wants to print out a copy. This time by working with Themes and JavaScript.

In last week's column (Supporting a "Printer Friendly Page" Button: Part I), I posed the problem of how to create a printer-friendly version of a Web page. I assumed that this meant getting rid of most -- but not all -- of the stuff on your Master Page (copyright notices and legal disclaimers would have to remain, for instance). I also assumed that you'd want to offer this button on every page on your site, rather than have to deal with it on a case by case basis. The solution I proposed last week involved swapping in a new Master Page with the minimum number of controls on it at run time, when the user clicked on a "Printer Friendly Version" button. This week I'll look at two other solutions.

Use the Theme
My second solution is to swap in a different Theme rather than a different Master Page. The structure of this solution is the same as the Master Page solution I proposed last week, but with this code in the PreInit event.

Protected Sub Page_PreInit(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.PreInit
        If Me.Request.QueryString("PrinterFriendly") = "True" Then
            Me.Theme = "PrintingDisplay"
        End If
End Sub

Here you'll want to create a new theme that uses CCS rules to remove items from your page or adjust their location. Your existing stylesheet is probably already controlling the appearance and placement of your controls so you just need to tweak those settings as part of creating your new theme. For instance, these two rules control the tags with the ID LogoImage (an Image control on my MasterPage) and Main (a ContentPlaceHolder control):

#LogoImageDiv {position: absolute; 
     left: 10; top: 30; 
     width: 175; height: 44 }
#MainPlaceHolderDiv {position: absolute; 
   left: 300; top: 175; 
   width: 400; height: 300 }

Here are the same two rules but the LogoImage rule now makes the Logo disappear and the ContentPlaceHolder rule moves it closer to the Page's upper left-hand corner:

#LogoImageDiv {visibility:hidden}
#MainPlaceHolderDiv{position: absolute; 
  left: 50; top: 50; 
  width: 400; height: 300 }

As with the Master Page solution, you'll need to keep your default and printer-friendly themes synchronized. At the very least, if you add anything to your Master Page that you don't want on your printer-friendly page, you'll need to update both stylesheets. Depending on the differences between your printer-friendly theme and your standard theme, this solution might be easier or harder to maintain than having a second Master Page. Unlike the Master Page solution, you can't add new controls to the printer-friendly version of the page.

Use JavaScript
My third solution is to add some JavaScript to your Master Page that does what the stylesheet in the previous solution does: make some items disappear and others move around. The JavaScript equivalent to the CSS in my previous example would look like this:

‹script type="text/javascript"›
        function PrinterFriendly() 
        {
            var logo = document.getElementById("LogoImageDiv");
            var main = document.getElementById("MainPlaceHolderDiv");
            logo.style.visibility = "hidden";
            main.style.left = 50;
            main.style.top = 50;
        }
‹/script›

To give the user the ability to trigger the JavaScript, you'd drag an HTML Input Button onto the page and set its onclick attribute to the name of your JavaScript function:

‹input id="PrinterFriendly" type="button" value="button" 
            onclick="return PrinterFriendly()" /›

In many ways this solution is the best of the three that I've presented, as it doesn't require a trip to the server to redisplay the page. Furthermore, the whole solution is encapsulated within the master file and doesn't require another file to be added to the site. You will still have some synchronization work: As items are added or removed from the Master Page, the JavaScript will need to be updated. And the JavaScript is sufficiently simple that even a JavaScript-phobic developer like me can probably work with it.

However, because you are using client-side code, you do need to recognize that executing JavaScript is browser dependent (though this code ran successfully in IE 7 and FireFox 3.0). Unlike the Theme-based solution, there is a penalty for removing controls from the Master page. Your stylesheets won't mind having a rule with no corresponding control, but JavaScript code will fail if you remove a control it's trying to modify.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube