I have the following code on my page to print the contents of the div tag:
function PrintDiv() {
document.getElementById('<%=lblDateTime.ClientID %>').style.display = "block";
document.getElementById('<%=lblDateTime.ClientID %>').innerHTML = new Date().toLocaleString();
var contents = document.getElementById("dvContents").innerHTML;
var frame1 = document.createElement('iframe');
frame1.name = "frame1";
frame1.style.position = "absolute";
frame1.style.top = "-1000000px";
document.body.appendChild(frame1);
var frameDoc = (frame1.contentWindow) ? frame1.contentWindow : (frame1.contentDocument.document) ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function () {
window.frames["frame1"].focus();
window.frames["frame1"].print();
document.body.removeChild(frame1);
}, 500);
return false;
}
I want to invoke this printing on the click of the LinkButton instead of the click of <input type="button". Can I invoke the above print function on the click of the linkbutton rather than <input type=button . Below is my link button:
<asp:LinkButton Width="130px" Height="50px" ID="print" CssClass="btns" runat="server" OnClientClick="printDiv();" >Print</asp:LinkButton>
This is the content that I am trying to print:
<div>
This is a test
</div>
This is the input button that I am using right now:
<input type="button" class="btns" runat="server" onclick="PrintDiv();" value="Print" />
As soon as I click on the <input type=button, the print function is invoked, but if I replace the <input type button with linkbutton, print function is not invoked. any help will be highly appreciated.