This is a migrated thread and some comments may be shown as answers.

[Solved] RadEditor setFocus

3 Answers 179 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Erling Ervik
Top achievements
Rank 1
Erling Ervik asked on 09 Mar 2010, 01:22 PM
The radEditor has a clientside api called setFocus, thats supposed to set focus to the control. I'm not much of a javascript programer, so I would like to make a function from serverside code with a parameter for the actuall radEditor control to set focus to this control.

On the serverside we could set the function by something like:

ClientScript.RegisterStartupScript(Me.GetType, "Key", sb.ToString)

Where the actual function is buildt with stringbuilder.

In the masterpage we have this function:

 

<script type="text/javascript" language ="javascript" > 
    function SetEditorFocus(id) {  
        var editor = document.getElementById(id);  
        editor.setFocus();  
        return true;  
    }  
</script> 
 

Is this function ok?

If it's ok, what to put into the stringbuilder to get this to work from serverside?

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 10 Mar 2010, 02:57 PM
Hello Erling,

You should be aware that in MS ASP.NET AJAX client control objects are created in a manner similar to this:

   .......
   <script type="text/javascript">
   <!--
   Sys.Application.initialize();
   Sys.Application.add_init(function() {
       $create(MS.AJAX.SomeControl, {controlparameters : ''}, null, $get("controlelement"));
   });
   // -->
   </script>
 
</body>
</html>

Here is how the client events are fired in the page life cycle:

window.onload -> Sys.Application.init -> Sys.Application.load

You can see that the client object representation of MS AJAX controls are created just as the last code on the page in the Sys.Application.init event, which is raised after the window.onload event. 

For that reason you can get a reference to RadEditor no earlier that in the Sys.Application.load event, which is raised after all scripts have been loaded and the objects in the application have been created and initialized.

Here is how to modify your code in the Page_Load server event handler:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load 'MyBase.Load,
        Me.ClientScript.RegisterStartupScript(Me.GetType(), "focusScript", "function pageLoad() { setTimeout(function(){$find('RadEditor1').setFocus();}, 100); }", True)
    End Sub

Kind regards,
Rumen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Erling Ervik
Top achievements
Rank 1
answered on 11 Mar 2010, 02:25 PM
Thanks for your answer. Unfortunately, I still can't get this to work.  Her is the complete sorce for the page_load function serverside:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        If Session("Level") < 1 Then  
            Server.Transfer("Login.aspx")  
        End If  
        If Not Me.IsPostBack Then  
            FyllCbo()  
            FyllBilde()  
            Me.reSvar.Enabled = True 
            Me.reSvar.Focus()  
        End If  
        Me.ClientScript.RegisterStartupScript(Me.GetType(), "focusScript", "function pageLoad() { setTimeout(function(){$find('reSvar').setFocus();}, 100); }", True)  
    End Sub 
When running, the page stops at the line show in the atachement.
The Radeditor I like to have focus on startup is called reSvar.
Do you see what the problem is?
0
Rumen
Telerik team
answered on 15 Mar 2010, 03:37 PM
Hello Erling,

You should pass as a parameter of the $find('') the ID of RadEditor, but not the name of the control.

Here is a working solution:

Imports Telerik.Web.UI
Partial Class _Default
    Inherits System.Web.UI.Page
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim editor As New RadEditor()
        editor.ID = "RadEditor1"
        form1.Controls.Add(editor)
        Me.ClientScript.RegisterStartupScript(Me.GetType(), "focusScript", "function pageLoad() { setTimeout(function(){$find('" & editor.ID & "').setFocus();}, 100); }", True)
    End Sub
End Class

Here is another easier solution that use the OnClientLoad event to get a reference to RadEditor, e.g.

Imports Telerik.Web.UI
Partial Class _Default
    Inherits System.Web.UI.Page
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim editor As New RadEditor()
        editor.ID = "RadEditor1"
        editor.OnClientLoad = "OnClientLoad"
        form1.Controls.Add(editor)
        Me.ClientScript.RegisterStartupScript(Me.GetType(), "focusScript", "function OnClientLoad(editor) { setTimeout(function(){editor.setFocus();}, 100); }", True)
    End Sub
End Class


Kind regards,
Rumen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Editor
Asked by
Erling Ervik
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Erling Ervik
Top achievements
Rank 1
Share this question
or