tisdag 22 maj 2012

Set focus on textbox in panel handled by CollapsiblePanelExtender

    </asp:Panel>
        <script type="text/javascript" language="javascript">
        //
        //  This part is done because that setting focus in code behind with someting like SetFocusOnControl(TextBoxQualifier)
        //  for some reson does not work when the text box is placed in a Panel that is handled by a CollapsiblePanelExtender.
        //
        //  Also for some reason it does not work to set focus direct in the pageLoad function (but it does work if a alert statement is done after it)
        //  thats why the setFocus function is called from setTimeout.
        //
        var objExtender;
        function pageLoad(sender, args) {
            objExtender = $find("<% =CollapsiblePanelExtender1.ClientID %>");
            objExtender.add_expandComplete(setFocus);
            objExtender.add_collapseComplete(setFocus);           
            setTimeout("setFocus()", 10);
        }
        function setFocus() {
            if (!objExtender.get_Collapsed()) {
                $get("<%=TextBoxQualifier.ClientID%>").focus();
            }
        }  
    </script>