Disable default button click in Asp.Net

| December 8, 2010 | 0 Comments

If your Asp.Net page is having Button control on it & if user press Enter key of keyboard then default’s button (first button on form declared or if developer set attribute defaultButton of form) click event is fired. This is the default behavior of button click in Asp.Net page. However in some of the browsers the page refreshes (or reloads) but click event doesn’t gets fired. It is because of way the browser are developed by their vendors. BUT you want to disable that default button click in asp.net. How?

Solution for Disabling default button click

You can try by one of the following methods:

  • Set UseSubmitBehavior attribute of button as False
  • Set OnSubmit attribute of button as False
  • By handling onkeydown event of body. Here is the code for same:
    <head runat="server">
        <title>Testing Page</title>
        <script type="text/javascript">
    
    function DisableEnterKey()
    {
    if(event.keyCode == 13)
    {
    return false;
    }
    else
    {
    return true;
    }
    
    }
        </script>
    </head>
    <body onkeydown="return DisableEnterKey()">
    

Category: Asp.Net

Leave a Reply

%d bloggers like this: