Search code examples
jqueryasp.netpaneljquery-eventspreventdefault

How can I disable the DefaultButton of an asp:Panel at ClientSide?


I have an asp:Panel whose default button is set to Save button of the form. But I want that when the focus is on one specific anchor element in my form, the Enter key should not fire the DefaultButton of Panel. Rather it should fire the Javascript function written inside the href of the anchor, which perfectly works if I just remove the asp:Panel from the form.

I have tried many solutions, but none of them seems to work. Some of them are:-

  1. I capture the focus event of anchor in which I hook another handler for keypress event and use event.preventDefault() so that event is cancelled there only and remove that keypress event on the blur of anchor element. But this does not stop the DefaultButton of Panel from being executed.

  2. Use another asp:Panel inside the outer Panel and set its DefaultButton to the ImageButton which replaces the anchor element(because we can not set DefaultButton to anchor). - But this still fires the DefaultButton of the outside Panel.

What I need is some way to disable the DefaultButton of the outer asp:Panel at the Client Side. Is this possible?


Solution

  • Not quite sure what you are doing, but when you add a DefaultButton attribute to an asp:Panel then it adds something like this to the rendered div:

    onkeypress="javascript:return WebForm_FireDefaultButton(event, 'Button1')"
    

    You can remove that attribute using jQuery by doing something like:

    $("#Panel1").removeAttr("onkeypress");
    

    (Assuming the ClientID of your Panel is Panel1). Hope that is of some help!