Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an asp page contains a div and asp button
the button should print the div content and call code behind


the problem in my code is the way I use to print the div content. it changes the page code then print then return the page code back.
so it prints the div content and do NOT call BTNPrint_Click event.

how could I do that?

What I have tried:

this is a javascript code to print div content
<script language="javascript" type="text/javascript">
         function printDiv(divID) {
             //Get the HTML of div
             var divElements = document.getElementById(divID).innerHTML;
             //Get the HTML of whole page
             var oldPage = document.body.innerHTML;

             //Reset the page's HTML with div's HTML only
             document.body.innerHTML =
              "<html><head><title></title></head><body>" +
              divElements + "</body>";

             //Print Page
             window.print();

             //Restore orignal HTML
             document.body.innerHTML = oldPage;


         }
    </script>



this is the asp button
C#
<asp:Button ID="BTNPrint" runat="server"
           Text="Print" OnClientClick="javascript:printDiv('div1')" onclick="BTNPrint_Click" />


and here is the div
<div id="div1">
//some labels and gridview
</div>
Posted
Updated 30-Jul-17 18:52pm

1 solution

 
Share this answer
 
Comments
M. Daban 31-Jul-17 6:42am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900