![](/img/trans.png)
[英]asp.net: How to open a new asp.net page in new window (not tab or current page) using c#
[英]How to open page in new tab using the response. redirect at asp.net
我想在按钮单击处理程序中使用Response.Redirect
打开新选项卡或新页面。 我正在使用查询字符串来传递一些值。 如何在新标签页中打开他的页面?
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Redirect("NewQuote.aspx?val=" + this.txtQuotationNo.Text);//displaying gridview in other page to print what needed
}
尝试这个。 这对我来说肯定有效......
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Response.Write("<script>window.open ('NewQuote.aspx?val=" + txtQuotationNo.Text+"','_blank');</script>");
}
简单的解决方案在这里。
编辑您的 html 按钮元素并添加属性OnClientClick="target ='_blank';" .
<asp:Button ID="myButton" runat="server" CssClass="btn1"
OnClick="btnSave_Click" OnClientClick="target ='_blank';" />
然后在 btnSave_Click
protected void btnSave_Click(object sender, EventArgs e) {
Response.Redirect(url);
}
您可以更改您的设计元素如下: OnClientClick
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick ="document.forms[0].target = '_blank';" />
Javascript代码:
Response.Write("<script>");
Response.Write("window.open('NewQuote.aspx' ,'_blank')");
Response.Write("</script>");
我今天遇到了同样的问题。
Response.write 对我不起作用,为了解决它,我改用 System.Web.UI.ScriptManager.RegisterClientScriptBlock。
这就是你的 btnSave 点击事件应该是这样的:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
}
注意:我使用 ScriptManager.RegisterClientScriptBlock 是因为该按钮位于 UpdatePanel 内,如果不是这种情况,您应该尝试:
protected void btnSave_Click(object sender, EventArgs e)
{
...//some code to insert records
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openModal", "window.open('NewQuote.aspx?val= " + this.txtQuotationNo.Text + "' ,'_blank');", true);
}
如果您使用的是 http 则使用以下代码
Response.Write("<script>window.open ('URL','_blank');</script>");
此代码不能用于 https for https,请执行以下操作
页面中的javascript
function shwwindow(myurl) {
window.open(myurl, '_blank');
}
在c#代码后面
string URL = ResolveClientUrl("~") + "**internal page path**";
ScriptManager.RegisterStartupScript(this, this.GetType(), "show window",
"shwwindow('"+URL+"');", true);
此代码不能绕过浏览器弹出窗口阻止程序。 用户必须允许它运行。 例如,它在新窗口或新标签中打开,直到 ie 版本在 Firefox 和 chrome 中打开新标签
好好享受!!
重定向始终与您来自的页面位于同一页面中,您无法通过重定向调用打开新窗口。
我建议在客户端注入一些 javascript 代码以在重新加载时打开新页面,或者更改为可以打开新页面的LinkButton
,例如具有正确Target
属性的LinkButton
。
您可以使用 ScriptManager 执行所需的操作:
private void Redirect_New_Tab(string url_To_Open)
{
string modified_URL = "window.open('" + url_To_Open + "', '_blank');";
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", modified_URL , true);
}
当我离开目标 ='_blank' 的双引号时为我工作;
Response.write 对我不起作用,所以我改用 System.Web.UI.ScriptManager.RegisterClientScriptBlock。
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
我在这上面花了很多时间,虽然将表单上的目标设置为 _blank 确实有效,但这意味着该页面上的每个其他控件之后都会尝试在新的 window 中打开。 而且您不能操纵表单后台服务器端,因为响应永远不会到达原始选项卡。
因此,我结合了在单击按钮时将目标设置为 _blank,然后在不久后将目标重置回来的组合。
我在页面上输入了这个 Javascript function:
<script type="text/javascript">
function openInNewTab() {
document.forms[0].target = "_blank";
setTimeout(function () {
document.forms[0].removeAttribute('target'); // After 500 ms revert back to normal mode so all other controls are not affected.
}, 500);
}
在需要在新选项卡中打开的控件上,只需添加:
OnClientClick="openInNewTab();"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.