ARTICLE

Print functionality in VB.NET

Posted by Sapna Malik Articles | Visual Basic 2010 August 05, 2010
In this article, We will see how to use print functionality in VB.NET.
 
Reader Level:

In this article, We will see how to use print functionality in VB.NET.  

This is aspx code:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel" runat="server" BackColor="#ffccff" Width="50%">
            <table cellpadding="4" cellspacing="4" width="100%" align="center">
                <tr>
                    <td align="center">                   
                        Fill All Information
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="LabelFName" runat="server" Text="F_Name"
Width="130px"></asp:Label>
                        <asp:TextBox ID="TextBoxName" runat="server" Width="315px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="LabelLName" runat="server" Text="L_Name"
Width="130px"></asp:Label>
                        <asp:TextBox ID="TextBoxCountry" runat="server" Width="315px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="LabelEmailId" runat="server" Text="EmailId"
Width="130px"></asp:Label>
                        <asp:TextBox ID="TextBoxEmailId" runat="server" Width="315px"></asp:TextBox>
                    </td>
                </tr>
                <tr align="right">
                    <td>
                        <asp:Button ID="ButtonPrint" runat="server" Text="Print" Font-Bold="true" ForeColor="Blue" OnClick="ButtonPrint_Click" /><br />
                    </td>
                </tr>
            </table>
        </asp:Panel>
    </div>
    </form>
</body>
</
html>

This code is VB:-

Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Imports System.Web.SessionState
-------------------------------------------------------------------------------------------------------
Partial Class _Default
    Inherits System.Web.UI.Page
-------------------------------------------------------------------------------------------------------    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)      
   End Sub
-------------------------------------------------------------------------------------------------------     Protected Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click
        Dim print As Control = DirectCast(Session("print"), Control)
        PrintHelper.PrintWebControl(print)
        Session("print") = Panel
        ClientScript.RegisterStartupScript(Me.[GetType](), "onclick", "<script language=javascript>window.open('Default.aspx','PrintMe','height=425px,width=425px,scrollbars=1');</script>")
    End Sub
End Class
-------------------------------------------------------------------------------------------------------

Use Of Helper:-

We will create the helper class.In this class, I made a method 'PrintWebControl'. With this method we can print any server control alike Datalist, Image, Textbox, Panel etc.

Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Imports System.Web.SessionState
Imports Microsoft.VisualBasic
Public Class PrintHelper
    Public Shared Sub PrintWebControl(ByVal print As Control)
        PrintWebControl(print, String.Empty)
    End Sub

    Public Shared Sub PrintWebControl(ByVal print As Control, ByVal Script As String)
        Dim stringWrite As New StringWriter()
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        If TypeOf print Is WebControl Then
            Dim webControl As New Unit(100, UnitType.Percentage)
            DirectCast(print, WebControl).Width = webControl
        End If
        Dim page As New Page()
        page.EnableEventValidation = False
        If Script <> String.Empty Then
            page.ClientScript.RegisterStartupScript(page.[GetType](), "PrintJavaScript", Script)
        End If
        Dim htmlForm As New HtmlForm()
        page.Controls.Add(htmlForm)
        htmlForm.Attributes.Add("runat", "server")
        htmlForm.Controls.Add(print)
        page.DesignerInitialize()
        page.RenderControl(htmlWrite)
        Dim strHTML As String = stringWrite.ToString()
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.Write(strHTML)
        HttpContext.Current.Response.Write("<script>window.print();</script>")
        HttpContext.Current.Response.[End]()
    End Sub
End Class


Login to add your contents and source code to this article
share this article :
post comment
 

thank u! It is useful for US!

Posted by Ramanathan Rams Sep 17, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor