Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to make Response.Redirect open in a new window?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what are the ways to let Response.Redirect open a new window". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Response.Rederect jumps on this page by default, so in addition to using window.open in js or adding the target attribute to the A tag, it seems impossible to open a new page in the background, but in fact, you can also open the url pointed to by Response.Rederect in a new window by setting the target property of form. Here are three ways to do this.

1. Specify the target attribute to form, and all Response.Rederect on this page will open in a new window. The code is as follows:

The copy code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

Form1.Target = "_ blank"

}

Or

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

Html Code:

The copy code is as follows:

ResponseRedirectDemo

C # Code:

[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");"

}

}

}

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

3. In addition to setting the target property of form, you can only use open to open a page in a new window. You can write a general method to do this, as follows:

The copy code is as follows:

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)

}}}

This allows you to use RedirectHelper.Redirect ("oec2003.aspx", "_ blank", "") in the program; the third parameter is some properties of the open window. But this doesn't seem to be very convenient. Extension methods are provided in .net3.5, and you can also borrow it here to implement the above static method as an overload of Response.Redirect. The specific code is as follows:

The copy 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 adding this class to the project, typing Response.Redirect in the program will find that the method has three overloads, which is very convenient to combine with the target of the previous form.

In addition:

Respose.Write ("window.open ('" + url + ");"); (open a concise window):

Respose.Write ("window.open ('" + url + ", 'resizable=1,scrollbars=0,status=1,menubar=no,toolbar=no,location=no, menu=no');")

1. Response.Redirect ("XXX.aspx", true)-go directly to the new page and the original window is replaced

2. Response.Write ("window.open ('XXX.aspx','_blank')")-keep the original window and add a new page

3. Response.Write ("_ window.location='XXX.aspx'")-opens a new page and the original window is replaced

4. Server.Transfer ("XXX.aspx")-- Open a new page

5. Response.Write ("window.showModelessDialog ('XXX.aspx')")-keep the original window and open a new window as a dialog box

6. Response.Write ("window.showModelDialog ('XXX.aspx')")-opens a new window in the form of a dialog box, and the original window is replaced

This is the end of the content of "what are the ways to let Response.Redirect open in a new window". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report