Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that when the user clicks the print button the print code that I have in place prints to the print screen for the user to click the printer that they have then click print. Is there a way that the user can click the print button and it prints out a report of the form that was filled out by the user? I am using VS 2010 with C#.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       ButtonPrint.Attributes.Add("onclick", "window.print(); return false");
   }
Posted
Updated 15-Oct-13 1:44am
v2
Comments
thatraja 15-Oct-13 7:51am    
which reporting tool? crystal reports?
Computer Wiz99 15-Oct-13 8:25am    
No, just a form report.
Jignesh Khant 15-Oct-13 8:36am    
Dou you want the code to print the form?
Computer Wiz99 15-Oct-13 8:38am    
Yes I would, please.

 
Share this answer
 
v2
Comments
Joezer BH 16-Oct-13 2:56am    
5ed!
protected void btnprint_Click(object sender, EventArgs e)
    {
        Session["ctrl"] = Panel2;
        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();
    }
 
Share this answer
 

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