here is the steps:
1) create simple web application (here i used asp.net with C#).
2) put following control in to Page.
1) DataList (id = DataList1 )
2) html button (id =Button1)
<inputid=”Button1″type=”button”value=”Print”language=”javascript”onclick=”return Button1_onclick()” />
<asp:DataList=”DataList1″runat=”server”>
</asp:DataList>
3) fill data into DataList1
//here i used the list you can bind with database also
System.Collections.Generic.List<string> obj = newSystem.Collections.Generic.List<string>();
for(inti = 0; i < 10; i++)
{
obj.Add(i.ToString());
}
DataList1.DataSource = obj;
DataList1.DataBind();
4) now write the print function. click on the Button1 or write Java Script.
<script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0');
//write gridview data into newly open window
a.document.write(document.getElementById('<%= DataList1.ClientID %>').innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
return false;
}
// -->
</script>
If this post has helped you then please mark as Answer. It will helpful for other users to find Thread easily. Thanks & Regards,
Anand Mani Tiwari
Software Engineer.
Marked as answer by Akshay786 on Sep 21, 2011 11:16 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintDiv.aspx.cs" Inherits="PrintDiv" %>
<!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>
<script language="javascript">
function CallPrint(strid) {
var prtContent = document.getElementById(strid);
var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
prtContent.innerHTML = strOldOne;
}
</script>
<div id="print_Grid">
This is Print This is Print This is Print This is Print This is Print
<div>
<asp:Button ID="btnPrint" runat="server" Text="Print" />
</form>
</body>
</html>
using System;
public partial class PrintDiv : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnPrint.Attributes.Add("onclick", "javascript:CallPrint('print_Grid')");
}
}
For More Information Visit : http://rajeshprajapati.blogspot.in/2009/05/how-to-print-div-tag-using-javascript.html
Rajesh Prajapati
My Blog My Website Please contact prajapati.rajesh@gmail.com
Akshay786
Member
7 Points
7 Posts
How to do custom printing in asp.net?
Sep 21, 2011 11:11 AM|LINK
I have a gridview like this and on top of it I have a print button .
The gridview is like this
id name country
1 test testcountry
2 test1 testtere
on click of print button
I have to print total pages containing a predefined format with all the details from gridview
page 1
--------
id---1
name--test
country-- test
more columns-trtrtytr
page 2
--------
id---2
name--test1
country-- testtere
more columns-trtrtytr
in each page we have more columns present in grid.
Please suggest methods to resolve this issue.I know abt the javascript to print grid.
Anand Mani T...
Member
720 Points
138 Posts
Re: How to do custom printing in asp.net?
Sep 21, 2011 11:15 AM|LINK
For Printing use below code:
here is the steps: 1) create simple web application (here i used asp.net with C#). 2) put following control in to Page. 1) DataList (id = DataList1 ) 2) html button (id =Button1) <inputid=”Button1″type=”button”value=”Print”language=”javascript”onclick=”return Button1_onclick()” /> <asp:DataList=”DataList1″runat=”server”> </asp:DataList> 3) fill data into DataList1 //here i used the list you can bind with database also System.Collections.Generic.List<string> obj = newSystem.Collections.Generic.List<string>(); for(inti = 0; i < 10; i++) { obj.Add(i.ToString()); } DataList1.DataSource = obj; DataList1.DataBind(); 4) now write the print function. click on the Button1 or write Java Script. <script language="javascript" type="text/javascript"> <!-- function Button1_onclick() { //open new window set the height and width =0,set windows position at bottom var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0'); //write gridview data into newly open window a.document.write(document.getElementById('<%= DataList1.ClientID %>').innerHTML); a.document.close(); a.focus(); //call print a.print(); a.close(); return false; } // --> </script>Thanks & Regards,
Anand Mani Tiwari
Software Engineer.
rbprajapati
Member
30 Points
21 Posts
Re: How to do custom printing in asp.net?
Feb 25, 2012 03:57 AM|LINK
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintDiv.aspx.cs" Inherits="PrintDiv" %> <!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> <script language="javascript"> function CallPrint(strid) { var prtContent = document.getElementById(strid); var WinPrint = window.open('', '', 'left=0,top=0,width=900,height=600,toolbar=1,scrollbars=1,status=0'); WinPrint.document.write(prtContent.innerHTML); WinPrint.document.close(); WinPrint.focus(); WinPrint.print(); WinPrint.close(); prtContent.innerHTML = strOldOne; } </script> <div id="print_Grid"> This is Print This is Print This is Print This is Print This is Print <div> <asp:Button ID="btnPrint" runat="server" Text="Print" /> </form> </body> </html> using System; public partial class PrintDiv : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { btnPrint.Attributes.Add("onclick", "javascript:CallPrint('print_Grid')"); } } For More Information Visit : http://rajeshprajapati.blogspot.in/2009/05/how-to-print-div-tag-using-javascript.htmlMy Blog
My Website
Please contact prajapati.rajesh@gmail.com