Issue with OnRowCommand event of GridView while using reserve words in CommandName property

I came across a very interesting error while using buttons/link buttons in the GridView for editing the GridView rows data. The exact situation I faced is like this – I have a GridView with a column which contains an edit button. The button is associated with the CommandName property as “Edit”. The GridView is associated with the OnRowCommand event. Then in the code behind I have handled the OnRowCommand event. In the event handler function I have checked for the CommandName property and write necessary code for each of the actions. The HTML code for the GridView is as follow :

         
    
        
        .
        .
        .
        .
        
            
                        
           
        
    

The event handling code for the RowCommand event is as follow:
 
protected void gdTest_RowCommand(object source, GridViewCommandEventArgs e)
{
     if (e.CommandName.Trim() == "Edit")
     {
          //Code for the editing
     }
}

Till now everything looks fine but the problem arises when you run this application. When you click on edit button of the GridView a Javascript error occurs :

“Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: The GridView
‘gdTest’ fired event RowEditing which wasn’t handled.”

At first look you may think that you have not handled some events fired by the GridView. But this is not the truth. The mystery behind this error is the property “CommandName”.

The simplest solutions for this error is : Just change the “CommandName” property of the “Edit” button from “Edit” to “EditRow”(or something else which is relevent to you but make sure it is not “Edit”). Now surprisingly the code works fine.

After scratching my head for sometime I found that the words like “Edit” are reserved to invoke some built in functionality of the GridView. So when we use these special key words as the CommandName for the buttons in the GridView, they automatically invoke the built in functionality of the GridView when we click on the button. As our code does not handle those events hence they throw an exception. So when we change the CommandName property of the buttons to some other text then it will work fine.
Some other GridViews reserved key words and their default associated events are as follow : “Cancel” – Raises the RowCancelingEdit event. “Delete”  – Raises the RowDeleting and RowDeleted events. “Edit” – Raises the RowEditing events. “Page” – Raises the PageIndexChanging and PageIndexChanged events. “Select” – Raises the SelectedIndexChanging and SelectedIndexChanged events. “Sort” – Raises the Sorting and Sorted events.

“Update” – Raises the RowUpdating and RowUpdated events.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!