For a project a requirement was to print a certain piece of the html code.
A custom webpart was created, that webpart contained a table which, if needed ,had to be printed.
With Javascript you are able to use the following command:
"window.print()"
to print your whole page, but in our case this was not the requirement.
The following code will look for a control called 'test', and will also print some more fields.
The extra field will be set by Javascript code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testproject._Default" %>
<script type="text/javascript" language="javascript">
var NewWindow;
var Content;
var Time = new Date();
var stringContent;
function PrintPage() {
Content = document.getElementById('test');
if (!NewWindow || NewWindow.closed) {
NewWindow = window.open("", "", "");
if (!NewWindow.opener) {
NewWindow.opener = window;
}
setTimeout("writeToWindow()", 50);
}
else {
NewWindow.focus();
}
}
function writeToWindow() {
stringContent = "<html><head></head><body onload='window.print()'>";
stringContent += "<table>" + Content.innerHTML + "</table>" + "Printed for signature date:" + Time.getDay() + "/" + Time.getMonth() + "/" + Time.getFullYear();
stringContent += "<br />";
stringContent += "<br />";
stringContent += "Signature :";
stringContent += "<br />";
stringContent += "<br />";
stringContent += '<input type="text"/>';
stringContent += "</body></html>";
NewWindow.document.write(stringContent);
NewWindow.document.close();
}
</script>
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<input type="button" onclick="PrintPage()" title="Pint" style="width: 84px" />
<div>
<table id="test" runat="server">
<tr>
<td>
R1C1
</td>
<td>
R1C2
</td>
<td>
R1C3
</td>
</tr>
</table>
<br />
Printed for signature date:
<br />
<br />
Signature :
<br />
<br />
<input type="text" style="width: 409px;" />
<br />
<br />
<input type="button" text="Click"/>
</div>
</form>
</body>
</html>