Tutorial :ASP.NET : Invoke codebehind method on Enter Key press in form


I have an asp.net form which has 3 text box and and a asp.net link button control.I want to invoke the button click event handler method when the user press enter key.I used asp.net panel and its default button property.Its working in IE.But not in other Browsers like firefox etc.. Is there any other method to do so ?


You could try the DefaultButton Property (ASP.Net 2.0 and above). The ASP.Net Form and Panel controls both have a DefaultButton Property. The default action means the enter key should trigger the button click.

<form id="Form1" defaultbutton="SubmitButton" runat="server">    <asp:panel id="panel1" defaultbutton="anotherbutton" runat="server">    </asp:panel>  </form>  

There is a known FireFox issue that may be resolved by adding: UseSubmitBehavior="False" to your submit button. This Blog describes the problem and solution in an UpdatePanel, it might work here as well.

Also check this question which has links to other possible solutions.


This seems to be a bug with FF3 (not sure), but the script that fixed is given below Keep it at the end of the page so that it overrides the WebForm_FireDefaultButton method rendered by ASP.NET.

var __defaultFired = false;

function WebForm_FireDefaultButton(event, target) { var element = event.target || event.srcElement;

if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {      var defaultButton;        if (__nonMSDOMBrowser)          defaultButton = document.getElementById(target);      else          defaultButton = document.all[target];        if (defaultButton) {          if(typeof(defaultButton.click) != "undefined")              defaultButton.click();          else              eval(unescape(defaultButton.href.replace("javascript:", "")));            event.cancelBubble = true;            if (event.stopPropagation) event.stopPropagation();          return false;      }  }  return true;  



Maybe this link helps
Capturing the enter key to cause a button click


A better option would be to use AJAX.NET and page methods to do this. Check this http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »