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

Getting focus of an insert column

5 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chad Johnson
Top achievements
Rank 1
Chad Johnson asked on 03 Oct 2008, 04:10 PM
Ok, so here is what's going on.  I created a radgrid with a radtoolbar within it.  When I begin an insert, I wish to make the textbox that appears for the name to have the focus of the page.  Here is what I have thus far.

protected void rgdStatusList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
        {  
            if ((e.Item is Telerik.Web.UI.GridEditFormInsertItem) && (e.Item.OwnerTableView.IsItemInserted))  
            {  
                Telerik.Web.UI.GridDataInsertItem insertItem = (Telerik.Web.UI.GridDataInsertItem)e.Item;  
                (insertItem["StatusDescription"].Controls[0] as TextBox).Focus();  
            }  
        } 

This is the example I found on this site to set the focus, but that didn't do anything for my grid.  Is there another way to do it?  Thanks.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 06 Oct 2008, 04:13 AM
Hi Chad,

Are you using InPlace editform type? If so try the following code snippet to set the focus for the textbox in insert mode.

CS:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridDataInsertItem) && (e.Item.OwnerTableView.IsItemInserted)) 
        { 
            GridDataInsertItem insertitm = (GridDataInsertItem)e.Item; 
            TextBox txtbx = (TextBox)insertitm["UniqueName"].Controls[0]; 
            txtbx.Focus(); 
        } 
    } 


Thanks
Shinu.
0
Chad Johnson
Top achievements
Rank 1
answered on 06 Oct 2008, 02:05 PM
Unfortunately, that is pretty much the same solution I had before.  I tried the same thing, and still it didn't work.  Is there possibly a way to set it clientside?
0
Sebastian
Telerik team
answered on 06 Oct 2008, 02:24 PM
Hello Chad,

You can try the javascript solution  for setting focus in grid editor presented in this article from the online documentation:

http://www.telerik.com/help/aspnet-ajax/grdsetfocusontextboxesineditcontrol.html

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Chad Johnson
Top achievements
Rank 1
answered on 06 Oct 2008, 02:57 PM
Thank you for your reply, but again, that didn't help.  My grid has no controls within itself, other than a checkbox.  I'm just wanting the first insert field (EditMode is InPlace btw, if it matters) to get the focus.
0
Sebastian
Telerik team
answered on 09 Oct 2008, 11:44 AM

Hi Chad,

When you use InPlace edit mode you will need to modify the code a bit since the control which you will reference will reside in the actual GridDataItem instead of GridEditFormItem, i.e.:

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)  
   {  
       //item is in edit mode or new item is about to be inserted  
        if ((e.Item is GridEditableItem && e.Item.IsInEditMode)|| e.Item.ItemIndex == -1)  
       {  
         RadGrid1.Controls.Add( new LiteralControl(String.Format( "<script type='text/javascript'>document.getElementById('{0}').focus();document.getElementById('{0}').select();</script>", ((e.Item as GridEditableItem)["<MyFirstColumnUniqueName>"].Controls[0] as TextBox).ClientID));  
       }  
   }  

Let me know whether this helps. You may also use the Focus() server method of the ASP.NET framework and test whether disabling the ajax for the grid (if the control is ajaxified in your case) makes a difference.

Best regards,

Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Chad Johnson
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Chad Johnson
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or