Printing Primer

Get the Help You Need

asp:Q&A

LANGUAGES: VB.NET

ASP.NET VERSIONS: 1.x

 

Printing Primer

Get the Help You Need

 

By Josef Finsel

 

This month s column is one that has a simple answer and a complex answer, depending on how you want to handle it.

Q: Is there a fairly easy way to print a Web form in VB.net (http://vb.net) using a Print button? I am modifying someone else s program and I would like to add a Print button to an ASP Web form. I intend to use an onClick event to make the Print button invisible (either in JavaScript or code-behind) during the print process, and then, I think, handle the printing itself in the code-behind.

 

A: The short answer to this is to add a button with a very small snippet of JavaScript code that says:

 

window.print

 

That command, the equivalent of selecting File | Print, opens the printer dialog box to allow the user to select where they want it to print. But there are problems with this; the most notable one being that Web pages don t always translate well into printed objects. For instance, take a multi-line text box. It s easy to fill the text box with text that causes some of the text to scroll out of site. When you print that Web page, the printout will show only as much text as you see displayed in the Web page.

 

To get around this, a Print button usually opens up a new window with the data formatted for printing. And that s just what we re going to do this month, beginning with the BasicForm.aspx form (available for download; see end of article for details).

 

BasicForm.aspx is a simple form with several different inputs: a couple of TextBoxes, a DropDownList, a multi-line TextBox, a RadioButtonList, and a CheckBoxList. This variety will cover just about everything necessary to build upon the code we re going to use to store the values and open up a printable form. This is all handled through the SaveControlInfo procedure, called by the Print button on the SaveControlInfo(Me.Controls) form (see Figure 1).

 

Private Sub SaveControlInfo(ByVal ctls As ControlCollection)

 Dim ctl As Control

 For Each ctl In ctls

   If ctl.HasControls Then

     Select Case ctl.GetType.ToString

       Case "System.Web.UI.WebControls.CheckBoxList"

           Dim x As System.Web.UI.WebControls.CheckBoxList

           x = ctl

           Dim li As ListItem

           Dim s As String

           For Each li In x.items

               If li.Selected = True Then

                   s = s & li.Text & "<br />"

               End If

           Next

           Session.Add(ctl.ID, s)

       Case Else

           SaveControlInfo(ctl.Controls)

     End Select

   Else

     If ctl.ID <> "" Then

       Select Case ctl.GetType.ToString

           Case "System.Web.UI.WebControls.TextBox"

               Dim x As System.Web.UI.WebControls.TextBox

               x = ctl

               Dim s As String

               s = x.Text

               s = s.Replace(Chr(10), "<BR />")

               Session.Add(ctl.ID, s)

           Case "System.Web.UI.WebControls.DropDownList"

               Dim x As System.Web.UI.WebControls.DropDownList

               x = ctl

               Session.Add(ctl.ID, x.SelectedValue)

           Case "System.Web.UI.WebControls.RadioButtonList"

               Dim x As System.Web.UI.WebControls.RadioButtonList

               x = ctl

               Session.Add(ctl.ID, x.SelectedValue)

           Case "System.Web.UI.WebControls.CheckBox"

               Dim x As System.Web.UI.WebControls.CheckBox

               x = ctl

               If x.Checked Then

                   Session.Add(ctl.ID, x.Text)

               End If

           Case Else

               Session.Add(ctl.ID, ctl.GetType.ToString())

       End Select

     End If

   End If

 Next

End Sub

Figure 1: SaveControlInfo is a recursive function designed to put data from controls on a form into session variables.

 

SaveControlInfo is fairly straightforward. It cycles through the controls and determines what needs to be done based on the type of control based on a Select Case statement. Although the Select Case statement provides cleaner code to read and debug than a large number of If Then/Else If statements, it precludes the use of GetType(control), which requires Is after it. Therefore, it is necessary to use the string description returned by control.GetType.ToString. In the simplest type of controls, a DropDownList for example, all that is required is creating a variable of the appropriate control type and then getting the selected value and storing it into the session variable with the Id of the control.

 

There are a couple of things that need to be addressed for some controls. For instance, multi-line TextBoxes are a good example. If the user has entered a carriage return/line feed in the box to separate a paragraph, this will not show up when the text is displayed in a literal tag. Instead, HTML will treat the carriage return/line feed as white space and skip over it. Therefore, I m doing a quick replace of all the line feeds in the text with the <BR /> element to provide a line break. Similarly, the CheckBoxList potentially requires displaying multiple items, which I am separating with the <BR /> tag.

 

Now we come to the second interesting problem: ASP.NET has no way to open a new window to display the form we want to print. ASP.NET is server side; it doesn t interact with the browser directly enough to open a popup window. JavaScript contains a window.open command that suits our needs perfectly. The question is, how do we get it to fire on command. The answer to that can be found in the Page object. There are two methods of the Page object that we can use: Page.RegisterStartupScript and Page.RegisterClientScriptBlock. Page.RegisterStartupScript adds a script that will execute as soon as the page is posted back. Page.RegisterClientScriptBlock allows you to register code and link it to the client-side event of another control. In this case, we ll use Page.RegisterStartupScript and just add it to the Print button s Click event following the call to SaveControlInfo. Figure 2 shows the whole code to load the session data and then pop up the window to print.

 

Private Sub Print_Click(ByVal sender As System.Object, _ &

 ByVal e As System.EventArgs) Handles Print.Click

 'Save data to session variables

 SaveControlInfo(Me.Controls)

 Dim popupScript As String = "<script language='javascript'>" & _

                 "window.open('PrintForm.aspx', '_blank', " & _

                 "'menubar=yes, resizable=yes, scrollbar=yes')" & _

                 "</script>"

 Page.RegisterStartupScript("PopupScript", popupScript)

End Sub

Figure 2: Save the values to a session variable and open up a popup window from which to print.

 

Finally, we get to PrintForm.aspx. This simple form retrieves data from the session, populates a form, displays it, then prints it by using a script block in the body element:

 

<body onload="window.print();">

 

Now that you understand the basics, this could easily be expanded upon to provide a generic class that would store all the data in a form and print it out. That would require adding a way to store the labels and the order in which they appear, but then the print page could easily pull them out of the session, build the table on the fly, and print it. I ll leave that as an exercise for you.

 

That wraps up this month s column. Send your ASP.NET questions to [email protected] so I can help you get the answers you need.

 

The sample code accompanying this article is available for download.

 

Josef Finsel is a software consultant. He has published a number of VB and SQL Server-related articles and is currently working on the syntax for FizzBin.NET, a programming language that works the way programmers have always suspected. He s also author of The Handbook for Reluctant Database Administrators (Apress, 2001).

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish