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);

Recommended Answers

All 2 Replies

I don't think you can get out of the box (Browser window). I believe that for securely reasons you can't track any of the keyboard clicks through a browser.

Actually, I can. When I debug in Firebug, I can see that the code is executed. E.g. I can show alert(), if I place it instead of button click. Moreover, it works in IE. I have a problem only in Firefox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.