我正在创建一个自定义 GridView 数据打印窗口,该窗口旨在仅打印出 gridview 区域。一个按钮将打印当前窗口的网格视图,另一个按钮将打印 GridView 中的所有记录。就目前的代码而言,当单击按钮时(分别打印当前页面和所有记录),预览窗口完美地显示了 GridView 中的所有记录(23 列,包括 GridView 两侧的两个命令字段)。但是,当打印出作业时,无论我调整什么打印机或 CSS/格式设置,网格视图都会被切掉(只显示大约一半的网格)。
我遇到的两个挑战是 1)我无法以纵向或横向打印完整的 gridview 和 2)我的 javascript 很弱。如何调整以下代码,以便在打印 gridview 时打印 FULL 网格?
如果需要更多信息,请随时询问。
这是我在打印按钮的 aspx 文件后面的代码。
protected void PrintCurrentPage_Click(object sender, EventArgs e)
{
GridView1.PagerSettings.Visible = false;
GridView1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
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=3000,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());
GridView1.PagerSettings.Visible = true;
GridView1.DataBind();
}
这是打印所有记录的代码。
protected void PrintAll_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
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=3000,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());
GridView1.AllowPaging = true;
GridView1.DataBind();
}