How do I use Target=_blank on a response.redirect?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

Use the button’s OnClientClick property:

<asp:Button runat="server" ID="cmd_Test" onclientclick="window.open('YourUrl')"  />

Method 4

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.

Method 5

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

Method 6

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();

Method 7

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.

Method 8

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.

Method 9

I use this code for redirects:

Response.Write(“window.open(‘http://www.whatever.com‘,’_blank’)<“+”/script>”);

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

Method 10

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.

Method 11

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

Method 12

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>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x