Dear experts,
I have a GridView with an invisible CancelEdit button. I want to use this button to get out of Edit mode. When I make this button visible for testing purposes and press it with the mouse, it works fine, getting me out of Edit mode. But I need to get out of Edit mode on pressing Esc key. I use a javascript function for this. But though the CancelEdit button is found, .click() doesn't do anything, and I still in Edit mode. Could you help me on this, please? Here is my code:
function keyPressHandler(e) {
var kC = (window.event) ? // MSIE or Firefox?
event.keyCode : e.keyCode;
var Esc = (window.event) ?
27 : e.DOM_VK_ESCAPE; // MSIE or Firefox
if (kC == Esc) {
try {
document.getElementById('<%=((FrontListGridView.EditIndex > -1)?(FrontListGridView.Rows[FrontListGridView.EditIndex].Cells[1].FindControl("CancelEdit").ClientID):string.Empty) %>').click();
}
catch (e) {
alert(e.message);
}
}
}
...
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="CancelEdit" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
...
In Page_Load():
Page.ClientScript.RegisterStartupScript(typeof(Page), "escscript", "document.onkeypress=keyPressHandler;", true);