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

How to Update when ENTER Key is Pressed

5 Answers 566 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 16 Nov 2011, 08:51 PM

My webform contains a RadGrid that has EditMode set to InPlace. After the webform is rendered, a user can click the edit button on any row within the grid. When edit is clicked, I have a GridTextBoxColumnEditor box that is displayed, which allows the user to edit the cell value. After the value has been changed, the end-user can click the Update button. The code-behind executes radGrid_UpdateCommand, which successfully saves the user’s changes; that all works fine.

In addition to the Update button, however, my users want to be able to simply press the Enter key, and have their changes saved, just as if they had clicked the Update button. How do I get radGrid_UpdateCommand to execute when the Enter key is pressed within the GridTextBoxColumnEditor box?

I was hoping that I could accomplish this objective by simply setting the TextBoxMode property of the GridTextBoxColumnEditor to SingleLine, but there is obviously more to it than that. So how can I get the radGrid_UpdateCommand to execute when a user presses the Enter key in the GridTextBoxColumnEditor box?

Thanks

5 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 16 Nov 2011, 09:18 PM
steven - it is a little more complicated than it looks
here is a snippet of code decorating a RADnumeric text box which updates when the Enter key is entered
Protected Sub rgAddItems_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgAddItems.ItemCreated
    Dim geItem As GridEditableItem = Nothing
    Dim tcPartNumber, tcQty As TableCell
    Dim txtPartNumber As TextBox
    Dim rntbQty As RadNumericTextBox
    Dim btnUpdate As LinkButton
    Dim sb As StringBuilder = Nothing
 
    If e.Item.IsInEditMode Then
    Else
        Exit Sub
    End If
    If TypeOf e.Item Is GridEditableItem Then
    Else
        Exit Sub
    End If
    geItem = DirectCast(e.Item, GridEditableItem)
    tcPartNumber = geItem.Cells(0)
    txtPartNumber = DirectCast(tcPartNumber.FindControl("txtPartNumber"), TextBox)
    btnUpdate = DirectCast(geItem.FindControl("btnUpdate"), LinkButton)
    tcQty = geItem.Cells(1)
    rntbQty = tcQty.FindControl("rntbQty")
    If rntbQty Is Nothing Then
    Else
        sb = New StringBuilder("if(event.which || event.keyCode)")
        sb.Append("{if ((event.which == 13) || (event.keyCode == 13)) ")
        sb.Append("{document.getElementById('")
        sb.Append(btnUpdate.ClientID)
        sb.Append("').click();return false;}} ")
        sb.Append("else {alert(keyCode);return true};")
        rntbQty.Attributes.Add("onkeydown", sb.ToString)
    End If
    If TypeOf e.Item Is IGridInsertItem Then
        If txtPartNumber Is Nothing Then
        Else
            txtPartNumber.Focus()
        End If
    Else
        txtPartNumber.ReadOnly = True
        rntbQty.Focus()
    End If
