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

Setting Focus IE/FF

2 Answers 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Declan
Top achievements
Rank 2
Declan asked on 17 Nov 2008, 04:30 PM
The following works just fine in FireFox to set focus to a text box when the form is in edit mode:

Private Sub grd_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grd.ItemDataBound 
 
If (TypeOf (e.Item) Is GridEditFormItem) AndAlso e.Item.IsInEditMode Then 
 
Dim box As TextBox = e.Item.FindControl("ContactName"
grd.ClientSettings.ClientEvents.OnRowCreated = String.Format("document.getElementById('{0}').focus()", box.ClientID) 
                     

In IE I get an error:

Sys.InvalidOperationException: Handler must be a function.

function Sys$Component$_setReferences(component, references) { 
 ... other code here ... 
    if (events) { 
        for (var name in events) { 
            if (!(component["add_" + name] instanceof Function)) throw new Error.invalidOperation(String.format(Sys.Res.undefinedEvent, name)); 
            if (!(events[name] instanceof Function)) throw new Error.invalidOperation(Sys.Res.eventHandlerNotFunction); 
            component["add_" + name](events[name]); 
        } 
    } 
 

The var name contains "rowCreated" suggesting that IE doesn't like "document.getElementById('{0}').focus()"

I have also tried inserting a function call here with a function in the ascx. This works in FF but, yet again not in IE.

I have read a lot of posts on this subject and tried many without success. IE seems to be the difficulty as it works in FF, Safari and Chrome.

All I want to do is setfocus to a text box when the grid enters edit mode. Not that I think this should be a problem but I am using dynamically loaded ascx controls running in DotNetNuke.

Declan





2 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 20 Nov 2008, 01:09 PM
Hello Declan ,

You have 3 options to achieve this functionality:

1. Enable RadGrid.KeyboardNavigation - thus when item enter in edit mode first editable control in the current EditForm will be focused. You should enable the following settings:
<ClientSettings AllowKeyboardNavigation="true">  
</ClientSettings> 

2. On RadGrid.ItemDataBound event  use this code:
void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            TextBox control = ((GridTextBoxColumnEditor)((GridEditFormItem)e.Item).EditManager.GetColumnEditor("ShipName")).TextBoxControl;  
            control.Focus();              
        }  
    } 
 
3. If both approaches above doesn't help you can try this one:
    void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            TextBox control = ((GridTextBoxColumnEditor)((GridEditFormItem)e.Item).EditManager.GetColumnEditor("ShipName")).TextBoxControl;  
            StringBuilder script = new StringBuilder();  
            script.Append("function(sender,args){debugger;");  
            script.AppendFormat(@"$get('{0}').focus();", control.ClientID);  
            script.Append("}");  
            RadGrid2.ClientSettings.ClientEvents.OnGridCreated = script.ToString();  
        }  
    } 

I hope this was helpful.

Kind regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rahul Khinvasara
Top achievements
Rank 1
answered on 04 May 2009, 07:44 AM
try this 

 

if (e.Item.OwnerTableView.Name == "gvVendor" && e.Item is GridEditFormItem && e.Item.IsInEditMode)

 

{

 

TextBox txtVendorName = (TextBox)e.Item.FindControl("txtVendorName");

 

txtVendorName.Focus();

}

 

else if (e.Item.OwnerTableView.Name == "gvProducts" && e.Item is GridEditFormItem && e.Item.IsInEditMode)

 

{

 

TextBox txtswName = e.Item.FindControl("txtswName") as TextBox;

 

txtswName.Focus();

}

Tags
Grid
Asked by
Declan
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Rahul Khinvasara
Top achievements
Rank 1
Share this question
or