0

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.

rimi
  • 624
  • 5
  • 15
  • A LinkButton will trigger a PostBack. The button does not. – VDWWD Sep 08 '21 at 19:15
  • See this post: https://stackoverflow.com/questions/16979120/calling-javascript-function-on-onclientclick-event-of-a-submit-button – Bernhard Beatus Sep 08 '21 at 19:21
  • is that why the javascript is not invoked? I invoked javascript several times before on link button. I just use OnClientClick= and name of the javascript function. – rimi Sep 08 '21 at 19:21
  • Thanks, link button is working with this post: https://stackoverflow.com/questions/16979120/calling-javascript-function-on-onclientclick-event-of-a-submit-button – rimi Sep 08 '21 at 19:29
  • `OnClientClick="printDiv(); return false;"` will do the trick – VDWWD Sep 08 '21 at 19:56
  • Yes, this worked! – rimi Sep 08 '21 at 20:01

0 Answers0