Response. Redirect open in New Window & amp; 3.0 Extension Method

Source: Internet
Author: User

Response. by default, rederect redirects to this page. Therefore, besides using Windows in JS. in addition to the open or add target attribute to tag a, it seems that you cannot open a new page in the background. Otherwise, you can set the target attribute of form to make response. the URL to which rederect points is opened in a new window. The following three methods are used.

1. Specify the target attribute for form. All response. rederect on this page will be opened in a new window. The Code is as follows:

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

Or

<form id="form2" runat="server" target="_blank">

2. Use a script to specify the target of a form for a control. The Code is as follows:

HTML code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ResponseRedirectDemo._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

C # code:

namespace ResponseRedirectDemo{    public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            Button1.Attributes.Add("onclick", "this.form.target='_blank'");            Button2.Attributes.Add("onclick", "this.form.target=''");        }        protected void Button1_Click(object sender, EventArgs e)        {            Response.Redirect("http://oec2003.cnblogs.com");        }        protected void Button2_Click(object sender, EventArgs e)        {            Response.Redirect("http://oec2003.cnblogs.com");        }    }}

In the code above, click button1 to open in the new window, and click button2 to open on this page.

3. In addition to setting the target attribute of form, to open the page in a new window, you can only use open. You can write a common method to implement it, as shown below:

public class RedirectHelper{    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.");            }            url = page.ResolveClientUrl(url);            string script;            if (!String.IsNullOrEmpty(windowFeatures))            {                script = @"window.open(""{0}"", ""{1}"", ""{2}"");";            }            else            {                script = @"window.open(""{0}"", ""{1}"");";            }            script = String.Format(script, url, target, windowFeatures);            page.ClientScript.RegisterStartupScript(page.GetType(),                "Redirect", script, true);        }    }}

In this way, you can use redirecthelper. Redirect ("oec2003.aspx", "_ blank", "") in the program. The third parameter is some attributes of the open window. However, it seems that this is not very convenient. In. net3.5, the feature of the extension method is provided. Here you can also use it to implement the above static method as an overload of response. Redirect. The Code is as follows:

public static class RedirectHelper{    public static void Redirect(this HttpResponse response,         string url, string target, string windowFeatures)    {        if ((String.IsNullOrEmpty(target) ||             target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&             String.IsNullOrEmpty(windowFeatures))        {            response.Redirect(url);        }        else        {            Page page = (Page)HttpContext.Current.Handler; if (page == null)            {                throw new                 InvalidOperationException("Cannot redirect to new window .");            }            url = page.ResolveClientUrl(url);            string script;            if (!String.IsNullOrEmpty(windowFeatures))            {                script = @"window.open(""{0}"", ""{1}"", ""{2}"");";            }            else            {                script = @"window.open(""{0}"", ""{1}"");";            }            script = String.Format(script, url, target, windowFeatures);            ScriptManager.RegisterStartupScript(page,                 typeof(Page), "Redirect", script, true);        }    }}

After the class is added to the project, input response. Redirect in the program and you will find that there are three overload methods in this method. In this way, it is very convenient to combine them with the target of the previous form.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.