在框架頁中彈出新視窗提供列印功能

暖楓無敵發表於2011-09-13

1、首先利用一個類,ResponseHelper.cs,如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    public class ResponseHelper
    {
        public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;
            if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
            {
                context.Response.Redirect(url);
            }
            else
            {
                Page page = (Page)context.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException("Cannot redirect to new window outside Page context.");
                } url = page.ResolveClientUrl(url); string script; if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"<script>window.open(""{0}"", ""{1}"", ""{2}"");</script>";
                }
                else
                {
                    script = @"<script>window.open(""{0}"", ""{1}"");</script>";
                }
                script = String.Format(script, url, target, windowFeatures);
                page.RegisterStartupScript("ddd", script);
            }
        }
    }

2、在框架頁中,進行頁面跳轉,程式碼:

ResponseHelper.Redirect("~/Vehicle/PrintTaskSheet.aspx?VID=" + braID, "_blank", "");

3、列印區程式碼:

<style media="print">
        .Noprint
        {
            display: none;
        }
        /*用本樣式在列印時隱藏非列印專案 */
        .PageNext
        {
            page-break-after: always;
        }
        /*--控制分頁*/
    </style>

 <!--引入一個object-->
    <object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0"
        width="0">
    </object>
               <div align="center">
                    <span class="Noprint">
                        <input name="button2" type="button" class="formbotton" onclick="document.all.WebBrowser.ExecWB(6,6)"
                            value="直接列印" />
                        <input name="button2" type="button" class="formbotton" onclick="document.all.WebBrowser.ExecWB(8,1)"
                            value="頁面設定" />
                        <input name="button2" type="button" class="formbotton" onclick="document.all.WebBrowser.ExecWB(7,1)"
                            value="列印預覽" />
                    </span>
                </div>



相關文章