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

[Solved] OnClientFocus does not fire when combobox is focused programatically

2 Answers 294 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Cyber High
Top achievements
Rank 1
Cyber High asked on 15 Jan 2010, 06:38 PM
I have set the OnClientFocus event of the combobox. When you click on the combobox or tab to it, the function gets called correctly.

If I call combobox.focus() from the server side, or if I get a reference to the Input Dom element client side and focus it, the OnClientFocus function is not fired.

To clarify, when I say client side, I mean when I add the client functions call to the AjaxManager's ResponseScript Collection. When I do this, $find('comboxClientID') returns null. I have tried adding the response script on PageLoad and PreRender.

Here is an example. In the code below, pressing the "Test" button on the page focuses the combobox correctly and the onclientfocus method gets called. If either of the server side methods are called, it does not work. I need to find a solution that will work when initiated on page load or prerender.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
</head> 
<body> 
  <form id="form1" runat="server"
        <asp:scriptmanager id="scriptmanager1" runat="server" /> 
        <telerik:radscriptblock id="radscriptblock1" runat="server"
            <script type="text/javascript"
 
                function rcb1_ClientFocus(sender, args) { 
                    alert('testing'); 
                } 
 
                function focusComboBox() { 
                    var rcb = $find('rcb1'); 
                    rcb.get_inputDomElement().focus(); 
                } 
             
            </script> 
        </telerik:radscriptblock> 
        <telerik:radcombobox id="rcb1" runat="server" onclientfocus="rcb1_ClientFocus" /> 
        <p><input type="button" value="Test" onclick="focusComboBox();" /></p
        <telerik:radajaxmanager id="ram1" runat="server"></telerik:radajaxmanager> 
  </form> 
</body> 
</html> 

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
public partial class Test : System.Web.UI.Page { 
 
  protected void Page_Load(object sender, EventArgs e) { 
        if (!IsPostBack) { 
            //scriptmanager1.SetFocus(rcb1.ClientID + "_Input"); 
            //RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("focusComboBox();"); 
        } 
  } 
 
    protected void Page_Prerender(object sender, EventArgs e) { 
        if (!IsPostBack) { 
            //scriptmanager1.SetFocus(rcb1.ClientID + "_Input"); 
            //RadAjaxManager.GetCurrent(Page).ResponseScripts.Add("focusComboBox();"); 
        } 
    } 
 

2 Answers, 1 is accepted

Sort by
0
Cyber High
Top achievements
Rank 1
answered on 19 Jan 2010, 02:56 AM
I figured out that I need to use the OnClientLoad event in order to get a client-side reference to the combobox on page load. From there I can call the method to focus the combobox and its OnClientFocus event will fire.
0
Kalina
Telerik team
answered on 19 Jan 2010, 09:12 AM
Hi Guido Prambs,

When you use ScriptManager or RadAjaxManager in your server code, the script that focuses the input executes before the client-side object of RadComboBox is initialized. That is why the input has the focus and the Focus event does not fire or the $find('comboxClientID') returns null in the second case. (The sequence of script execution can be seen when you open page source rendered in browser.)

The approach here is to set the focus on the input in the client-side Load event of RadComboBox, because at this stage the RadComboBox object will be already initialized:

<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager1" runat="server" />
<telerik:radscriptblock id="radscriptblock1" runat="server">
<script type="text/javascript">
    function rcb1_ClientFocus(sender, args) {
        alert('testing');
    }
 
    function focusComboBox() {
        var rcb = $find('rcb1');
        alert(rcb);
        rcb.get_inputDomElement().focus();
    }
 
    function onLoad(sender) {
        sender.get_inputDomElement().focus();
    }
</script>
    </telerik:radscriptblock>
<telerik:radcombobox id="rcb1" runat="server" OnClientFocus="rcb1_ClientFocus" />
<p>
    <asp:Button ID="Button1" runat="server" Text="Submit" /></p>
<telerik:radajaxmanager id="ram1" runat="server"></telerik:radajaxmanager>
</form>

protected void Page_Load(object sender, EventArgs e)
{
 
     if (!IsPostBack)
     {
         rcb1.OnClientLoad = "onLoad";
     }
     else
         rcb1.OnClientLoad = string.Empty;
 }

Sincerely yours,
Kalina
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.
Tags
ComboBox
Asked by
Cyber High
Top achievements
Rank 1
Answers by
Cyber High
Top achievements
Rank 1
Kalina
Telerik team
Share this question
or