简体   繁体   中英

pass gridview name for printing

I have a working function that prints a grid. I have many grids. In the code below the gridname is hardcoded(code behind).

Can I pass in a gridview name as a paramater when the asp.net button is pressed?

Then I can have one print function that handles all my grids which would be extensible and awesome.

Markup:

<input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" />

Code Behind:

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>

Yes it is possible, you can pass the id to your function, and use it to retrieve your grid control from the page..

Suppose "myGridId" is passed to function...

Your code should now look like this

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(\"");
GridView gv = (GridView)this.FindControl("myGridId");
sb.Append(gv);
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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM