Selecting TextBox text when residing in a RadWindow

5 posts, 1 answers
  1. Robert
    Robert avatar
    253 posts
    Member since:
    Aug 2007

    Posted 01 Feb 2012 Link to this post

    For a long time now I've used a dependable Javascript function to preselect the text in a textbox.  However I discovered today that when a textbox resides in an AJAXifield RadWindow, a more complex approach has to be used.  Here's the code showing both approaches:

      function SetFocusAndSelectText(tbID) {<br>    var textBox = $get(tbID);<br><br>    // Standard approach<br>    textBox.focus();<br>    textBox.select();<br><br><br>    // Approach when the TextBox resides in an AJAXified RadWindow<br>    //    function f() {<br>    //      debugger <br>    //      if (textBox != null) {<br>    //        textBox.focus();<br>    //        textBox.select();<br>    //      }<br><br>    //      Sys.Application.remove_load(f);<br>    //    }<br><br>    //    Sys.Application.add_load(f);<br>  }

    I'm wondering if there's a way to test for whether the textbox sits within a RadWindow?  If so, then I could just call one function and let it decide which approach to use.

    Sincerely,

    Robert W.
  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4526 posts

    Posted 03 Feb 2012 Link to this post

    Hi Robert,

    I assume your textbox is in the ContentTemplate of a RadWindow. If this is the case then it acts like any other container - you can check, for example for its parent elements, yet this is an unsafe approach for a common function - you cannot be always certain what the page's layout would be.

    The approach when you use the Sys.Application.Load event to make sure the response is received would also be applicable for a regular postback, so you can leave only this function.

    Alternatively, you can create two functions - one to use with AJAX and one - with your current approach for the rest of the scenarios.


    Greetings,
    Marin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. Robert
    Robert avatar
    253 posts
    Member since:
    Aug 2007

    Posted 03 Feb 2012 Link to this post

    Marin,

    You wrote: "The approach when you use the Sys.Application.Load event to make sure the response is received would also be applicable for a regular postback, so you can leave only this function."

    I would have thought that too but I tested the Sys.Application only version on a page where the controls were not in a RadWindow.  It didn't work.

    Robert
  4. Answer
    Marin Bratanov
    Admin
    Marin Bratanov avatar
    4526 posts

    Posted 06 Feb 2012 Link to this post

    Hello Robert,

    I tested this approach and things seem to be working properly with me: http://screencast.com/t/W2H8iZvrdRS. I only moved the line where we get the reference inside the function we call f, so that I am sure that it can be referenced. I am also attaching my test page as a reference. Please compare it with your scenario and try to find the differences. What I would advise is that  you make sure you pass a correct ClientID to this JS function so that you get a correct reference.


    Regards,
    Marin
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Robert
    Robert avatar
    253 posts
    Member since:
    Aug 2007

    Posted 12 Feb 2012 Link to this post

    Marin,

    My apologies for the delay in getting back to you.

    In any case, your solution works perfectly in both RadWindow and non-RadWindow containers.  I'm really not sure why the approach didn't seem to be working for me before. But all is good now!

    Robert
Back to Top