Print the webpage using button


Today i am talking about printing, and how to print the content or you can say selected content in If you have a webpage and you want to print this then you have to choose command key (ctrl+p) for that. Also you want to print the selected part then you have to select the text first then you have to use command key. But sometimes your selected page could not printed. So many websites provide the printing facilities on the webpage. Today i am talking about the same topics here.
Lets take a simple example to demonstrate the topic.

Source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="printpage.aspx.cs" Inherits="printpage" %>

<!DOCTYPE html>

<html xmlns="">
function printpage() {

var getpanel = document.getElementById("<%= Panel1.ClientID%>");
var MainWindow ='', '', 'height=500,width=800');
MainWindow.document.write('<html><head><title>Print Page</title>');
setTimeout(function () {
}, 500);
return false;

    <form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">

<table style="width: 100%;">
<td>English Marks</td>
<td>Social Marks </td>
<td>Science Marks</td>
    <asp:Button ID="Button1" runat="server" OnClientClick="return printpage();"  Text="Print Page" />
Code generate the following output:

Print the webpage using button

In this example i have a panel control with table. Table contains data. Also i have a button control in the page . When we click on it, a javascript function will raised. In this function, first of all get the panel with their id property. Create a window with the help of ) method also set the width and height of it. Now, write the html code in the window by using write method also write the panel contents in the body section. After that print the window.

1 comment: