ListBox 在调用 window.open() 后被阻止

ListBox is blocked after calling window.open()

本文关键字:调用 window open ListBox      更新时间:2023-09-26

我在Page_Load()中注册了以下JavaScript:

var scriptReihe = "<script type='"text/javascript'">function OnClientLoadHandlerReihe(sender) {"
                     + "var listbox = $find('"" + lbReihen.ClientID + "'");"
                     + "var item = listbox.get_selectedItem();"
                     + "item.ensureVisible();"
                     + "}"
                     + "</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OnClientLoadHandlerReihe", scriptReihe);
lbReihen.OnClientLoad = "OnClientLoadHandlerReihe";

其中lbReihenRadListBox

这工作得很好,selectedItem在列表框的可见区域中。

在页面上,还有一个按钮:

<asp:Button ID="myBtn" runat="server" Text="Call google" OnClientClick="window.open('http://www.google.ch', '_blank');" />

现在的问题是,当单击按钮并打开新页面(在新选项卡中)时,我的ListBox之后被阻止。我无法在其中滚动等。

当我没有为OnClientLoad注册事件处理程序时,一切正常。

有人可以给我一个提示,有什么问题吗? - 谢谢。

请确保在每次回发时注册脚本,因为提供的按钮声明将在主页上调用回发。如果脚本未正确注册,您将收到脚本错误,这将解释为什么您在滚动到该项目时遇到问题,以及为什么如果不添加处理程序,一切看起来正常。也许在标记中添加脚本块并使用服务器代码块来获取列表框 ID 会更容易,如下所示:

        <telerik:RadListBox ID="lbReihen" runat="server"></telerik:RadListBox>
        <telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
            <script type="text/javascript">
                function OnClientLoadHandlerReihe(sender) {
                    var listbox = $find("<%=lbReihen.ClientID%>");
                    var item = listbox.get_selectedItem();
                    item.ensureVisible();
                }
            </script>
        </telerik:RadCodeBlock>

此外,请考虑通过返回 false 来阻止按钮回发:

<asp:Button ID="myBtn" runat="server" Text="Call google" OnClientClick="window.open('http://www.google.ch', '_blank'); return false;" />