Search code examples
c#asp.netvisual-studio-2010textboxautopostback

How to stop IsPostBack from a textbox carriage return?


I want to stop the IsPostBack fired from the enter key pressed at a TextBox. The textBox can not be multiline.

I'm trying this:

<asp:TextBox ID="kemetTextBox" runat="server" Width="215px">      
                </asp:TextBox>

                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#kemetTextBox").keyup(function (e) {
                            if (e.keyCode == 13) {
                                Search();
                                return false;
                            }
                        });
                    });
                </script>

But it stills reloading the page.

Data: Visual Studio 2010, Asp.net, C# as codebehind.

Thanks


Solution

  • Just set the AutoPostBack="False" like this:

    <asp:TextBox ID="kemetTextBox" runat="server" Width="215px" AutoPostBack="False">
    

    TextBox.AutoPostBack Property

    Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.


    Adding to this you can do this too:

    <asp:TextBox ID="kemetTextBox" runat="server" Width="215px" onkeydown="return (event.keyCode!=13);">
    

    Source: Disable Enter key in TextBox to avoid postback in ASP.Net