Click here to Skip to main content
14,094,159 members
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 29 Jul 2009
Licenced CPOL

Handling the Enter key pressed in a GridView's row edit mode

, 29 Jul 2009
Rate this:
Please Sign up or sign in to vote.
Fire a row command and updating event when pressing the Enter key during a row edit.


This article demonstrates how to tie the Enter key pressed during a row edit to the GridView's Update event, passing the CommandName, CommandArgument, and RowIndex. During the edit of a row, the Enter key will be captured, and then a postback call will occur, which will fire the GridView's events OnRowCommand and OnRowUpdating. Once this happens, we can do our update to the database within the routines gvChild_RowCommand or gvChild_RowUpdating.


In this ASPX page, the GridView is defined with the various events needed:

<asp:GridView ID="gvChild" 

    runat="server" AutoGenerateColumns="False" 








        <asp:ImageButton ID="btnEdit" runat="server" ToolTip="Edit" 

        CommandName="Edit" CommandArgument="<%# Container.DataItemIndex %>" 

        ImageUrl="~/Images/EditDocument.gif" /> 
        <asp:ImageButton ID="btnUpdate" runat="server" ToolTip="Update" 

        CommandName="Update" CommandArgument="<%# Container.DataItemIndex %>" 

        ImageUrl="~/Images/Save.gif" /> 
        <asp:ImageButton ID="btnCancel" runat="server" ToolTip="Cancel" 

        CommandName="Cancel" CommandArgument="<%# Container.DataItemIndex %>" 

        ImageUrl="~/Images/close.png" /> 


In the code-behind page, we will examine the different events: OnRowDataBound, OnRowCommand, and OnRowUpdating. In gvChild_RowDataBound, for each Row Edit (DataControlRowState.Edit) in the GridView, we add an onkeypress event for the row. On this onkeypress event, we add the JavaScript logic to be executed. First, we look if event.keyCode == 13, which is looking for the Enter key. If the Enter key is pressed, we call __doPostBack with the following parameters: The first parameter is gvChild.UniqueID which is the unique ID of the GridView. The next parameter contains both the CommandName (Update$) and the CommandArgument (e.Row.RowIndex.ToString()). So, when the Enter key is pressed, gvChild_RowCommand will get fired. It will go to the else if statement e.CommandName == "Update" and then it will get the row information based on e.CommandArgument which contains the row index to be updated. I chose to do the update of the database in gvChild_RowCommand, but you could also handle the update in gvChild_RowUpdating.

protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
    // if enter key is pressed (keycode==13) call __doPostBack on grid and with 
    // 1st param = gvChild.UniqueID (Gridviews UniqueID)
    // 2nd param = CommandName=Update$  +  CommandArgument=RowIndex
    if ((e.Row.RowState == DataControlRowState.Edit) || 
       (e.Row.RowState == (DataControlRowState.Edit|DataControlRowState.Alternate)))
        e.Row.Cells[0].Width = Unit.Pixel(35);
        e.Row.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) { 
            __doPostBack('" + gvChild.UniqueID + "', 'Update$" + 
           e.Row.RowIndex.ToString() + "'); return false; }");

protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName == "copy")
    else if (e.CommandName == "remove")
    else if (e.CommandName == "up")
    else if (e.CommandName == "down")
    else if (e.CommandName == "Update")
         GridViewRow row = gvChild.Rows[Convert.ToInt32(e.CommandArgument)];
         ProductArmType pat = GetByProductArmTypeSkey(productArmTypeSkey);
         pat.CustomProductName = 
         gvChild.EditIndex = -1;
    gvChild.EditIndex = -1;
    BindChild();  //rebind gvChild gridview after update or any other command

protected void gvChild_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //Could handle update here, but chose to do it in gvChild_RowCommand


This small coding example allows the user to trap the Enter key during a row edit. When typing changes in the fields during row edit and pressing Enter, a postback is done which executes the GridView's OnRowCommand and OnRowUpdating events. In these events, we do the update to the database based on the CommandName passed in (Update) and the CommandArgument passed in (in our case, RowIndex). From there, we can get the current GridView's row based on the index and read out the fields of the row and then update the database.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul Daulisa
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionadd row Pin
Member 1327070722-Jun-17 21:10
memberMember 1327070722-Jun-17 21:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190518.1 | Last Updated 29 Jul 2009
Article Copyright 2009 by Paul Daulisa
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid