C# 在新选项卡中打开 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16896284/
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
Opening a URL in a new tab
提问by Pallavi Sagar
I have an url like
我有一个像
Response.Redirect("~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher");
I want to open this url in new tab of browser. I tried below code...
我想在浏览器的新标签页中打开这个网址。我试过下面的代码...
string pageurl = "~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher";
Response.Write("<script>");
Response.Write("window.open('" + pageurl + "','_blank')");
Response.Write("</script>");
also i tried below
我也试过下面
string pageurl = "~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + pageurl + "','_blank')", true);
also i tried
我也试过
<asp:Button ID="btnPrint" Text="Print" runat="server" OnClick="btnPrint_Click" OnClientClick="aspnetForm.target ='_blank';"/>
but all are not working. Please tell me any another solution. Thanks in advance.
但都不起作用。请告诉我任何其他解决方案。提前致谢。
采纳答案by Eugene Trofimenko
You are using URL with ~and it won't recognize by javascript. You should process url with ~by using ResolveUrlmethod which
您正在使用带有~ 的URL,它不会被 javascript 识别。您应该使用ResolveUrl方法使用~处理 url
converts a URL into one that is usable on the requesting client(c)msdn
将 URL 转换为可在请求客户端 (c)msdn 上使用的 URL
In your case:
在你的情况下:
Response.Write(String.Format("window.open('{0}','_blank')", ResolveUrl(pageurl)));
回答by anand360
With the help of JavaScript we can set the target property of form to _blank whenever we want to open the page in a new window. Try the following
每当我们想在新窗口中打开页面时,我们都可以借助 JavaScript 将表单的目标属性设置为 _blank。尝试以下
I have an ASP.Net Button
我有一个 ASP.Net 按钮
<asp:Button ID="btnPrint" runat="server" Text="PRINT BILL" Onclick="btnPrint_Click" OnClientClick="SetTarget();" />
I am calling the SetTarget() JavaScript function OnClientClick event of the ASP.Net Button Control as described below
我正在调用 ASP.Net 按钮控件的 SetTarget() JavaScript 函数 OnClientClick 事件,如下所述
<script type = "text/javascript">
function SetTarget() {
document.forms[0].target = "_blank";
}
</script>
calling the btnPrint_Click method OnClick event Control as described below
调用 btnPrint_Click 方法 OnClick 事件控件如下所述
protected void btnPrint_Click(object sender, EventArgs e)
{
Response.Redirect("ReportViewer1.aspx");
}
回答by Watz
The answer given by anand360 worked for me. Thanks!!
anand360 给出的答案对我有用。谢谢!!
I made a slight change in JavaScript as follows to access only the desired element.
我对 JavaScript 进行了如下轻微更改,以仅访问所需的元素。
document.getElementById["element_id"].target = "_blank";

