Friday, October 3, 2014

Print a DIV or Panel in ASP.NET C# JavaScript

Hello, today I will show you how to print a div or panel in your ASP.NET page using JavaScript and Jquery with the help of a button. Normally to print we use windows.print() option to print the whole page. but if we need to print a particular portion of a page then what to do? Solution is here.

Create a new page and add a div or ASP panel and after finishing the div add an Button. We don't need to generate the click event of the button, because we are just use it as a normal button and call JavaScript function using this. in the div write your all data, which you want to print.

Check the bellow code.

Your all data goes here....

aNow its time for dome JavaScript code to handle...

function printDiv() {
 //Get the HTML of div
 var divElements = document.getElementById("<%= PanelDiv.ClientID %>").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 =
   "" +
   divElements + "";

 //Print Page

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

Now run your project. We have use PanelDiv.ClientID because its an ASP control and its the process to access the server side controls into client side code. Hope it helps you... Happy coding ... :)


Post a Comment

Popular Posts