HtmlBox CodeSample - Printing the contents of one HtmlBox only

From Visual WebGui Wiki

Jump to: navigation, search


Contents

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 use

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

HtmlBox 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 Tricks

HtmlBox 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

Rererences

Forum discussons

Personal tools