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

  • SEARCH
  • Home
  • 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 3459446
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:06:25+00:00 2026-05-18T10:06:25+00:00

I have the following problem in ASP.NET: there is a form that contains a

  • 0

I have the following problem in ASP.NET: there is a form that contains a textbox and a button next to it that is supposed to be pressed by the user after filling the box (sample on http://www.burnthespam.info, click "Pick your one", or when using ReCaptcha in a popup). Often, instead, users press ENTER key to submit the form.

This doesn’t cause the click event on the button to be triggered and possibly cause an unexpected behaviour. In burnthespam, I "tried" to solve by checking if there is data inside the text box (but now if you do something different than pressing ENTER it’s like you pressed it) to workaround it.

Do you know if there is another way to handle the form submission with ENTER key, or a Javascript snippet that when you press ENTER presses the button I like?

EDIT

I want to handle the ENTER key event on the server-side, ie. I already have

protected void button_Click(object sender, EventArgs e)
    {
        Response.Redirect(...);
    }

I want that method to be called not only when I submit the form using the button (click or space with it highlighted) but also when a user presses ENTER when focusing the text-box

EDIT 2

Do you know if it’s possible to programmatically click a button in Javascript? Maybe it’s not possible to prevent phishing/spamming (see Facebook and "share to friends" for example) but I still would like to ask…

  • 1 1 Answer
  • 2 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-18T10:06:26+00:00Added an answer on May 18, 2026 at 10:06 am

    There some ways you can set a default button using the form object DefaultButton property or the DefaultButton property of a panel, but I have found them to be unreliable in the past in various browsers so usually I rely on javascript.

    The only downside to this code is you have to turn off event validation with a page directive, but it should fire off click events, and trigger validators and all that.

    Here is an example the code that we use. Normally I would put the register function in a utility class, but for this example it is in the page code.
    Try this out.

    <%@ Page Language="C#" EnableEventValidation="false" %>
    
        <script runat="server">
    
            protected void cmdSubmit1_Click(object sender, EventArgs e)
            {
                litValue.Text = "Value 1 - You entered: " + txtValue1.Text;
            }
            protected void cmdSubmit2_Click(object sender, EventArgs e)
            {
                litValue.Text = "Value 2 - You entered: " + txtValue2.Text;
            }
    
            /// <summary>
            /// This function registers what button is clicked based on whatever control currently has focus
            /// so for example if the user password field has focus then you can cause the enter button to click
            /// if the enter key is pressed This works with ie and firefox as far as I know
            /// </summary>
            /// <param name="ControlWithFocus"></param>
            /// <param name="ControlToClick"></param>
            private void RegisterDefaultButton(System.Web.UI.Control ControlWithFocus, System.Web.UI.Control ControlToClick)
            {
    
                PostBackOptions p = new PostBackOptions(ControlToClick);
                p.PerformValidation = true;
                if (ControlToClick is Button)
                {
                    p.ValidationGroup = ((Button)ControlToClick).ValidationGroup;
                }
                else if (ControlToClick is ImageButton)
                {
                    p.ValidationGroup = ((ImageButton)ControlToClick).ValidationGroup;
                }
                else if (ControlToClick is LinkButton)
                {
                    p.ValidationGroup = ((LinkButton)ControlToClick).ValidationGroup;
                }
    
                p.RequiresJavaScriptProtocol = false;
    
                AttributeCollection a = null;
                if (ControlWithFocus is HtmlControl)
                {
                    a = ((System.Web.UI.HtmlControls.HtmlControl)ControlWithFocus).Attributes;
                }
                else if (ControlWithFocus is WebControl)
                {
                    a = ((System.Web.UI.WebControls.WebControl)ControlWithFocus).Attributes;
                }
    
                if (a != null)
                {
                    a["onKeyDown"] = string.Format("if (event.keyCode == 13) {{{0}}}"
                          , ControlToClick.Page.ClientScript.GetPostBackEventReference(p));
                }
            }
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
                RegisterDefaultButton(txtValue1, cmdSubmit1);
                RegisterDefaultButton(txtValue2, cmdSubmit2);
    
            }
    
        </script>
    
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
        </head>
        <body>
            <form id="form1" runat="server">
                Enter Value 1: <asp:TextBox ID="txtValue1" runat="server"></asp:TextBox>
                <br />
                Enter Value 2: <asp:TextBox ID="txtValue2" runat="server"></asp:TextBox>
                <br />
                <asp:Literal ID="litValue" runat="server"></asp:Literal>
                <asp:LinkButton ID="cmdSubmit1" runat="server" Visible="false" OnClick="cmdSubmit1_Click">Hidden Button 1</asp:LinkButton>
                <input id="cmdSubmit2" runat="server" visible="false" type="button" value="Hidden Button 2" onserverclick="cmdSubmit2_Click" />
            </form>
        </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextBox in an Asp.net form. there is a simple javascript witch
I have following problem: I have to make a ASP.NET Webapplication with a two-row
My situation : I have a problem with an ASP.NET button, which does not
I have the following asp.net mvc2 template: <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<ncontinuity2.core.dto.TrainingLookUpContainer> %> <%= Html.EditorFor(x
I have the following problem: Our system has products that when released only are
I have a Database Entity Model within my ASP.Net Project There are two foreignkeyconstructs,
I have an ASP.NET webpage that is rendering many (~3000) <input type=text> textboxes on
I have a String to Date conversion problem using SQL Bulkcopy in asp.net 3.5
I have a multi-step data entry form in my ASP.NET MVC application. When the
I have a couple of standard ASP.NET web methods that I'm calling from javascript

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.