Friday 13 September 2013

How to Print GridView data using JavaScript in asp.net?


In this article I will explain How to Print GridView data using JavaScript in asp.net.

AGENDA:   Print the data in GridView for one page.

Step-1:  First Copy and paste the below in aspx page (design page).

<script language="javascript" type="text/javascript">
     function PrintGridData() {
      var prtGrid = document.getElementById('<%=GridViewID.ClientID %>');
       prtGrid.border = 1;
            var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=10,scrollbars=1,status=0,paresizable=1');
            prtwin.document.write(prtGrid.outerHTML);
            prtwin.document.close();
            prtwin.focus();
            prtwin.print();
            prtwin.close();
        }
</script>




Step-2:  Design will be like this

<table>
<tr>
    <td>
        <asp:Button ID="btnPrintCurrentPage" runat="server" Text="Print Current Page" OnClientClick="PrintGridData();" /><vr />
     </td>
 </tr>
</table>


Note: 

(1.) Don’t write any OnClick Events

 (2.) In JavaScript Paste your gridview ID in place of    ('<%=GridViewID.ClientID %>')  

No comments:

Post a Comment