Results 1 to 2 of 2

Thread: KeyPress 'Enter' fires Button_Clicked event

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    KeyPress 'Enter' fires Button_Clicked event

    When I hit Enter anywhere on my page, one of my Button_click events fire. Where can I stop that from happening?

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: KeyPress 'Enter' fires Button_Clicked event

    Internet Explorer assigns the first html input field with type submit (an Asp.Net button for example) to receive the Enter keypress on a webpage when in a field.

    Put this into your declerations section of your page:
    Code:
    protected System.Web.UI.HtmlControls.HtmlForm Form1;
    
    or for VB:
    Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

    Put this into your Page_Load handler:

    c#
    Code:
    if (!(Page.IsPostBack))
    			{
    				Page.RegisterStartupScript("blockEnter",
    					"<script type=\"text/javascript\">function inspectEvent(evt){if (evt.keyCode == 13)return false;}</script>");
    				Form1.Attributes.Add("onkeypress","return inspectEvent(event)");
    			}
    Vb
    VB Code:
    1. If Not Page.IsPostBack
    2.                 Page.RegisterStartupScript("blockEnter", _
    3.                  "<script type=""text/javascript"">function inspectEvent(evt){if (evt.keyCode == 13)return false;}</script>")
    4.                 Form1.Attributes.Add("onkeypress","return inspectEvent(event)")
    5.             End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width