Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a pannel inside it i had an gridview

i used static class Print helper

i used to send panel as arguments printpreview is showing but css style i applied not getting

-------------------PrintHelperClass-------------------------------
C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Web.SessionState;
//using System.Web.HttpContext;
/// <summary>
/// Summary description for PrintHelper
/// </summary>
public class PrintHelper
{
	public PrintHelper()
	{
		
	}

    public static void PrintWebControl(Control ctrl)
    {
        PrintWebControl(ctrl, string.Empty);
    }

    public static void PrintWebControl(Control ctrl, string Script)
    {
        //if (System.Web.HttpContext.Current.(string)Session["isgrid"] == "grid")
        //{
        //    GridView grid = (GridView)ctrl;
        //    grid.AllowPaging = false;
        //    grid.DataBind();
        //}


        StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        if (ctrl is WebControl)
        {
            Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
        }
        Page pg = new Page();
        pg.EnableEventValidation = false;
        if (Script != string.Empty)
        {
            pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
        }
        HtmlForm frm = new HtmlForm();
        pg.Controls.Add(frm);
        frm.Attributes.Add("runat", "server");
        frm.Controls.Add(ctrl);
        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();

    }
}.
-----------------clickevent for print-----------------
protected void btnPrint_Click(object sender, ImageClickEventArgs e)
{
 
    Session["ctrl"] = dgvRegisterReport;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window.open( 'http://localhost:57601/PolosysWebERP5.2_1/GeneralPages/Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');", true);
}
-------------------------------
for grid i given style but while printing the style is not getting 
i used @media print also then also not getting.
Posted
Updated 10-Dec-13 1:23am
v2
Comments
Nelek 10-Dec-13 3:43am    
Don't think we can read minds or do astral projections to see your monitor. If you need help, the least you could do is to add some relevant code to your question or to explain your problem in such a way, that the users of CP can understand it. Otherwise, nobody will be able to help you.

Please use the "improve question" and add relevant information or a piece of code envolved. There are thousands of ways to screw things up, how are we supposed to know which one did you choose?
Please read What have you tried?[^] and edit your post.
Mohammed Shamsheer 10-Dec-13 23:08pm    
thanks for your reply.....i improved my question...

1 solution

i solved my problem

C#
protected void btnPrint_Click(object sender, ImageClickEventArgs e)
{
   
    Session["GridView"] = dgvRegisterReport;
    Session["ctrl"] = pnlContents;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "onclick", "javascript:window.open( 'http://localhost:57601/PolosysWebERP5.2_1/GeneralPages/Print.aspx','PrintMe','height=600px,width=600px,scrollbars=1');", true);
}

on print.aspx page

 protected void Page_Load(object sender, EventArgs e)
    {

        Control ctrl = (Control)Session["ctrl"];
        GridView GridView1 = (GridView)Session["GridView"];
        
        PrintHelper.PrintWebControl(ctrl,string.Empty,GridView1);

        

       
    }

class file : 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Web.SessionState;
//using System.Web.HttpContext;
/// <summary>
/// Summary description for PrintHelper
/// </summary>
public class PrintHelper
{
	public PrintHelper()
	{
		
	}

    //public static void PrintWebControl(Control ctrl)
    //{
    //    PrintWebControl(ctrl, string.Empty,GridView salesgridview);
    //}

    public static void PrintWebControl(Control ctrl, string Script, GridView SalesGridView)
    {
        //if (System.Web.HttpContext.Current.(string)Session["isgrid"] == "grid")
        //{
        //    GridView grid = (GridView)ctrl;
        //    grid.AllowPaging = false;
        //    grid.DataBind();
        //}
        
        SalesGridView.UseAccessibleHeader = true;
        SalesGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
        SalesGridView.FooterRow.TableSection = TableRowSection.TableFooter;
        SalesGridView.Attributes["style"] = "border-collapse:separate";
        foreach (GridViewRow row in SalesGridView.Rows)
        {
            if (row.RowIndex % 30 == 0 && row.RowIndex != 0)
            {
                row.Attributes["style"] = "page-break-after:always;";
            }
        }

        StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        if (ctrl is WebControl)
        {
            Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
        }
        Page pg = new Page();
        pg.EnableEventValidation = false;
        if (Script != string.Empty)
        {
            pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
        }
        HtmlForm frm = new HtmlForm();
        pg.Controls.Add(frm);
        frm.Attributes.Add("runat", "server");
        frm.Controls.Add(ctrl);
        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>");
        string style = "<style type="text/css">td { padding: 4px; border: solid 1px #c1c1c1; color: #474545; font-family:Arial; font-size:12px } th { padding: 2px 2px; color: #3e4d9e; background: #e0d7f4 ; font-family:Arial; text-align:left;text-indent:10px; font-size:14px ;border-top: solid 1px #a99cec; border-left:none;border-right:none; border-bottom:none }{ width: 100%;background-color: #fff; margin: 5px 0 10px 0; border-top: solid 1px #a99cec; border-left:solid 1px #a99cec;border-right:solid 1px #a99cec;font-family:Arial; font-size:15px; </style>";
 
        HttpContext.Current.Response.Write(style);
        HttpContext.Current.Response.End();
    }
}
 
Share this answer
 
v2

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