locked
set focus to end of text box in update panel postback not working in FF & Chrome RRS feed

  • Question

  • User1009387043 posted

    My solution is working fine in IE but not in FF & Chrome browsers. Please give suggestions/solution  to implement the functionality that works for all browsers. I am using the text box to search a grid view on keyup.

    Javascript Code:

    <script type="text/javascript">
    
            var tbPostBackTimeout = null;
    
            function onTextBoxChanging(sender) {
                clearTimeout(tbPostBackTimeout);
    
                tbPostBackTimeout = setTimeout(function () {
                    __doPostBack(sender.name, '');
                }, 500);
            }
    
            function setFocustxt() {
                var currentCaret;
                function getCaret(textbox) {
                    var textbox = $get("<%=txtSearch.ClientID%>");
                    var control = document.activeElement;
                    textbox.focus();
                    var rang = document.selection.createRange();
                    rang.setEndPoint("StartToStart", textbox.createTextRange());
                    return rang.text.length;
                }
    
    
                function setfocus() {
                    var txb = $get("<%=txtSearch.ClientID%>");
                    var t = txb.createTextRange();
                    t.collapse(true);
    
                    t.moveStart("character", currentCaret);
                    t.select();
    
                }
    
                Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(
                function (sender, e) {
                    currentCaret = getCaret($get("<%=txtSearch.ClientID%>"));
    
                });
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                function (sender, e) {
    
                    setfocus();
                });
    
            }
    
    </script>

    ASP.net code in update panel.

    <asp:TextBox ID="txtSearch" runat="server" Width="171px" AutoPostBack="True" OnTextChanged="txtSearch_TextChanged"
                onkeyup="onTextBoxChanging(this);"  onfocus="setFocustxt();"  ></asp:TextBox>

     

    Thursday, October 9, 2014 10:45 AM

All replies