我不一定非要使用response.redirect,但这是我的想法。我想在新窗口中打开所选链接。我该怎么做?
context.Response.Redirect(ConfigurationManager.AppSettings["URL"] + ext );
简单来说,你不能打开一个新的浏览器窗口,因为只有浏览器才能做到。
但你可以在返回的HTML代码中添加一个带有链接和目标="_blank"属性的标签,并通过一段JavaScript在页面加载时模拟点击该标签。如果这种方法不起作用,则可以使用window.open(url)函数。
response.write("<script>");
response.write("window.open('page.html','_blank')");
response.write("</script>");
你正在尝试从服务器端完成客户端任务,因此需要进行一些黑客攻击。
一种选择是发送一个只包含一小段JavaScript代码的页面,这个代码将处理重定向。
虽然不太干净,但有什么办法呢:
Response.Write("<script>window.open('http://www.somesite.com/','_blank');</script>");
使用 Response.Redirect()
是无法实现这个功能的。
不过,你可以在 Response.Write
中嵌入简单的 JavaScript 代码来实现。
Response.Write("<script>window.open('page.html','_blank')</script>");
<asp:Button runat="server" ID="cmd_Test" onclientclick="window.open('YourUrl')" />
protected void Page_Load (object sender, EventArgs e)
{
lnkViewPage.NavigateURL = sURL;
lnkViewPage.Target = "_blank";
}
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();
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 ');");
这只是一个替代标准按钮的选择,否则会调用点击处理程序。请记住,您可以将整个内容作为属性从前面添加。
我使用这段代码进行重定向:
Response.Write("window.open('http://www.whatever.com','_blank')<"+"/script>");
最后的标签需要格式化为<"+"/script>
如果我理解正确的话,您想要能够在新窗口中打开重定向的URL,但是保留原始目标在同一窗口中。
不幸的是,您无法做到这一点,因为重定向是由服务器而不是浏览器提供的。您可以潜在地重定向到包含一些脚本的页面,该脚本基于URL查询字符串参数打开一个新窗口。但是,如果您不小心,这将使您容易受到XSS攻击。
<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");