【转】使用Response.Redirect打开新窗口的方法

方法一:

protected void Page_Load(object sender, EventArgs e)
    {
        form1.Target = "_blank";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://dotnet.aspx.cc");
    }


办法二:采用客户端脚本的方法设置 target 属性。代码如下:

复制  保存


protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "this.form.target='_newName'");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://dotnet.aspx.cc");
    }


方法三
protected void Page_Load(object sender, EventArgs e)
    {
      string WindowName = "win" + System.DateTime.Now.Ticks.ToString();
      Page.RegisterOnSubmitStatement("js", "window.open('','" + WindowName + "','width=600,height=200')");
        form1.Target = WindowName;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://dotnet.aspx.cc");
    }

方法4:

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);
           
            //ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
            page.RegisterStartupScript("ddd", script);
        }
    }
}

调用:

ResponseHelper.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");

posted on 2009-12-01 09:49  陈老师博客  阅读(338)  评论(0编辑  收藏  举报

导航