How to Print Web Control using ASP.Net with C#


in this article , I explained about how to Print Web Control using ASP.Net with C#. Hereunder code snippet helps us for clearing concepts of Web Control using ASP.Net with C#

How to Print Web Control using ASP.Net with C#

This is very simple asp.net web application project.If you want to print asp.net web controls ,take the below example then modify as you want.

Just Drag and drop Two textbox and one button control to your asp.net web page.

Asp.net Code behind File


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Session["ctrl"] = Panel1;
Control ctrl = (Control)Session["ctrl"];
PrintWebControl(ctrl);
}
public static void PrintWebControl(Control ControlToPrint)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ControlToPrint is WebControl)
{
Unit w = new Unit(100, UnitType.Percentage);
((WebControl)ControlToPrint).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false;
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ControlToPrint);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
}


In Aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">

UserName :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Pass Word :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Print" />
</asp:Panel>
</div>
</form>
</body>
</html>



Comments

Author: surendar14 Jun 2010 Member Level: Silver   Points : 0

Hi
good stuff...........

Author: vinay30 Aug 2010 Member Level: Bronze   Points : 1

Hi UltimateRengan,

First of all thanks for this code and small issue how can I remove page numbers on the print page.

Vinay

Author: Pal (Parthiv) Patel31 Aug 2010 Member Level: Gold   Points : 1

----------1111111111-------------------
hey
this one is awesome code snippet
keep it up
put good codes and earn money and points
------------!!!!!!!!!!-------------------

Guest Author: Pinky21 Jun 2012

I tried to your code for print panel on host server. I got this error.

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

How can I solve this error?

Guest Author: sariii18 Apr 2013

when i run this code, its run without error but don't print

Guest Author: raghu13 May 2013

I have a panel with multiple web controls...when i used your code its throwing error as "The Controls collection cannot be modified because the control contains code block."
in frm.controls.add(controlToPrint)...so how can i solve dis?

Guest Author: kondareddy27 May 2013

Please give me reply i am using this code but its not working in my system error is the control with ID 'RadGrid1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.

Author: Ultimaterengan15 Oct 2013 Member Level: Gold   Points : 2

"The Controls collection cannot be modified because the control contains code block." To avoid this error you should copy and paste all java script codes after HTML tag.

Guest Author: .net Developer29 Jan 2014

Thanks for that good snippet but i am facing a problem. when i click on the print button there is an image in my page the preview page magnifies it. how can i make it to the exact size.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: