简体   繁体   English

将Focus设置为从C#(或JS)弹出的窗口

[英]Set Focus to a popup window from C# (or JS)

I'm trying to get a popup window to focus i tried the window.open().focus(); 我正在尝试获取一个弹出窗口以聚焦我尝试过window.open().focus(); but it keeps changing in the background so I'm trying in my code to close a window and re-opening it again. 但它在后台不断变化,因此我尝试在我的代码中关闭一个窗口,然后再次将其重新打开。

string url = "Report.aspx";
string s = "window.open('" + url + "', 'popup_window456789123', 'width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes')";

string t = "window.close()";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", t, true);

ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);

Can anyone tell me why this doesn't work? 谁能告诉我为什么这行不通?

I tried it in Jquery but that didn't work either: 我在Jquery中尝试了一下,但是还是没有用:

<script type="text/javascript">
    $(document).ready(function () {
        alert("test");
        window.focus();
    });
</script>

Flow: 流:

  1. Main page: click button1 ► opens popup1 主页:单击按钮1►打开弹出窗口1
  2. popup is in focus and shows data 弹出窗口处于焦点并显示数据
  3. mainpage(gets focus): click button2 ► opens same popup1 主页(获取焦点):单击按钮2►打开相同的弹出窗口1
  4. popup changes data (but dos not get focus) This is the issue 弹出窗口更改数据(但没有获得焦点) 这是问题

Ok I've done a fiddle which shows what you should have. 好的,我做了一个小提琴,显示了您应该拥有的东西。 View Fiddle 查看小提琴

If I click off one window and click on another then it comes back (reloads) and gets focus. 如果我单击一个窗口,然后单击另一个窗口,则它将返回(重新加载)并获得焦点。

If you are expecting something like a text box getting focus or something in the loaded window then you need to have something within the page that is getting loaded to give that text box focus. 如果您希望某个文本框成为焦点,或者在加载的窗口中出现某个东西,那么您需要在页面中放置一些东西以使该文本框成为焦点。

      <script> function openwin(url) {
      window.open(url, 'popup_window456789123','width=650,height=500,left=100,top=100,resizable=yes,scrollbars=yes ')
    }
</script>

    <button onclick="openwin('https://www.webpagetest.org/')">
Button1
</button>
<button onclick="openwin('https://www.webpagetest.org/')">
Button2
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM