Search KB Filter article types
HtmlBox CodeSample - Printing the contents of one HtmlBox only
Categories: DHTML, HTML
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
Revision: 1
Posted: 22/Feb/2010
Updated: 23/July/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample

Overview

Printing the contents of the whole browser window is simple enough by just invoking a JavaScript call to "windows.top.print()".

Printing the contents of one HtmlBox only, introduces a few challenges.

This demo application will show you how to print the contents of the whole browser window and also how to print the contents of one HtmlBox only.

Samples of useVB.NET Code
Private Sub btnHtmlBoxOnly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHtmlBoxOnly.Click
    Me.InvokeScript("document.getElementById(""TRG_" + Me.HtmlBox1.ID.ToString() + """).contentWindow.focus();" + _
        "document.getElementById(""TRG_" + Me.HtmlBox1.ID.ToString() + """).contentWindow.print();")
End Sub

C# Code
private void btnHtmlBoxOnly_Click(object sender, System.EventArgs e)
{
    this.InvokeScript("document.getElementById(\"TRG_"
                    + this.HtmlBox1.ID.ToString() + "\").contentWindow.focus();" 
                    + "document.getElementById(\"TRG_"
                    + this.HtmlBox1.ID.ToString() + "\").contentWindow.print();");
}

The demo applicationHtmlBox with data from another domain

No browser's security will allow you to invoke (Java)Scripts in another domain, so if your HtmlBox contains data from another domain, you will not be able to use this method, as you will get browser permission or access errors.

How exactly you would accomplish that task is beyond the scope of this article.

For the purpose of the demo, a simple .aspx webform is included and shown in the HtmlBox of the demo application.

Cross browser compatibility

The JavaScript used in the code, is one found to be cross browser compatible, and has been tested on Internet Explorer 8, FireFox 3.6 and Chrome.

Visual WebGui version compatibility

The demo application is written and compiled for version 6.4 and above of Visual WebGui. It will not compile correctly in version 6.3, and will give errors on the InvokeScript() method that was implemented in 6.4 and allows you to invoke JavaScript code directly. However, the method to use in pre 6.4 versions is to register a client action on some control, like a button, by calling it's RegisterClientAction() method. Both methods of invoking JavaScript are included in the demo, and to compile in 6.3, you need to comment out the relevant sections containing the version 6.4 specific code, which in turn makes two of the four buttons inactive.

A second reason for compiler errors when compiled in version 6.3 is for HtmlBox.ID to get the unique identifier for the HtmlBox. Replace the "HtmlBox.ID" with "HtmlBox.Guid" and it should compile. The "Guid" propertiy is not shown in Intellisense in the editor in 6.3 but it does exist all the same and should give no errors. Please note that in C# it is case sensitive.

Demo application download

Demo application - Printing HtmlBox contents

Tips and TricksHtmlBox is in an IFrame

Each HtmlBox is rendered to your browser in HTML within an IFrame. To print the contents of the IFrame, you must first locate it. To locate it, you use GetElementById() JavaScript/DOM funtion. The actual Id will be the string "TRG_x", where x is the HtmlBox.Id() and you will see in the code how this is used.

First focus then print

To print the contents of an IFrame only, you need to first focus() on it and then you can print() it.

SDK Version highlights
  • InvokeScript() method was implemented in 6.4 and you need to comment out portions of code to compile on pre 6.4 versions.
  • Version 6.3.x general notes - Please view the Downgrade Changelog article for what further actions might be required to use this code in version 6.3.x
RererencesForum discussons

About the author

Related Articles

DHTML  
Title Update Author
The following code integrates google maps into Visual WebGui by creating a GoogleMap control which can be used as any Visual WebGui control.
Tags: Architects, Developers
05/Jan/2007    2007/01/05
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, DHTML, v6.3, v6.4 and Later
30/Oct/2010    2010/10/30
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Integration, Pre v6.3, v6.3, v6.4 and Later
14/Nov/2010    2010/11/14
Tags: Architects, Developers, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
23/July/2010    2010/07/23
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
23/July/2010    2010/07/23
Tags: Developers, 1. Beginner, Integration, Silverlight, Pre v6.3, v6.3, 2. Intermediate, 3. Advanced
23/July/2010    2010/07/23
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS