28 February 2012

Click event when Enter button is Pressed in asp.net

Click event when Enter button is Pressed in asp.net

Javascript
function clickButton(e, buttonid){
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt){
          if (evt.keyCode == 13){
                bt.click();
                return false;
          }
      }
}




//code behind TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");
 

The code behind generates the following code:

<    asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server" onkeypress="return clickButton(event,'Button1')"/   >




This causes web control Button1 to be clicked when the enter key is hit inside TextBox1.


No comments:

Post a Comment

Comments Welcome