Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3218902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:34:56+00:00 2026-05-17T15:34:56+00:00

In ASP.NET, I am preventing default postback action when user presses enter while textbox

  • 0

In ASP.NET, I am preventing default postback action when user presses enter while textbox control has focus. See the following piece of code

      $("#TextBox1").keydown(function(e)
        {
            if(e.keyCode == 13)
            {
                //alert("Hello");
                return false;
            }
        });

This code works fine. But if I uncomment the alert function call then page post does not stop.

My question is why alert call is creating a problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:34:57+00:00Added an answer on May 17, 2026 at 3:34 pm

    I missed that you were using keydown. To prevent form submission, I’d use a submit handler instead, in this case working with your keydown handler and probably a blur handler as well (to reset the flag):

    var cancelFlag = false;
    
    $('#TextBox1')
        .keydown(function(e) {
            cancelFlag = e.keyCode == 13;
        })
        .blur(function() {
            cancelFlag = false;
        });
    
    $('#theform').submit(function() {
        if (cancelFlag) {
            alert("Hello!");
        }
        return cancelFlag;
    });
    

    (Or you can call e.preventDefault(); instead of returning false, either works.) You’ll need to do testing to ensure the flag gets cleared in every situation you want it to be cleared in.


    Alternately, make the alert asynchronous:

    $("#TextBox1").keydown(function(e)
    {
        if(e.keyCode == 13)
        {
            setTimeout(function()
            {
                alert("Hello");
            }, 10);
            return false;
        }
    });
    

    That lets the event stack unwind before putting up the alert. But I’m not at all sure you can reliably (cross-browser and OS) prevent a form submission by cancelling the default action of a keydown. You said it was working, and if it works in your target browsers, great, but…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ASP.Net 3.5 I want to create a user control that contains the ListView and
ASP.Net MVC Controllers have several methods of forwarding control to another controller or action.
ASP.NET: How do you debug dynamically loaded user controls using LoadControl(string path);
I need a generic method for preventing XSS attacks in ASP.NET. The approach I
I want to implement the user authentication and login page in ASP.NET, I want
(ASP.NET Web Application) I'd like to create a page which allows the user to
What is the best way to sanitize the user input in asp.net mvc without
I am having an asp.net sever control as below <asp:HyperLink Visible='<%# (GetAnswer(Eval(AnsQID))) != 1
Class file Conflicts in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ is preventing me from building the solution.
ASP.NET MVC 2 project. I want to create a master page that has three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.