Response. Redirect method to open a new window

Source: Internet
Author: User
In general, response. the redirect method is switched on the server side, so unless response. write ("<SCRIPT> window. location = 'HTTP: // DOTNET. aspx. CC '; </SCRIPT> ") the specified URL address cannot be opened in a new window. However, if you carefully analyze it and set the target attribute of the form element, you can still open a new window. The following two methods can be used.

Method 1: set the target attribute on the server. This method is also very suitable when the client does not support scripts.CodeAs follows:

Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en "
  " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< Script runat = " Server " >

Protected   Void Page_load ( Object Sender, eventargs E)
{
Form1.target= "_ Blank";
}

Protected   Void Button#click ( Object Sender, eventargs E)
{
Response. Redirect ("Http://dotnet.aspx.cc");
}
Script >

< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head ID = " Head1 " Runat = " Server " >
< Title > Title >
Head >
< Body ID = " B " Runat = " Server " >
< Form ID = " Form1 " Runat = " Server " >
< ASP: button ID = " Button1 " Runat = " Server " Onclick = " Button#click " Text = " Open a new window or new tab "   />
Form >
Body >
Html >

Method 2: Use a client script to set the target attribute. The Code is as follows: Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en "
  " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< Script runat = " Server " >

Protected   Void Page_load ( Object Sender, eventargs E)
{
Button1.attributes. Add ("Onclick","This.form.tar get = '_ newname'");
}

Protected   Void Button#click ( Object Sender, eventargs E)
{
Response. Redirect ("Http://dotnet.aspx.cc");
}
Script >

< HTML xmlns = " Http://www.w3.org/1999/xhtml " >
< Head ID = " Head1 " Runat = " Server " >
< Title > Title >
Head >
< Body ID = " B " Runat = " Server " >
< Form ID = " Form1 " Runat = " Server " >
< ASP: button ID = " Button1 " Runat = " Server " Onclick = " Button#click " Text = " Open a new window or new tab "   />
Form >
Body >
Html >

The target attribute in the above two methods can use any valid name, but note that if the window with the same name already opens, the new window will open in the window with the same name already exists.

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.