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 :
<asp:GridView ID="gdTest" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="gdTest_RowCommand" OnRowDataBound="gdTest_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Header" DataField="DATAFIELD" />
.
.
.
.
<asp:TemplateField HeaderText="Action">
<itemtemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit"></asp:Button>
</itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
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".