Focus lost in RadWindow

4 posts, 0 answers
  1. Datamex
    Datamex avatar
    161 posts
    Member since:
    Jun 2012

    Posted 22 Sep 2009 Link to this post

    Hi,

    I have a RadWindow in which a page is shown, containing a form. I wish the first RadTextBox in that Form to have focus, which works as it should when the page is loaded directly.
    But, when I load the page in a RadWindow (dynamically, as it uses a QueryString for parameters, so the url is set from javascript), none of the Controls have focus.
    I have altered the page by adding a line that explicitly sets focus to the control in the page load event, but it didn't work. I finally discovered, that setting ShowContentDuringLoad="true" on the RadWindow solved the problem. But that has a negative effect, because if the RadWindow is opened a second time (with a different QueryString), the old page is displayed while the new is loading, which gives a strange effect.

    I have tried adding a javascript function to the onload event of the page, which sets focus to the RadTextBox, but even that seems cancelled by the RadWindow.

    How can I make sure that the RadTextBox has focus after opening the page in a RadWindow?
  2. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 23 Sep 2009 Link to this post

    Hello Datamex,

    In this case I suggest to use RadTextBox's client-side API and to call its focus() method in the ASP.NET AJAX's pageLoad() function (it is automatically called when all ASP.NET AJAX controls are loaded on the page).
    e.g.
    <script type="text/javascript"
     
        function pageLoad() 
        { 
            var radtextbox = $find("<%= RadTextBox1.ClientID %>"); 
            radtextbox.focus(); 
        } 
     
    </script> 
     
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox><br /> 
    <telerik:RadTextBox ID="RadTextBox1" runat="server"
    </telerik:RadTextBox> 
    <br /> 
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox> 


    Regards,
    Georgi Tunev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Datamex
    Datamex avatar
    161 posts
    Member since:
    Jun 2012

    Posted 23 Sep 2009 Link to this post

    Hello Georgi,

    The solution you proposed yields the same results. But I have discovered something strange: both setting focus from codebehind as well as setting it from JavaScript (either the onload= and pageLoad() approach) work when I have a breakpoint on the line that sets focus and wait a little while before pressing play again.

    I have tried adding a Thread.Sleep(1000) call in the codebehind (which is not much of a fix, but just a try), but even giving it a long time to sleep would not yield the same result as pausing on a breakpoint.

    Using Firebug and pausing on a breakpoint in the JavaScript also solved the problem. I haven't been able to try a Thread.Sleep equivalent in JavaScript, as JavaScript doesn't support waiting.

    As a last remark, I'd like to point out that I have been using Firefox for testing. If I try your solution in IE8, it works as it should.
  5. Georgi Tunev
    Admin
    Georgi Tunev avatar
    7207 posts

    Posted 23 Sep 2009 Link to this post

    Hello Datamex,

    Thank you for the feedback - now I was able to reproduce the problem. I suggest using a small timeout in such case - just enough to allow the browser to properly focus the textbox (when ShowContentDuringLoad is set to false, the initial size of the RadWindow's IFRAME is 0x0 pixels and after the page is loaded, it is "stretched" to fill the window content area).

    function pageLoad() 
        window.setTimeout(function() 
        { 
            var radtextbox = $find("<%= RadTextBox1.ClientID %>"); 
            radtextbox.focus(); 
        }, 100); 


    Greetings,
    Georgi Tunev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top