textbox loses focus upon ajax timer async postbacks.

Last post 10-27-2009 3:06 AM by SkyStrike. 8 replies.

Sort Posts:

  • textbox loses focus upon ajax timer async postbacks.

    01-01-2008, 1:54 AM
    • Member
      12 point Member
    • tal88
    • Member since 08-21-2007, 9:12 AM
    • Posts 143

    Hi,

    Im writting a custom server control (inherited from CompositeControl) .

    the custom control is composed from a TextBox and an ajax timer.

    I'm using the timer control to refresh content every 5 seconds

     I lose focus on my textbox when the timer fires. (every async postback)

    I registered a javascript block that set the focus on the TextBox  through ScriptManaer.RegisterStartupScript(Page, Type, String, String, Boolean).

    but it doesn't seem to help. (it enters the script and for a second its focused  , but afterwards the focus is losed).

    any idea?

  • Re: textbox loses focus upon ajax timer async postbacks.

    01-01-2008, 4:45 AM
    Are you sure you are using the ClientId of the TextBox in the javascript you are registering?
    Regards,
    Prashant


    Dont forget to click "Mark as Answer" on the post that helped you.
  • Re: textbox loses focus upon ajax timer async postbacks.

    01-01-2008, 5:08 AM
    • Member
      12 point Member
    • tal88
    • Member since 08-21-2007, 9:12 AM
    • Posts 143

    Hi,

    Thanks for your reply.

    yes Im sure its correct , because if I put alert before and after the focus I can see its focused , but if I remove the alerts

    its to quick to see the focus on the textbox (it runs away too fast upon async postbacks generated by the ajax timer)

    I tried to add a setInterval with the focus command every 2 seconds.

    it seems to work, but im not sure its the best solution.

    I also have a problem with AutoCompleteExtender targeting that textbox , as the completion list disappears when an

    async postback occurs.

    Any idea?

    Thanks,

    Tal88

  • Re: textbox loses focus upon ajax timer async postbacks.

    01-02-2008, 2:56 PM

    ScriptManager.GetCurrent(Page).SetFocus(TextBoxName)

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: textbox loses focus upon ajax timer async postbacks.

    01-02-2008, 5:22 PM
    • Star
      13,541 point Star
    • gt1329a
    • Member since 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 2,236
    • TrustedFriends-MVPs

    It's important to understand that if your TextBox is inside an UpdatePanel that's being updated, the entire element is destroyed and refreshed when the partial postback completes.  So, even though the update may seem somewhat seamless in the browser, the element that was previously focused doesn't even exist after the partial postback completes.

    You can try to work around that by using SetFocus or other methods of refocusing the element, but there is no way to completely maintain focus since the element is being destroyed and recreated every five seconds or so.

    If you can manage it, you should try to move the TextBox outside of the UpdatePanel that's being refreshed by the Timer. 

    Encosia - ASP.NET, jQuery, AJAX, and more.

    Latest article: Emulate ASP.NET validation groups with jQuery validation
  • Re: textbox loses focus upon ajax timer async postbacks.

    01-02-2008, 10:23 PM

    Sets the browser focus to the specified control

    http://www.asp.net/AJAX/Documentation/Live/mref/O_T_System_Web_UI_ScriptManager_SetFocus.aspx

     

    You can use the following code to set the focus:
     

    ScriptManager sm = ScriptManager.GetCurrent(this);
    sm.SetFocus(myTextBox);

    To set focus and select text of a you have to use the following:
     

    ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
    http://forums.asp.net/t/1099345.aspx

     

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: textbox loses focus upon ajax timer async postbacks.

    01-03-2008, 3:01 AM
    Answer
    • Member
      12 point Member
    • tal88
    • Member since 08-21-2007, 9:12 AM
    • Posts 143

    Hi,

    Thanks alot for the reply,

    I moved the TextBox outside the UpdatePanel, and it solved all the problems i had :-)

    (the set focus problem , and the completion list now isn't displayed upon every async postback, even though TextChanged event didn't occur...)

    anyway Im interested to know what is the best solution if i want that the event of selecting an item from complition list (AutoCompleteExtender)

    will generate an asyn postback?

    As far as I know , add an UpdatePanel and add the TextBox to the triggers collection.

    this solution is not optimal because im not interested in rerendering content.

    (my custom server control, inherited from CompositeControls is composed of a TextBox , UpdatePanel and an AutoCompleteExtender).

    Any Idea?

    Thanks,

    Tal88

     

  • Re: textbox loses focus upon ajax timer async postbacks.

    01-26-2009, 7:24 PM
    • Member
      4 point Member
    • sunithashukla
    • Member since 01-20-2009, 6:47 PM
    • Posts 2

    Hi,

    I am having same situation.Below code helps me alot.Thanks for it.

    ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);

    I need this same code in javascript .how can i do it.

    Can you please help me.

  • Re: textbox loses focus upon ajax timer async postbacks.

    10-27-2009, 3:06 AM
    • Member
      20 point Member
    • SkyStrike
    • Member since 09-13-2007, 9:11 PM
    • Posts 5

    i was using this following piece of code

    <script type="text/javascript">

    document.getElementById(" + <%=myTextBox.ClientID%> + ").focus();

    </script>


    add this piece of code at the bottom of the page or at least below the textbox (to prevent javascript errors)


    It is a dirty but quick solution.

Page 1 of 1 (9 items)