How to use Target = _blank for response.redirect?

I don't have to use response.redirect, but this is what I had. I would like to open the selected link in a new window. How to do it?

context.Response.Redirect(ConfigurationManager.AppSettings["URL"] + ext ); 
+6
source share
12 answers

You cannot get a short answer. A browser is the only thing that can open a new window.

What you can do is send a piece of html response that has a link with your url like href, target = "_ blank" and a piece of javascript loading a form that fakes a click. If this does not work, use window.open (url);

 response.write("<script>"); response.write("window.open('page.html','_blank')"); response.write("</script>"); 
+12
source

You are trying to complete a client task from the server side, so you need to hack a bit.

One option is to send back a page containing only part of JavaScript, which will then handle the redirect.

This is not particularly clean, but what about:

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

Use the OnClientClick property:

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

You cannot do this with Response.Redirect()

Well, you can do this using simple Javascript inside Response.Write

 Response.Write("<script>window.open('page.html','_blank')</script>"); 
+1
source

If you just handle the navigation, you can try the ASP: Hyperlink control, not the button, so the target is specified for the browser when rendering the page:

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

Of course, it’s more polite to leave. By itself, because in this case the hyperlink can be right-clicked and "open on a new page / tab" will be available from the context menu.

+1
source

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

Unfortunately, you cannot do this because the redirect is served by the server, not the browser. You could redirect to a page containing some script that opened a new window based on the querystring URL parameter. But it will open itself to XSS if you are not careful.

0
source

What about a hyperlink that you program differently? Imagine this. asp hyperlink, that when clicked, a new window opens, possibly without scroll bars, there is no address bar, nothing 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 the standard button, which would otherwise call the click handler. Keep in mind you can add all this from the front as an attribute.

0
source

I use this code to redirect:

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

Final tag must be formatted <"+" / script>

0
source

I added @DaveWalker in the answer:

 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 .

0
source

I saw a lot of announcements and no one works for me. So I tried the following code and solved my problem:

First, I changed the mt button using the link button with the same aspect as the button.

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

After that, I will add the following to my code.

 btPrint.Attributes.Add("href", String.Format("printPage.aspx?&id={0}", txtId.Text)); btPrint.Attributes.Add("target", "_blank"); 
0
source

It works for me

In aspx code:

  <a id="myLink" target="_blank" onclick="window.open('ExamplePage.aspx, '_blank');">Link To A Page And Open Other Tab</a> 
0
source
 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(); 
-1
source

Source: https://habr.com/ru/post/906791/


All Articles