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