In this article we will see how to print a portion of a page.
We are often required to print a particular portion of a page at times that we can not create several pages then redirect them and then print.
See the page below to see how it takes effect.
- <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
- CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
-
- <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
- <script language="javascript">
- function printPartOfPage(elementId) {
- var printContent = document.getElementById(elementId);
- var windowUrl = 'about:blank';
- var uniqueName = new Date();
- var windowName = 'Print' + uniqueName.getTime();
- var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');
-
- printWindow.document.write(printContent.innerHTML);
- printWindow.document.close();
- printWindow.focus();
- printWindow.print();
- printWindow.close();
- }
- </script>
- </asp:Content>
- <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
- <div id="MyFirstDiv" style="padding: 10 10 10 10; background-color: LightCyan; width: 300px;
- float: left;" onclick="javascript:printPartOfPage('MyFirstDiv')">
- C# Corner's mission is to become the world's top online developer community.
- Each day, we empower millions of developers worldwide by providing the latest unbiased news, advice,
- and tools for career growth. We’re passionate about nurturing the next young generation of developers
- so that they can become not only great programmers, but also exceptional human beings.</div>
- <div id="Div1" onclick="javascript:printPartOfPage('Div1')">
- <div style="padding: 10 10 10 10; background-color: LightCyan; width: 300px; float: left;
- margin-left: 50px;">
- C# Corner, headquartered in Philadelphia, PA, is an online global community of 3 million software developers.
- We serves 5+ million visitors with 9 million page views each month. We publish the latest news and articles
- on cutting-edge software development topics. Developers share their knowledge and connect via content, forums,
- and chapters. Thousands of members benefit from our monthly events, webinars, and conferences.
- We also provide tools for career growth such as career advice, resume writing, training, certifications,
- books and whitepapers, and videos. We also connect developers with their potential employers via out Job board.</div>
- </div>
- </asp:Content>
The output for above code is
On click, we are calling the JavaScript function to print the div content.