9

I do not necessarily have to use response.redirect, but that is what I had. I would like to open the selected link in a new window. How do I do that?

context.Response.Redirect(ConfigurationManager.AppSettings["URL"] + ext );
MrM
  • 21,709
  • 30
  • 113
  • 139

12 Answers12

16

You can't is the short answer. The browser is the only thing that can open up a new window.

What you can do is send a chunk of html down the response that has a link with your url as an href, target="_blank" and a chunk of javascript onload of the form that fakes a click. If this doesn't work then use a window.open(url);

response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>"); 
Dave Walker
  • 3,498
  • 1
  • 24
  • 25
5

You're trying to accomplish a client-side task from the server side, so you'll need to do a bit of hacking.

One option is sending back a page that's just a bit of JavaScript, which then will handle the redirect.

This isn't particularly clean, but what about:

Response.Write("<script>window.open('http://www.somesite.com/','_blank');</script>");
Josh Earl
  • 18,151
  • 15
  • 62
  • 91
2

If you are just handling navigation you can try a ASP:Hyperlink control rather than a button, that way the target is specified for the browser when the page is rendered:

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

Of course it is more polite to leave .Target alone because in this case the hyperlink could be right clicked and "open in new page/tab" would be available from the context menu.

Brian P
  • 43
  • 3
2

Use the button's OnClientClick property:

<asp:Button runat="server" ID="cmd_Test" onclientclick="window.open('YourUrl')"  />
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Rudy
  • 21
  • 2
2

You cannot do that with Response.Redirect()

Well you could do this using a simple Javascript inside Response.Write

Response.Write("<script>window.open('page.html','_blank')</script>");
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
1
SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=sandesh;database=BeautyJunction;");
        string strSQL = "Select BenificiaryType,BenificiaryName,DisttCode,IFSC,AC_No from BenificiaryMaster";
        SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
        DataSet ds = new DataSet();
        dt.Fill(ds, "UserDetail");
        string dat = String.Format("{0: MM_dd_yyyy}", DateTime.Now);
        //string dat = Convert.ToString(DateTime.UtcNow.ToShortDateString());
        sb.Append("~/Folder1/BenificiaryMaster_file_1" + dat + ".xml");
        string path = sb.ToString();
        ds.WriteXml(Server.MapPath(path));
        LabelMessage.Text = "Your XML file has Been created with name 'BenificiaryMaster_file_1" + dat +"'  <a target='_blank' href=Folder1/BenificiaryMaster_file_1.xml>click here</a> to show Benificiary record file";
        //GridView1.DataBind();
alstonp
  • 700
  • 6
  • 25
Vishal
  • 11
  • 1
0

I use this code for redirects:

Response.Write("window.open('http://www.whatever.com','_blank')<"+"/script>");

The final tag needs to be formatted <"+"/script>

Tug Strongly
  • 187
  • 2
  • 5
  • 18
0

I added to @DaveWalker response:

Response.Write("<script>")
Response.Write("window.open('NewPage.aspx','_blank','resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=no')")
Response.Write("</script>")

This will create a popup instead of opening a new tab.

JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
0

I've seen many anwers and no one really works for me. So I tried the following code and it solved my problem:

First I changed mt button by a linkbutton with the same aspect as a button.

<asp:LinkButton ID="btPrint" runat="server" class="btn btn-primary"><span class="glyphicon glyphicon-print"></span> Print</asp:LinkButton>

After this I add the following in my code behind.

btPrint.Attributes.Add("href", String.Format("printPage.aspx?&id={0}", txtId.Text));
btPrint.Attributes.Add("target", "_blank");
mathias.horn
  • 241
  • 1
  • 8
  • 12
0

This works for me

On aspx code:

 <a id="myLink" target="_blank" onclick="window.open('ExamplePage.aspx, '_blank');">Link To A Page And Open Other Tab</a>
CREM
  • 1,929
  • 1
  • 25
  • 35
0

If I understand this correctly, you want to be able to open the redirected URL in a new window, but presumably retain the original target in the same window.

Unfortunately you cannot do this, because the redirect is served by the server and not browser. You could potentially redirect to a page that contained some script that opened a new window based on a URL querystring parameter. But this would open yourself up to XSS if your not careful.

Digbyswift
  • 10,310
  • 4
  • 38
  • 66
0

How about a hyperlink that you program dymanically? Imagine this. asp hyperlink that when you click opens a new window, possibly with no scroll bars, no address bar, anything you want. Here is an example:

hyperlink1.Attributes.Add("onclick", "window.open(http://www.mylink.com?sessionvar1=" + someValue + "',null,'height=251px, width=600px,status=no, resizable=no, scrollbars=no, toolbar=no,location=no,menubar=no ');");

This is just an alternative to a standard button that would otherwise call a click handler. Keep in mind, you can add the whole thing from the front as an attribute.

Lukas
  • 2,885
  • 2
  • 29
  • 31