如何在response.redirect中使用Target=_blank?

9

我不一定非要使用response.redirect,但这是我的想法。我想在新窗口中打开所选链接。我该怎么做?

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

2
可能是重复问题:[https://dev59.com/IHVD5IYBdhLWcg3wDXJ3] - perfectionist
12个回答

16

简单来说,你不能打开一个新的浏览器窗口,因为只有浏览器才能做到。

但你可以在返回的HTML代码中添加一个带有链接和目标="_blank"属性的标签,并通过一段JavaScript在页面加载时模拟点击该标签。如果这种方法不起作用,则可以使用window.open(url)函数。

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

5

你正在尝试从服务器端完成客户端任务,因此需要进行一些黑客攻击。

一种选择是发送一个只包含一小段JavaScript代码的页面,这个代码将处理重定向。

虽然不太干净,但有什么办法呢:

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

2
弹出窗口拦截器怎么办? - GlennG
这会生成一个错误,内容为“检测到来自客户端的可能危险的Request.Path值”。 - TheAlbear

2

使用 Response.Redirect() 是无法实现这个功能的。

不过,你可以在 Response.Write 中嵌入简单的 JavaScript 代码来实现。

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

2
使用按钮的 OnClientClick 属性:
<asp:Button runat="server" ID="cmd_Test" onclientclick="window.open('YourUrl')"  />

2
如果您只需要处理导航,可以尝试使用ASP:Hyperlink控件而不是按钮,这样在渲染页面时浏览器就会指定目标。
protected void Page_Load (object sender, EventArgs e)
{
    lnkViewPage.NavigateURL = sURL;
    lnkViewPage.Target = "_blank";
}

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

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

尝试将答案写得整洁,以便他们容易理解。 - Chella

0
如何动态编程超链接?想象一下,当您单击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 ');");

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


0

我使用这段代码进行重定向:

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

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


0

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

不幸的是,您无法做到这一点,因为重定向是由服务器而不是浏览器提供的。您可以潜在地重定向到包含一些脚本的页面,该脚本基于URL查询字符串参数打开一个新窗口。但是,如果您不小心,这将使您容易受到XSS攻击。


0
我看过很多答案,但没有一个真正适合我。所以我尝试了以下代码,解决了我的问题:
首先,我将按钮更改为具有与按钮相同外观的链接按钮。
<asp:LinkButton ID="btPrint" runat="server" class="btn btn-primary"><span class="glyphicon glyphicon-print"></span> Print</asp:LinkButton>

在我的代码后面,我添加了以下内容。
btPrint.Attributes.Add("href", String.Format("printPage.aspx?&id={0}", txtId.Text));
btPrint.Attributes.Add("target", "_blank");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接