HDI: Disable postback on html input box

10,108

Solution 1

Special thanks to: http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

<input type="text" id="addressInput" onkeydown="if (window.event.keyCode == 13) 
{
    event.returnValue=false; 
    event.cancel = true;
    searchLocations();
}" />

Solution 2

Just return false form JS function, add return false; at the end of function.

or <input type="text"
id="addressInput"
onkeydown ="return (event.keyCode!=13)" autopostback="false"/>

UPDATE: How about this..?

<asp:TextBox ID="TextBox1" runat="server" 
 onkeydown="return CallEnterKeyFunc(event.keyCode);"> 
</asp:TextBox>
    <script type="text/javascript">
    function CallEnterKeyFunc(key) {
        if (key == 13) { //for FF, i think you have to use evt.which
                //enter key press
                seachLocations();
                return false;

            }
            else
                return true;

    }

    function seachLocations() {
       //your code
        alert("hello");
    }
</script>
Share:
10,108
Chris Hayes
Author by

Chris Hayes

Updated on November 20, 2022

Comments

  • Chris Hayes
    Chris Hayes over 1 year

    I have an input box that I don't want postback to occur on someone striking the enter key

    I want the javascript event to take place instead.

    <input 
    type="text" 
    id="addressInput" 
    onkeydown="inputenter()" 
    autopostback="false"/>
    
        function inputenter() {
            if (event.keyCode == 13) {
                seachLocations();
                return false;
            }
            else {
                return false;
            }
        }
    
  • Chris Hayes
    Chris Hayes over 13 years
    Thank you for your help and diligence. It's still not working. if 'false' gets returned ONLY it works fine. When it tries to run another function/method it fails and does the postback. :o(