protected void PrintButton_Click(object sender, EventArgs e) { PrintDetailsView.DataBind(); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); Control parent = PrintDetailsView.Parent; int GridIndex = 0; if (parent != null) { GridIndex = parent.Controls.IndexOf(PrintDetailsView); parent.Controls.Remove(PrintDetailsView); } PrintDetailsView.RenderControl(hw); if (parent != null) { parent.Controls.AddAt(GridIndex, PrintDetailsView); } string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, ""); StringBuilder sb = new StringBuilder(); sb.Append("<script type = 'text/javascript'>"); sb.Append("window.onload = new function(){"); sb.Append("var printWin = window.open('', '', 'left=0"); sb.Append(",top=0,width=1000,height=600,status=0');"); sb.Append("printWin.document.write(\""); sb.Append(gridHTML); sb.Append("\");"); //sb.Append("printWin.document.close();"); sb.Append("printWin.focus();"); sb.Append("printWin.print();};"); //sb.Append("printWin.close();};"); sb.Append("</script>"); ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString()); PrintDetailsView.DataBind(); }