End Sub
and the grid
<telerik:RadGrid ID="rgAddItems" AutoGenerateColumns="False" GridLines="Both" ShowFooter="true" runat="server">
    <MasterTableView DataKeyNames="PartNumberID" CommandItemDisplay="Bottom" NoDetailRecordsText="begin entering data" runat="server">
    <ItemStyle CssClass="grid_item_style" />
    <AlternatingItemStyle CssClass="grid_alternate_item_style" />
    <Columns>
        <telerik:GridBoundColumn UniqueName="PartNumberID" DataField="PartNumberID" Readonly="True" Visible="False"/>
        <telerik:GridTemplateColumn HeaderText="ItemID" Visible="False" >
            <ItemTemplate>
                <asp:Label ID="lblItemID" Text='<%# Bind("ItemID") %>' runat="server" />
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn UniqueName="PartNumber" DataField="PartNumber" HeaderText="Part Number" ReadOnly="True" />
        <telerik:GridTemplateColumn HeaderText="Item Description">
            <ItemTemplate>
                <asp:Label ID="lblItemDescription" Text='<%# Bind("Description") %>' runat="server" />
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn UniqueName="Qty" DataField="Qty" HeaderText="Quantity" />
        <telerik:GridEditCommandColumn EditText="Edit" />                    
        <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn"/>    
    </Columns>
    <EditFormSettings EditFormType="Template">
        <EditColumn UniqueName="ecColumn" />
        <FormTemplate>
            <table border="0" cellpadding="2" cellspacing="2">
            <tr>
            <td>
                <asp:HiddenField ID="hdnPartNumberID" Value='<%# Bind("PartNumberID") %>' runat="server" />
            </td>
            <td>
                <asp:HiddenField ID="hdnItemID" Value='<%# Bind("ItemID") %>' runat="server" />
            </td>
            </tr>
            <tr>
            <td class="body_text_black_BOLD">Part Number</td>
            <td>
                <asp:TextBox ID="txtPartNumber" Text='<%# Bind("PartNumber") %>' CssClass="body_text_black" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfdPartNumber" ControlToValidate="txtPartNumber" ErrorMessage="Part Number Required" runat="server" />               
            </td>
            </tr>
            <tr>
            <td class="body_text_black_BOLD">Quantity</td>
            <td>
                <telerik:RadNumericTextBox ID="rntbQty" Text='<%# Bind("Qty") %>' MaxLength="5" MaxValue="9999" MinValue="1" CssClass="body_text_black" runat="server" >
                    <NumberFormat DecimalDigits="0" />
                </telerik:RadNumericTextBox>
                <asp:RequiredFieldValidator ID="rfQty" ControlToValidate="rntbQty" ErrorMessage="Quantity required" runat="server">
                </asp:RequiredFieldValidator>
            </td>
            </tr>
            <tr>
            <td>
                <asp:LinkButton ID="btnUpdate" Text='<%# IIF(TryCast(Container,GridItem).OwnerTableView.IsItemInserted,"Insert","Update") %>'  CommandName='<%# IIF(TryCast(Container,GridItem).OwnerTableView.IsItemInserted,"PerformInsert","Update") %>' runat="server" />
            </td>
            <td>
                <asp:LinkButton ID="btnCancel" Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" />
            </td>
            </tr>
            </table>
        </FormTemplate>
    </EditFormSettings>
    </MasterTableView>
    <ClientSettings>
        <ClientEvents OnRowDblClick="RowDblClick" />
    </ClientSettings>
    </telerik:RadGrid>
hope that helps
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 06:58 AM
0
Brian
Top achievements
Rank 1
answered on 17 Nov 2011, 08:50 PM
Jayesh,

Thank you so much for your reply. It was exactly what I needed. And the methodology was not complex at all. Simply apply the following RadGrid settings and you are done:

<ClientSettings AllowKeyboardNavigation="true">
    <KeyboardNavigationSettings AllowSubmitOnEnter="true" />
</ClientSettings>

Thank you,
Steven
0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 29 Nov 2011, 12:50 PM
I have found, if I enable the Enter key to be used for Insert/update, that when the user hits enter and a record is updated and saved, the grid puts the first row into editmode. This occurs even if I include MyGrid.EditIndexes.Clear at the end of my update code. Any idea what is going on?
0
Elliott
Top achievements
Rank 2
answered on 29 Nov 2011, 03:05 PM
that was a requirement of the application I developed - but the code to accomplish it was not in the snippet I posted
try something like this after the update is completed successfully:
geItem.Edit = false;
rgEditOrder.MasterTableView.Rebind();
where geItem is the e.Item cast as whatever type it is and rgEditOrder is the grid
Tags
Grid
Asked by
Brian
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Jayesh Goyani
Top achievements
Rank 2
Brian
Top achievements
Rank 1
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
Share this question
or