C# 如何在 response.redirect 上使用 Target=_blank?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8994285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:30:10  来源:igfitidea点击:

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

c#asp.net.net

提问by MrM

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?

我不一定必须使用 response.redirect,但这就是我所拥有的。我想在新窗口中打开选定的链接。我怎么做?

context.Response.Redirect(ConfigurationManager.AppSettings["URL"] + ext );

采纳答案by Dave Walker

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

您可以做的是将一段 html 发送到响应中,其中包含一个带有您的 url 的链接作为 href、target="_blank" 和一段伪造点击的表单的 javascript 加载。如果这不起作用,则使用 window.open(url);

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

回答by Josh Earl

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.

一种选择是发回一个只有一点 JavaScript 的页面,然后它将处理重定向。

This isn't particularly clean, but what about:

这不是特别干净,但是呢:

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

回答by Shankar Damodaran

You cannot do that with Response.Redirect()

你不能这样做 Response.Redirect()

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

那么你可以在里面使用一个简单的 Javascript 来做到这一点 Response.Write

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

回答by Digbyswift

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.

如果我理解正确,您希望能够在新窗口中打开重定向的 URL,但大概在同一窗口中保留原始目标。

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.

不幸的是你不能这样做,因为重定向是由服务器而不是浏览器提供的。您可能会重定向到包含一些基于 URL 查询字符串参数打开新窗口的脚本的页面。但是,如果您不小心,这会使您面临 XSS。

回答by Lukas

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:

你动态编程的超链接怎么样?想象一下。asp 超链接,当您点击时打开一个新窗口,可能没有滚动条,没有地址栏,任何你想要的。下面是一个例子:

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.

这只是标准按钮的替代方案,否则会调用点击处理程序。请记住,您可以从前面添加整个内容作为属性。

回答by Brian P

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:

如果您只是处理导航,您可以尝试使用 ASP:Hyperlink 控件而不是按钮,这样在呈现页面时为浏览器指定目标:

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.

当然,不理会 .Target 更为礼貌,因为在这种情况下,可以右键单击超链接,并且可以从上下文菜单中使用“在新页面/选项卡中打开”。

回答by Rudy

Use the button's OnClientClick property:

使用按钮的 OnClientClick 属性:

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

回答by Tug Strongly

I use this code for redirects:

我使用此代码进行重定向:

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

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

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

最后的标签需要格式化 <"+"/script>

回答by JoshYates1980

I added to @DaveWalker response:

我添加到@DaveWalker 回复中:

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 popupinstead of opening a new tab.

这将创建一个弹出窗口而不是打开一个新选项卡

回答by Vishal

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