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

Set textbox focus on Insertmode

5 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 12 Jul 2011, 10:11 PM
Hi all,

I have a CarNumber column

<telerik:GridTemplateColumn UniqueName="car_number" SortExpression="car_number" HeaderText="Car Number" DataField="car_number" CurrentFilterFunction="Contains">
    <ItemTemplate>
       <asp:Label ID="car_number" runat="server" Text='<%#Eval("car_number")%>' />
    </ItemTemplate>
    <EditItemTemplate>
         <telerik:RadTextBox ID="ed_car_number" runat="server" Text='<%# Bind("car_number")%>' Width="100px" Font-Size="Small" Font-Names="Arial">
         </telerik:RadTextBox>
     </EditItemTemplate>
</telerik:GridTemplateColumn>

And would like set focus in the InsertMode as http://www.telerik.com/help/aspnet-ajax/grid-set-focus-on-textboxes-in-edit-control.html

If (TypeOf e.Item Is GridDataInsertItem And e.Item.IsInEditMode) Then
            Dim MyUserControl As UserControl = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) '
            myRadGrid.Controls.Add(New LiteralControl(String.Format("<script type='text/javascript'>document.getElementById('{0}').focus();document.getElementById('{0}').select();</script>", MyUserControl.FindControl("ed_car_number").ClientID)))
            e.Item.Selected = True
End If

However, it doesn't work.

Please help.

Thanks.

Andy.


 

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 13 Jul 2011, 07:14 AM

Hello Andy,

Try the folowing code snippet to achieve the scenario.
C#:

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted)
        {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            RadTextBox txt = (RadTextBox)item.FindControl("ed_car_number");
            txt.Focus();
        }
    }

Thanks,
Shinu.

0
Andy
Top achievements
Rank 1
answered on 13 Jul 2011, 03:20 PM
Hi Shinu,

I made a debug. It never runs into the IF condition.

Thanks.

Andy.
0
Andy
Top achievements
Rank 1
answered on 13 Jul 2011, 03:51 PM
Hi,

I tried another code

If (TypeOf e.Item Is GridDataInsertItem And e.Item.IsInEditMode) Then
            Dim txt As RadTextBox = DirectCast(e.Item.FindControl("ed_car_number"), RadTextBox)
            txt.Focus()
End If

It doesn't work either. An javascript error happens

Thanks.

Andy.
0
Shinu
Top achievements
Rank 2
answered on 14 Jul 2011, 06:07 AM
Hello Andy,

I am not  quite sure about the EditMode you are trying with,the above mentioned code was for Editmode in Editforms/PopUp and while for Editmode in InPlace.
Try the following code snippet.
C#:
if (e.Item is GridDataInsertItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted)
        {
            GridDataInsertItem InsertItem = (GridDataInsertItem)e.Item;
        }
Thanks,
Shinu.

0
Andy
Top achievements
Rank 1
answered on 14 Jul 2011, 03:46 PM
Hi Shinu,

Thanks for your relying.
I found the solution. The "focus" function doesn't work with the RadTextBox in the Insert Mode because of RadAjax problem
Here is the code to solve it:

Protected Sub dgList_ItemCreated(ByVal sender As System.Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles dgList.ItemCreated
 
 If (TypeOf e.Item Is GridDataInsertItem And e.Item.IsInEditMode) Then
            Dim txtCarNumberFocus As RadTextBox = e.Item.FindControl("ed_car_number")
            RadAjaxManager1.FocusControl(txtCarNumberFocus.ClientID + "_text")
            e.Item.Selected = True
  End If
 
End Sub
Note: This code just works with RadTextbox, not asp:Textbox

Thanks.

Andy.
Tags
Grid
Asked by
Andy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Andy
Top achievements
Rank 1
Share this question
or