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

Focus the GridTemplateColumn field

6 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cecilie Nordbø
Top achievements
Rank 1
Cecilie Nordbø asked on 21 Jul 2010, 02:17 PM
Hi.
I have a radgrid with a GridTemplateColumn containing a EditItemTemplate with a RadNumerictextBox named UserNrTb like this:

 <telerik:RadGrid ID="RadGrid1" runat="server" ......
 <Columns>
        <telerik:GridTemplateColumn DataField="UserNr" DataType="System.Int32" HeaderText="UserNr" UniqueName="UserNr" >
            <EditItemTemplate>
                <telerik:RadNumericTextBox ID="UserNrTb" runat="server" DbValue='<%# Bind("UserNr") %>'></telerik:RadNumericTextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label runat="server"  ID="UserNrLbl" Text='<%# Eval("UserNr") %>' ></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridNumericColumn DataField="Account" DataType="System.Int32" HeaderText="Account" UniqueName="column2">
        </telerik:GridNumericColumn>
......

I want to have focus on the field UserNrTb in editmode but I don't know how to iterate the GridTemplateColumn. I have read the article "Focus the text boxes in the edit control" and the description work fine for GridNumericColumn as followed:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
        
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           GridEditableItem form = (GridEditableItem)e.Item;
           RadNumericTextBox dataField = (RadNumericTextBox)form[
"column2"].Controls[0];
           dataField.Focus();
       }
   }

I tried to do the same for the field UserNrTb but it's not working. So how can i find the field from GridTemplateColumn?

Ceci


 

       

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Jul 2010, 02:43 PM
Hello Cecilie,


You need to use FindControl() method to get reference to control placed in TemplateColumn. See the modified code.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem form = (GridEditableItem)e.Item;
        RadNumericTextBox dataField = (RadNumericTextBox)form.FindControl("UserNrTb");
        dataField.Focus();
    }
}



Thanks,
Princy.
0
Cecilie Nordbø
Top achievements
Rank 1
answered on 21 Jul 2010, 03:05 PM
I have already tried that and the problem is that the dataField is null.
0
Princy
Top achievements
Rank 2
answered on 22 Jul 2010, 07:44 AM
Hello,


I am not sure about the issue since the code worked very well in my side. I hope someone from Telerik can give us a hand?


Thanks,
Princy.
0
Cecilie Nordbø
Top achievements
Rank 1
answered on 23 Jul 2010, 09:53 AM
I forgot to say that when it's in editmode i want to have focus on field column2 but when it's in insertmode I want to have focus in UserNrTb. Therefore I have used a if-test on e.Item.OwnerTableView.IsItemInserted and the dataField is null. See code block. So what do i have to test on when i need focus when insertmode or if i can use the if-test i already have, how can i set focus?

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e
{
   if (e.Item is GridEditableItem)
   {
      if (e.Item.IsInEditMode)
      {
         GridEditableItem form = (GridEditableItem) e.Item;
         RadNumericTextBox dataField = (RadNumericTextBox)            form["column2"].Controls[0];
          dataField.Focus();
       }
       else if (e.Item.OwnerTableView.IsItemInserted)
       {
         GridEditableItem form = (GridEditableItem)e.Item;
         RadNumericTextBox dataField = (RadNumericTextBox)form.FindControl("UserNrTb");
         dataField.Focus();
        }
   }
}


0
Accepted
Princy
Top achievements
Rank 2
answered on 23 Jul 2010, 11:48 AM
Hello,

Try the following code snippet to set focus on different controls in edit and insert mode.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
      if (e.Item.OwnerTableView.IsItemInserted)
       {
           //item is about to be inserted  
           if (e.Item is GridDataInsertItem)
           //if you are using EditMode as 'EditForms' then check with GridEditFormInsertItem
           {
               GridDataInsertItem form = (GridDataInsertItem)e.Item;
               RadNumericTextBox dataField = (RadNumericTextBox)form.FindControl("UserNrTb");
               dataField.Focus();
           }
       }
       if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
       //item is about to be edit
       {
           GridEditableItem form = (GridEditableItem)e.Item;
           RadNumericTextBox dataField = (RadNumericTextBox)form["column2"].Controls[0];
           dataField.Focus();
       }  
   }

Thanks,
Princy.
0
Cecilie Nordbø
Top achievements
Rank 1
answered on 23 Jul 2010, 12:18 PM
Thanks, it works.
Tags
Grid
Asked by
Cecilie Nordbø
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Cecilie Nordbø
Top achievements
Rank 1
Share this question
or