在同一个窗口中打开网址 [英] Open url in same window

查看:80
本文介绍了在同一个窗口中打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法(在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.

The ASP.NET Button server control will render as type="submit".Invoking the OnClick event will submit the form, and result a page reload, which clashes with your attempt using window.open with _self. You can just add a type="button" attribute to the button to prevent that. Alternatively, you might want to consider using window.location.href instead: JavaScript Window Location[^]


这篇关于在同一个窗口中打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