how to set a default 'enter' on a certain button
Asked Answered
G

6

86

There is a textbox on a ContentPage. When the user presses Enter in that textbox I am trying to fire a 'Submit' button on this ContentPage. I'd like to fire off that particular button's event.

Instead, there is a search textbox & button on the top of the page from a MasterPage, and this search button's event fires off.

How do I control to fire off this ContentPage's submit button, instead of the MasterPage's search button?

I am using Ektron CMS for my content management.

Greathearted answered 3/10, 2011 at 16:52 Comment(2)
Do you have a panel in the content page, maybe you could set default button as the submit in that caseSandglass
Thanks V4Vendetta, yes, i resolved this through adding a panel and setting the defaultbutton to submitGreathearted
D
102

The easiest way is to put the fields and button inside of a Panel and set the default button to the button you want to be activated on enter.

<asp:Panel ID="p" runat="server" DefaultButton="myButton">
  <%-- Text boxes here --%>
  <asp:Button ID="myButton" runat="server" />
</asp:Panel>
Delocalize answered 3/10, 2011 at 16:58 Comment(4)
Please note that this solution relies on javascript, as something like onkeypress="javascript:return WebForm_FireDefaultButton(event, &#39;ctl00_MainContentPlaceHolder_btnSave&#39;)"> is added to your pagePontiff
Perfect Solution - Thanks KirkUnshaped
@kirk What if text-box is somewhere above the button I mean not together then how we could achieve the same?Farandole
@JyotishSingh You can do the same thing if you put the TextBox and Button within the same Panel. Then set the DefaultButton property.Delocalize
L
37

if you need to do it from code, use

Me.Form.DefaultButton = Me.btn.UniqueID

Where btn is your button control.

Lemuel answered 14/2, 2013 at 23:37 Comment(0)
A
18

You can use the DefaultButton property on either a server-side form control or Panel control. In your case, group the controls together in a Panel that should fire off the same button:

<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
    ...
    <asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
    ...
    <asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>
Anikaanil answered 3/10, 2011 at 16:55 Comment(0)
C
7

You can now use UseSubmitBehavior property to disable all the buttons you don't want to fire when hitting submit (check out the documentation for more info)

    <asp:Button ID="BtnNotToFIre" runat="server" Text="Search" UseSubmitBehavior="false" />
Claudieclaudina answered 2/7, 2017 at 12:43 Comment(2)
Absolute star! This is what I need!Entoderm
And it doesn't require additional html/asp.net elements that may not work with the page layout.Nonillion
L
1

Microsoft say:

<form id="Form1"
    defaultbutton="SubmitButton"
    defaultfocus="TextBox1"
    runat="server">

enter link description here

Latricialatrina answered 13/6, 2018 at 15:59 Comment(0)
S
0
$(document).ready(function(){
    document.getElementById("text_box_id")
    .addEventListener("keyup", function(event) {
    event.preventDefault();
    if (event.keyCode === 13) {
        document.getElementById("button_id").click();
    }
    });
});
Skvorak answered 22/5, 2018 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.