0

I am printing the grid as a part of my html report. I have used a printhelper function for printing the grid(kept grid in session and passing it to another page for printing). I have paging enabled on my grid. So, when I came to next page of the grid and click the print button, I only get the second page grid records to print. How can I get the whole grid for printing? Do i have to remove paging from my grid in order to get all the records?

following is my code

1) Print buttons coding.

protected void btn_betweeen_date_print_Click(object sender, EventArgs e)
{
    try
    {
        Session["ctrl"] = pnl_between_dates_grid;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick",
            "<script language=javascript>window.open('Print.aspx','PrintMe','height=500px,width=500px,scrollbars=1');</script>");
    }
    catch (Exception ex)
    {
        lblmsg.Text = ex.Message;
    }
}

2) Page load of the print form

protected void Page_Load(object sender, EventArgs e)
{
    Control ctrl = (Control)Session["ctrl"];
    PrintHelper.PrintWebControl(ctrl);
}

3) My print Helper function

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

    public static void PrintWebControl(Control ctrl, string Script)
    {
        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();
    }
4

1 回答 1

1

问题解决了。将我的打印按钮编码更改为以下内容。

protected void btn_betweeen_date_print_Click(object sender, EventArgs e)
    {
        try
        {
            grd_between_dates.AllowPaging = false;
            grd_between_dates.DataSource = (DataTable)ViewState["data_between_dept_wise_male_female"];
            grd_between_dates.DataBind();
            Session["ctrl"] = pnl_between_dates_grid;
            ClientScript.RegisterStartupScript(this.GetType(), "onclick",
                "<script language=javascript>window.open('Print.aspx','PrintMe','height=500px,width=500px,scrollbars=1');</script>");
        }
        catch (Exception ex)
        {

            lblmsg.Text = ex.Message;
        }
    }
于 2013-08-11T10:00:47.747 回答