在同一个窗口中打开网址 [英] Open url in same window
本文介绍了在同一个窗口中打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法(在C#中)需要在同一个窗口中打开一个URL,而是在新标签页中打开网址
public static void OpenSameWindow (字符串网址)
{
string cmd = @< script> window.open('{0}','_ self',false);< / script>;
System.Web.HttpContext.Current.Response.Write(string.Format(cmd,url));
}
我读过的所有内容都表明这是正确的解决方案,但它始终会在新标签页中打开。有什么想法吗?
我的尝试:
Response.Redirect(somepage.aspx,true);
rver.Transfer(somepage.aspx);
解决方案
如上所述,Response.Redirect
如果您只想导航到相同的窗口页面。
ASP.NET按钮
服务器控件将呈现为type =submit
。调用OnClick
事件将提交
表格
,并导致页面重新加载,与使用window.open
与_self
的尝试冲突。您只需在按钮中添加type =button
属性即可。或者,您可能需要考虑使用window.location.href
: JavaScript窗口位置 [ ^ ]
I have a method (in C#) that needs to open a URL in the same window, but instead it opens the url in a new tab
public static void OpenSameWindow(string url) { string cmd = @"<script>window.open('{0}','_self',false);</script>"; System.Web.HttpContext.Current.Response.Write(string.Format(cmd, url)); }
Everything I have read indicates that this is the correct solution, but it always opens in a new tab. Any thoughts?
What I have tried:
Response.Redirect("somepage.aspx",true);
rver.Transfer("somepage.aspx");
解决方案
As mentioned,Response.Redirect
should work if you just want to navigate to the same window page.
TheASP.NET Button
server control will render astype="submit"
.Invoking theOnClick
event willsubmit
theform
, and result a page reload, which clashes with your attempt usingwindow.open
with_self
. You can just add atype="button"
attribute to the button to prevent that. Alternatively, you might want to consider usingwindow.location.href
instead: JavaScript Window Location[^]
这篇关于在同一个窗口中打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文