ListBox is blocked after calling window.open()

82 Views Asked by At

I register the following JavaScript in the Page_Load():

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";

where lbReihen is a RadListBox

This is working great and the selectedItem is in the visible area of the ListBox.

On the Page, there is also a button:

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

The problem is now, when the button will be clicked and the new page (in a new tab) will be opened, my ListBox is blocked after. I cannot scroll in it etc.

When I don't register the EventHandler for OnClientLoad, all is working great.

Can someone give me a hint, what there is wrong? - Thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

Make sure you register the script with each postback, because the provided button declaration will invoke a postback on your main page. If the script does not get registered properly, you will get script errors, which will explain why you have issues with scrolling to the item and why things appear OK if you do not add the handler. Perhaps it is easier to just add the script block in the markup and use server code blocks to get the listbox id, something like:

        <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>

Also, consider preventing the button postback by returning false:

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