Page view counter

ASP.NET 2.0 - Enter Key - Default Submit Button

Rate It (3)

Last post 05-06-2009 2:38 PM by Laplain. 76 replies.

Sort Posts:

  • ASP.NET 2.0 - Enter Key - Default Submit Button

    04-27-2006, 11:31 AM
    • Loading...
    • ranganh
    • Joined on 02-11-2004, 6:35 PM
    • India
    • Posts 2,428
    • Points 12,063

    Hi,

    One of the most annoying things in developing web pages is handling the "Enter key" for form submission. Enter key has been the favourite way users like to submit forms. Though we provide Buttons to click on, the easiest and intuitive way is that, I can enter some text, make some changes and then hit "Enter" to accomplish my submission.

    "Enter" Key is handled in a little tricky way by uplevel browsers like Internet Explorer, when it comes to ASP.NET.

    • If there is a single Textbox and single button, then it becomes straight forward, the button is submitted. However, the event code doesnt get executed, though the page postsback.

    • If there are two or more, buttons, then it takes up the first button as the default button. However, it still doesnt execute the event handler but just refreshes the page.

    You can supress the Enter key event using Javascript. But this would result in other undesirable effects like, any Enter key in the form i.e. within Text Area or basically where large text is entered, would be disabled.

    The earlier work around was to associate a javascript function to each Button to verify the that the relevant button is submitted upon Enter key.

    ASP.NET 2.0 introduces a wonderful work around for this. By simply specifying the "defaultbutton" property to the ID of the <asp:Button>, whose event you want to fire, your job is done.

    The defaultbutton property can be specified at the Form level in the form tag as well as at panel level in the <asp:panel> definition tag. The form level setting is overridden when specified at the panel level, for those controls that are inside the panel.

    Also, the Event Handler for the specified button, fires thereby simulating a true submit button functionality.

    The following sample code contains a form and 4 panels with each of them containing different buttons. It can be noticed that for each panel, there is a default button specified which would trigger the corresponding button's event handler when "Enter" Key is pressed upon a text changed event.



    <form id="form1" runat="server" defaultbutton="btn1">

    <div>

    <asp:TextBox ID="txt" runat="server"></asp:TextBox>

    <asp:Button ID="Button5" runat="server" Text="Cancel" OnClick="Button5_Click" />

    <asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />

    <asp:Panel ID="pnl1" runat="server" defaultbutton="Button1">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />

    </asp:Panel>

    <asp:Panel ID="Panel1" runat="server" defaultbutton="Button2">

    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

    <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />

    </asp:Panel>


    <asp:Panel ID="Panel2" runat="server" defaultbutton="Button3">


    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>

    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>

    <asp:Button ID="Button3" runat="server" Text="Button3" OnClick="Button3_Click" />

    </asp:Panel>

    <asp:Panel ID="Panel3" runat="server" defaultbutton="Button4">

    <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>

    <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>

    <asp:Button ID="Button4" runat="server" Text="Button4" OnClick="Button4_Click" />

    </asp:Panel>

    </div>

    </form>

    The corresponding, sample events for the button clicks are


    protected void Button1_Click(object sender, EventArgs e)
    {
    Response.Write(Button1.Text);
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
    Response.Write(Button2.Text);
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
    Response.Write(Button3.Text);
    }


    protected void Button4_Click(object sender, EventArgs e)
    {
    Response.Write(Button4.Text);
    }


    protected void btn1_Click(object sender, EventArgs e)
    {
    Response.Write(btn1.Text);
    }


    protected void Button5_Click(object sender, EventArgs e)
    {
    Response.Write(Button5.Text);
    }

    Once we execute the above functionality, we can notice, the corresponding Buttons' text are displayed when the Enter key is pressed from within a panel and at the form level, it fires the btn1 Button's event.

    Thanks.

    regards,
    Harish

    http://geekswithblogs.net/ranganh
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    06-10-2006, 10:18 AM
    • Loading...
    • shuaiqy
    • Joined on 06-09-2006, 8:14 AM
    • Posts 12
    • Points 60
    how can use defaultbutton in asp:Content ??
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    06-14-2006, 4:30 PM
    • Loading...
    • Rohit Gupta
    • Joined on 11-02-2005, 4:36 PM
    • CA
    • Posts 86
    • Points 318

    You can also define the defaultbutton property in a panel as well ...

    RG

  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    06-26-2006, 3:43 AM
    • Loading...
    • hondav
    • Joined on 06-12-2006, 11:51 AM
    • Posts 2
    • Points 10
    I can hardly see anything 'wonderful' about this. It is a fudge. There is a BUG, to use Microsoft's own words and instead of fixing it they give you the option of deciding which way the bug is going to work. Why should there be ANY action on pressing the ENTER key outside the context of the control (textbox) without the programmer specifically including it? I do not want any action on Enter. Too many people, including myself, still use this key at the end of every entry in a textbox when, apart from trapping it for validation purposes, it should merely send the user to the next field. Is this why so many data entry forms I use on the web include only one item on each of a multitude of pages?
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-04-2006, 11:57 PM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569

    Indeed, there's no need to do all this work.

    Check this topic for a clean approach: http://forums.asp.net/thread/1361423.aspx

    HTH. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-07-2006, 5:03 AM
    • Loading...
    • dabas
    • Joined on 07-27-2006, 6:30 PM
    • Posts 4
    • Points 20

    hi ranghan

    Can you tell me how can i do that the same in version 1.

    if u know then plz send me the code .

     

     

    thanx in advance

  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-07-2006, 10:01 AM
    • Loading...
    • ranganh
    • Joined on 02-11-2004, 6:35 PM
    • India
    • Posts 2,428
    • Points 12,063

    >> Indeed, there's no need to do all this work.

    The main aim of the defaultButton is to ensure Enter Key Submissions.  Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.

    The defaultButton really provides a way to handle Enter Keys for respective portions of the page when the focus is there.

    Hope this clarifies.

    Thanks.

     

     

     

    regards,
    Harish

    http://geekswithblogs.net/ranganh
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-07-2006, 10:36 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569
    ranganh:

    > The main aim of the defaultButton is to ensure Enter Key Submissions.  Typically people are very much used to Enter Key submission and if we disable it might be annoying certain times.

    Sorry, but i don't think so. A form submitted with an enter key is on the contrary a problem you encounter any time you have a *single* textbox in a form, and it is a legacy behaviour you usually have to... disable. Now the Fwk has a defaultButton property, but that outputs some javascript to handle key events and act accordingly, so this is something *more* than simply dealing with this legacy behaviour with enter keys. That is, i would at least combine them both: first, disable legacy behaviour, then eventually add support for enter key submission...

    As [hondav] is telling:

    > There is a BUG, to use Microsoft's own words and instead of fixing it they give you the option of deciding which way the bug is going to work.

    So, great to have a defaultButton and great you have posted a sample usage. Yet i believe this should be unrelated to the legacy behaviour we are talking about.

    My humble opinion. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-14-2006, 2:29 PM
    • Loading...
    • FMastro
    • Joined on 01-22-2004, 9:16 PM
    • Tampa, FL
    • Posts 158
    • Points 620

    shuaiqy:
    how can use defaultbutton in asp:Content ??

    I have this same question. Even worse is I have an asp:login which iselt creates the button. So how can I set the ASP.Net 2.0 Login window to fire the button click on Enter? And it's in a asp:content

    ...Mastro...
    FredMastro.Com
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-21-2006, 6:10 PM
    • Loading...
    • eappell
    • Joined on 07-13-2005, 3:19 PM
    • Sacramento, CA
    • Posts 221
    • Points 468
    I have a page with two forms, one with just one textbox, and the other with many textboxes, dropdownlists, checkboxes, etc.  This is a search page, with the results showing up in popups.  The basic requirement is that if the user types in a value in the first textbox and hits the ENTER key, the first form is submitted and a popup shows the results of the search.  If the user fills in any fields in the second part of the form and hits ENTER on any textbox, the second form is submitted and the results are popped up.  The two popups are different, obviously.

    I've been struggling with how to capture the ENTER key and send the user one or the other result popup.  Then someone directed me to your article here.  I inserted two panels enclosing the two different sets of controls into the panels.  I set the defaultbutton for the panels and tried it out.  It works the first time I hit the ENTER key on any field.  But if the user hits ENTER again, the first form is ALWAYS submitted. So, for example, if they didn't get the restuls they expected and went back to the main form and added more criteria and hit ENTER, it would submit the first form, which is incorrect.  The user has to refresh the page to get the second form to submit on ENTER (or click the submit button).  This won't work for us.  I'm wondering if this is a bug, and if so is there a workaround?  if not, what could I be missing to cause this behavior?

    Thanks so much for your help - this webapp is nearly done, but this issue is causing me a lot of headaches!  Hopefully this will be resolved soon.

    Thanks again,

    eddie
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-23-2006, 1:03 PM
    • Loading...
    • fengkui
    • Joined on 06-21-2006, 7:51 AM
    • Posts 1
    • Points 5
    you may use javascript in the client side.
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-23-2006, 9:09 PM
    • Loading...
    • Yovav
    • Joined on 02-19-2003, 7:35 PM
    • Las Vegas
    • Posts 171
    • Points 698

    eappell:
    I have a page with two forms, one with just one textbox, and the other with many textboxes, dropdownlists, checkboxes, etc.  This is a search page, with the results showing up in popups.  The basic requirement is that if the user types in a value in the first textbox and hits the ENTER key, the first form is submitted and a popup shows the results of the search.  If the user fills in any fields in the second part of the form and hits ENTER on any textbox, the second form is submitted and the results are popped up.  The two popups are different, obviously.

    I've been struggling with how to capture the ENTER key and send the user one or the other result popup.  Then someone directed me to your article here.  I inserted two panels enclosing the two different sets of controls into the panels.  I set the defaultbutton for the panels and tried it out.  It works the first time I hit the ENTER key on any field.  But if the user hits ENTER again, the first form is ALWAYS submitted. So, for example, if they didn't get the restuls they expected and went back to the main form and added more criteria and hit ENTER, it would submit the first form, which is incorrect.  The user has to refresh the page to get the second form to submit on ENTER (or click the submit button).  This won't work for us.  I'm wondering if this is a bug, and if so is there a workaround?  if not, what could I be missing to cause this behavior?

    Thanks so much for your help - this webapp is nearly done, but this issue is causing me a lot of headaches!  Hopefully this will be resolved soon.

    Thanks again,

    eddie

     

    I'm having the same problem with two controls on the same page (Signin & Signup)

    I wonder if this was forgotten when creating ASP.NET coz I can not thing on any "EZ workaround" (as it was so EZ 2 implement on classic ASP)

    any ideas ?

    code samples ?

    (hate to mess with all those low level events :-)

    10X N C U L8R
    Best Regards
    YovavG@GMail.com
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-24-2006, 6:47 AM
    • Loading...
    • LudovicoVan
    • Joined on 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,725
    • Points 8,569

    Yovav:> I'm having the same problem with two controls on the same page

    Just an idea, not checked:

       DefaultButton='<%= MyControl.FindControl("MyControlButton").ClientID %>'

    Let me know.

    -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-24-2006, 12:49 PM
    • Loading...
    • Yovav
    • Joined on 02-19-2003, 7:35 PM
    • Las Vegas
    • Posts 171
    • Points 698

    Another sample code (to be applied on each control):

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signin.ascx.cs" Inherits="Signin" %>

    <

    asp:Panel DefaultButton="_buttonSignin" runat="server">

    ...

    </

    asp:Panel>

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Signup.ascx.cs" Inherits="Signup" %>

    <

    asp:Panel DefaultButton="_buttonSignup" runat="server">

    ...

    </

    asp:Panel>

     

    So now - whenever the control area is being clicked (or focused) or any control inside it - the default button will used,

    It will not go to a default button if the focus is outside of these two controls (for example - on the web site header) - the following script might also B useful:

    <script language="javascript">
    // <!--

        function onKeySignin()
        {
        if (window.event.keyCode == 13)
            {
            document.getElementById('<%=_buttonSignin.ClientID%>').focus();
            document.getElementById('<%=_buttonSignin.ClientID%>').click();
            }
        }

        document.attachEvent("onkeydown", onKeySignin);

    // -->
    </script>

    10X N C U L8R
    Best Regards
    YovavG@GMail.com
  • Re: ASP.NET 2.0 - Enter Key - Default Submit Button

    08-28-2006, 1:09 AM
    • Loading...
    • GrantDG
    • Joined on 04-13-2006, 10:46 PM
    • Sydney, Australia
    • Posts 3
    • Points 15

    After much seraching, I found a workable solution to this problem. Basically we need to manually set up the same client code that would be injected if we were using a Default button for a panel.

    Assume we have a <asp:Login ... /> control on a page named "Login1"

    In the Page_Load event handler:

    ------ 
    Control ctl = Login1.FindControl("LoginButton");
    if(ctl != null && ctl is IButtonControl)
       Login1.Attributes.Add(
    "onkeypress", string.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')",ctl.ClientID));

    -------

    I'm assuming that we also have the WebForm_FireDefaultButton client script file available on the page... (if not - you'll get a JavaScript error) 

     

    People go through periods in the lives - I'd rather go through exclamation points.
    - Schultz
Page 1 of 6 (77 items) 1 2 3 4 5 Next > ... Last ยป