85

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.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Ron
  • 1,901
  • 4
  • 19
  • 38
  • Do you have a panel in the content page, maybe you could set default button as the submit in that case – V4Vendetta Oct 03 '11 at 16:57
  • Thanks V4Vendetta, yes, i resolved this through adding a panel and setting the defaultbutton to submit – Ron Oct 04 '11 at 03:43

6 Answers6

101

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>
Kirk
  • 16,182
  • 20
  • 80
  • 112
  • Please note that this solution relies on javascript, as something like `onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl00_MainContentPlaceHolder_btnSave')">` is added to your page – R. Schreurs Aug 14 '15 at 12:14
  • Perfect Solution - Thanks Kirk – Sensa Jan 29 '17 at 09:42
  • 1
    @kirk What if text-box is somewhere above the button I mean not together then how we could achieve the same? – Jyotish Singh Jul 06 '17 at 05:35
  • @JyotishSingh You can do the same thing if you put the `TextBox` and `Button` within the same `Panel`. Then set the _DefaultButton_ property. – Kirk Jul 06 '17 at 16:33
37

if you need to do it from code, use

Me.Form.DefaultButton = Me.btn.UniqueID

Where btn is your button control.

Mike
  • 23,542
  • 14
  • 76
  • 87
Lester
  • 371
  • 3
  • 2
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>
mellamokb
  • 56,094
  • 12
  • 110
  • 136
6

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" />
adinas
  • 4,150
  • 3
  • 37
  • 47
1

Microsoft say:

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

enter link description here

VolkanCetinkaya
  • 645
  • 7
  • 13
0
$(document).ready(function(){
    document.getElementById("text_box_id")
    .addEventListener("keyup", function(event) {
    event.preventDefault();
    if (event.keyCode === 13) {
        document.getElementById("button_id").click();
    }
    });
});
JavaGeek
  • 475
  • 5
  • 14