textbox loses focus upon ajax timer async postbacks.

Last post 01-03-2008 3:01 AM by tal88. 6 replies.

Sort Posts:

  • textbox loses focus upon ajax timer async postbacks.

    01-01-2008, 1:54 AM
    • Loading...
    • tal88
    • Joined on 08-21-2007, 5:12 AM
    • Posts 142

    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
    • Loading...
    • tal88
    • Joined on 08-21-2007, 5:12 AM
    • Posts 142

    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
    • Loading...
    • gt1329a
    • Joined on 06-24-2002, 12:53 AM
    • Atlanta
    • Posts 1,521

    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. 

  • 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
    • Loading...
    • tal88
    • Joined on 08-21-2007, 5:12 AM
    • Posts 142

    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

     

Page 1 of 1 (7 items)