我有一个打印网格的工作功能。我有很多网格。在下面的代码中,gridname 是硬编码的(后面的代码)。
按下 asp.net 按钮时,我可以传入 gridview 名称作为参数吗?
然后我可以有一个打印功能来处理我所有的网格,这将是可扩展的和很棒的。
标记:
<input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" />
代码背后:
protected void PrintCurrentPage(object sender, EventArgs e) // Print Current Page
{
try
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridViewErrorReport.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=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>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
}
JavaScript:
<script type="text/javascript">
function PrintGridData() {
var prtGrid = document.getElementById('<%=GridViewErrorReport.ClientID %>');
prtGrid.border = 0;
var prtwin = window.open('', 'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>