Friday, July 17, 2009

Print the contents of a grid on button click

Step 1:

Put the below mentined script below head tag



script language="javascript" type="text/javascript"



function printDiv() {

//open new window set the height and width =0,set windows position at bottom

var a = window.open ('','','location=1,status=1,scrollbars=1,width=600,height=600');

a.moveTo(0,0);//window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=400,height=400,toolbar=0,scrollbars=0,status=1');

//write gridview data into newly open window

a.document.write(document.getElementById('<%= Printtbl.ClientID %>').innerHTML);

a.document.close();

a.focus();

//call print

a.print();

a.close();

return false;

}

//
script


Step 2:

Put print button


<asp:Button ID="btnPrint" runat="server" OnClientClick="javascript:return printDiv();"

Text="Print" />



Step 3:

Put the Gridview1 on page

No comments: