天天看点

.net 嵌入在框架页面内弹出新窗口方法介绍

在App_Code中添加下面一个静态类ResponseHelper

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

/// <summary>

///ResponseHelper 的摘要说明

/// </summary>

public static 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("", script);

    }

}

以下是在页面,比如一个按钮事件中,传递参数并打开一个新窗口

string url = "khbtj_print.aspx?dw=" + Server.UrlEncode(this.ddlDW.SelectedItem.Value.Trim()) + "&mc=" + Server.UrlEncode(this.ddlgcmc.SelectedItem.Value.Trim()) + "&dt1=" + Server.UrlEncode(dt1.ToString()) + "&dt2=" + Server.UrlEncode(dt2.ToString());

ResponseHelper.Redirect(url,"_blank", "");

继续阅读