zoukankan      html  css  js  c++  java
  • [转]打印gridview

    本文转自:http://blog.csdn.net/fanfengchimo/archive/2007/07/23/1703307.aspx
    原文如下:
    今天公司让做一个打印功能,就是把gridview中的内容打印出来,再网上查了好多方法,最后决定把gridview包含到一个div中,然后将div的html提交到另一个页面,最终打印这个页面.

    打印指定内容:
    <html>
    <head>
    <script type="text/javascript" language="javascript">
    function printPage() {
    var newWin = window.open('printer','','');
    var titleHTML = document.getElementById("printdiv").innerHTML;
    newWin.document.write(titleHTML);
    newWin.document.location.reload();
    newWin.print();

    newWin.close();
    }
    </script>
    </head>
    <body>
    <div id="printdiv">
     <table class="sontable" cellspacing="0" cellpadding="0" style=" 13%">
                                <tr>
                                    <td style=" 700px; height: 161px">
                                       <asp:GridView ID="GridData"  runat="server" CellPadding="3" CellSpacing="0"  BorderWidth="1px" BackColor="LightSteelBlue" BorderColor="White" BorderStyle="None" Font-Size="12px" Width="543px" Height="20px"  OnRowDataBound="GridData_RowDataBound">
                                        <RowStyle BackColor="GhostWhite" BorderColor="#006699" />
                                            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" Wrap="True" />
                                            <HeaderStyle Height="25px" BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" CssClass="Freezing"/>
                                    </asp:GridView>                                    
                                    </td>
                                </tr>
                            </table>
    </div>
    <a href="javascript:;" onclick="printPage()">打印</a>
    </body>
    </html>

    代码就是这样的.

    第二种方法是,先将页面上的除了你要打印的标签外隐藏,接着执行打印,在打印后将页面上的标签全部显示

    代码如下:
    function printer()
    {
      
     beforeprint();
     window.focus();
     window.print()
     afterprint();
    }

    function beforeprint()
    {

     for(i = 0; i < document.all.length; i++)
     {
     
        if ((document.all(i).id.indexOf("div_table_")!=-1) && document.all(i).tagName=="TABLE")

    //其中"div_table_"检测你要打印的标签ID
        {
         document.all(i).style.display="none";
        }
     }
    }

    function afterprint()
    {

     for(i = 0; i < document.all.length; i++)
     {
        if ((document.all(i).id.indexOf("div_table_")!=-1) && document.all(i).tagName=="TABLE")
        {
          document.all(i).style.display="block";
        }
     }
    }

    调用printer()就可以了

  • 相关阅读:
    JS对文本框值的判断
    PostgreSQL导出表中数据
    postgreSQL中跨库查询在windows下的实现方法
    获取表中每个编号最新一条数据的集合
    开源跨平台声波传输库:Sonic
    Windows下配置cygwin和ndk编译环境
    char的定义在iOS和Android下是不同的
    stdout引发的curl 302跳转 crash
    WebBrowser内嵌页面的跨域调用问题
    【已解决】Ubuntu 12.04 LTS Source安装nodejs时出现"bash ./configure permission denied"
  • 原文地址:https://www.cnblogs.com/freeliver54/p/1130734.html
Copyright © 2011-2022 走看看